-
Notifications
You must be signed in to change notification settings - Fork 241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Keep specific element to single line while using indent with serde serializer #655
Comments
The difference between Anyway, writing of struct content is buffered, so we should be able to understand if intendation is needed or not and apply it if required. I would happy to merge a PR that do that. |
Some investigation. Currently the type struct Xml {
tag1: String,// = "value1"
#[serde(rename = "$text")] // or $value
text: String,// = "text"
tag2: String,// = "value2"
} will be serialized to such XML when using indent with 2 chars: <some-surrounding-tag>
<tag1>value1</tag1>
text
<tag2>value2</tag2>
</some-surrounding-tag> The <some-surrounding-tag>
<tag1>value1</tag1>text<tag2>value2</tag2>
</some-surrounding-tag> If <some-surrounding-tag>text<tag2>value2</tag2>
</some-surrounding-tag> <some-surrounding-tag>
<tag1>value1</tag1>text</some-surrounding-tag> @ShaddyDC, @RedIODev and anyone else who is interested could confirm, that those expectations are correct? |
Not sure. <some-surrounding-tag>
<tag1>value1</tag1>
text
<tag2>value2</tag2>
</some-surrounding-tag> Looks correct to me. But I'm not familiar what the correct formatting should be. Great regards |
Java's Jackson XML gives such result: <Xml>
<tag1>value1</tag1>text
<tag2>value2</tag2>
</Xml> If comment tag fields, then: <Xml>text
<tag2>value2</tag2>
</Xml>
<Xml>
<tag1>value1</tag1>text
</Xml> Both: <Xml>text</Xml> So, it does not write indent before text field, but writes after. Or: tag always indented. Codepackage ru.mingun.xml.test;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText;
/*
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.7</version>
</dependency>
*/
public class Xml {
@JacksonXmlProperty
String tag1;
@JacksonXmlText
String text;
@JacksonXmlProperty
String tag2;
public static void main(String[] args) throws Exception {
final XmlMapper mapper = new XmlMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
final Xml bean = new Xml();
bean.tag1 = "value1";
bean.text = "text";
bean.tag2 = "value2";
System.out.println(mapper.writeValueAsString(bean));
}
} |
Java's XmlBeans: <Xml>
<tag1>value1</tag1>
text
<tag2>value2</tag2>
</Xml> Without <Xml>
text
<tag2>value2</tag2>
</Xml> Without <Xml>
<tag1>value1</tag1>
text
</Xml> Without <Xml>text</Xml> So current behaviour almost the same, as XmlBeans, except the text only case. Codepackage ru.mingun.xml.test;
import org.apache.xmlbeans.XmlObject;
/*
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>3.1.0</version>
</dependency>
*/
public class Xml {
public static void main(String[] args) throws Exception {
System.out.println(XmlObject.Factory.parse("<Xml><tag1>value1</tag1>text<tag2>value2</tag2></Xml>"));
System.out.println(XmlObject.Factory.parse("<Xml>text<tag2>value2</tag2></Xml>"));
System.out.println(XmlObject.Factory.parse("<Xml><tag1>value1</tag1>text</Xml>"));
System.out.println(XmlObject.Factory.parse("<Xml>text</Xml>"));
}
} |
Python: <Xml>
<tag1>value1</tag1>text<tag2>value2</tag2>
</Xml> Without <Xml>text<tag2>value2</tag2>
</Xml> Without <Xml>
<tag1>value1</tag1>text</Xml> Without <Xml>text</Xml> The same, as I proposed in the last comment Codeimport xml.etree.ElementTree as ET
root = ET.fromstring("<Xml><tag1>value1</tag1>text<tag2>value2</tag2></Xml>")
ET.indent(root)
print(ET.tostring(root, encoding='unicode'))
root = ET.fromstring("<Xml>text<tag2>value2</tag2></Xml>")
ET.indent(root)
print(ET.tostring(root, encoding='unicode'))
root = ET.fromstring("<Xml><tag1>value1</tag1>text</Xml>")
ET.indent(root)
print(ET.tostring(root, encoding='unicode'))
root = ET.fromstring("<Xml>text</Xml>")
ET.indent(root)
print(ET.tostring(root, encoding='unicode')) |
I don't have much experience with xml, so I can only answer for the narrow use case I have, and testing it is a bit of a pain, so I'll answer based on what I believe to be the case instead. I think this example which you gave would be correct for me. <some-surrounding-tag>
<tag1>value1</tag1>text<tag2>value2</tag2>
</some-surrounding-tag> The problem was that having newlines around "text" would parse them as part of the text. I assume that would not be a problem with this example, and it looks otherwise as expected to me. I am not confident about that, however, but for my specific use case it is enough if this works: <some-surrounding-tag attribute="test1">text2</some-surrounding-tag> I'm sorry that I cannot provide much broader feedback. |
C# (mono implementation): <?xml version="1.0" encoding="us-ascii"?>
<Xml xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<tag1>value1</tag1>text<tag2>value2</tag2></Xml> Without <?xml version="1.0" encoding="us-ascii"?>
<Xml xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">text<tag2>value2</tag2></Xml> Without <Xml xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<tag1>value1</tag1>text</Xml> Without <?xml version="1.0" encoding="us-ascii"?>
<Xml xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">text</Xml> Writing text disables indent of next fields, but it is still applied to the fields before the text field Codeusing System;
using System.Xml;
using System.Xml.Serialization;
public class Xml
{
public String tag1;
[XmlText]
public String text;
public String tag2;
public static void Main(string[] args)
{
var s = new XmlSerializer(typeof(Xml));
using (var xmlWriter = XmlWriter.Create(Console.Out, new XmlWriterSettings { Indent = true }))
{
s.Serialize(xmlWriter, new Xml {
tag1 = "value1",
text = "text",
tag2 = "value2",
});
}
}
} |
Hi, thanks for making this library; I'm getting a lot of use out of it.
However, I've run into an issue where I have to format some code a certain way that a proprietary reader expects it to be. Namely, I've got these structures:
I'm writing like this, though admittedly with some context missing:
The texture coordinates serialise to something like this:
However, the program I need to serialize for requires this format instead:
Is there some way to achieve this that I'm not aware of?
I'm currently working around it by just not using indentation, so everything is written in a single line, but it would be nice to have.
The text was updated successfully, but these errors were encountered: