Skip to content
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

Seems inconsistent in semantics when serializing java values as Strings of xml and json #603

Closed
ifindya opened this issue Jul 28, 2023 · 3 comments

Comments

@ifindya
Copy link

ifindya commented Jul 28, 2023

Trying to serialize POJOs containing arrays or collections.
Noticed that arrays or collections are handled correctly in XML when serializing:

<person>
	<id>0</id>
	<infos>
		<info>
			<name>Lisa</name>
			<sex>female</sex>
		</info>
		<info>
			<name>Jack</name>
			<sex>male</sex>
		</info>
	</infos>
</person>

But in JSON:

{
	"person": {
		"id": 0,
		"infos": [
			{
				"name": "Lisa",
				"sex": "female"
			},
			{
				"name": "Jack",
				"sex": "male"
			}
		]
	}
}

And not sure that we should get:

{

  "person": {

    "id": "0",

    "infos": {

      "info": [

        {

          "name": "Lisa",

          "sex": "female"

        },

        {

          "name": "Jack",

          "sex": "male"

        }

      ]

    }

  }

}

We use Jackson Dataformat XML 2.15.2

codes:

@JacksonXmlRootElement(localName = "person")
@JsonRootName(value = "person")
public class Person {

	private int id;

	@JacksonXmlElementWrapper(localName = "infos")
	@JacksonXmlProperty(localName = "info")
	private List<Info> infos;
        
        ...
	setters and getters
        ...

	static class Info {

		public Info(String name, String sex) {
			super();
			this.name = name;
			this.sex = sex;
		}

		private String name;

		private String sex;

                 ...
		setters and getters
                 ...

	}
@pjfanning
Copy link
Member

Try putting @JsonRootName("info") on static class Info. Annotations that have names starting with JacksonXml only affect XML (eg @JacksonXmlProperty(localName = "info")) and do not affect JSON.

@ifindya
Copy link
Author

ifindya commented Jul 28, 2023

Try putting @JsonRootName("info") on static class Info. Annotations that have names starting with JacksonXml only affect XML (eg @JacksonXmlProperty(localName = "info")) and do not affect JSON.

Adding @JsonRootName on Info like below, i've tried but it didn't work.

@JsonRootName("info")
static class Info {

		public Info(String name, String sex) {
			super();
			this.name = name;
			this.sex = sex;
		}

		private String name;

		private String sex;

                 ...
		setters and getters
                 ...

	}

@cowtowncoder
Copy link
Member

Behavior of JSON and XML differ because they are conceptually different data formats: they cannot be handled identically.
I don't just mean curly braces vs angle brackets, but rather that structural components are different.

Because of this certain wrapping that XML typically requires (since it lacks native Array type, elements are used for Arrays and Objects) are not added in JSON.

So I don't think there is anything to work on here.

If you have specific types of JSON or XML structures you want, please file issue at appropriate repo (for XML, this one, for JSON jackson-databind). But general "why isn't JSON output like XML" (and vice versa) questions are not useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants