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

Allow children to be objects instead of arrays #489

Open
the1mills opened this issue Dec 3, 2018 · 6 comments
Open

Allow children to be objects instead of arrays #489

the1mills opened this issue Dec 3, 2018 · 6 comments

Comments

@the1mills
Copy link

the1mills commented Dec 3, 2018

I have only played with this lib for a little while, but it's shame that it seems like every child has to be an array. For example:

<bookstore>
  <book>foo</book>
  <zoom>bar</zoom>
</bookstore>

so that will get parsed to:

{
  bookstore:{
    "$":{},
    "book":['foo'],
    "zoom":['bar']
  }
}

but it would be really cool, if there was a way to standardize the XML, so that if it assumes it's not a list, unless you explicitly tell it, so that the above would get parsed to:

{
  bookstore:{
    "$":{},
    "book": 'foo',
    "zoom": 'bar'
  }
}

so that if you did this:

<bookstore list="book,zoom">
  <book>foo</book>
  <zoom>bar</zoom>
  <dang>zzz</dang>
</bookstore>

it would get parsed to:

{
  bookstore:{
    "$":{},
    "book": ['foo'],
    "zoom": ['bar'],
    "dang": 'zzz'
  }
}
@Leonidas-from-XIV
Copy link
Owner

Leonidas-from-XIV commented Dec 3, 2018 via email

@the1mills
Copy link
Author

@Leonidas-from-XIV yep I agree, I am trying to think of a generic solution, but yeah there might not be one. If you don't control the XML that comes to you, or if it could change, then you have to handle the format that it's in. Personally, I consider this a faulty design on the part of XML.

@the1mills
Copy link
Author

@Leonidas-from-XIV this is what I am wondering -
https://gist.github.com/ORESoftware/c917af15b1e5bce9617e1ebcec780554

maybe XML has a newer standard that will fix this. It's crazy.

@ALCarden
Copy link

@Leonidas-from-XIV this is what I am wondering -
https://gist.github.com/ORESoftware/c917af15b1e5bce9617e1ebcec780554

maybe XML has a newer standard that will fix this. It's crazy.

There is a standard, it's just in the javascript world everyone seems to forget things called schemas. Proper XML tools in most languages when doing parsing, code gen etc will use and respect a schema.

The schema can tell you if a child is a list via the schema properties, minOccurs, maxOccurs, mustOccurs, e.g. if minOccurs and maxOccurs = 1 then its a single element not a list, if it had minOccurs=0 and no maxOccurs then it would be optional list

@Leonidas-from-XIV
Copy link
Owner

I am aware of the existence of schemas. But while sometimes you have a schema and can apply it often you just get some XML so I'm parsing it in the most general way.

I am not sure it is even worth implementing support for this given the low amount of XML Schema in the wild.

@smandlavdiya
Copy link

smandlavdiya commented Feb 16, 2021

@Leonidas-from-XIV do we have an option to add an attribute to all the list when we build the xml from json
example array=true for all the list irrespective of number of elements? and can use same attribute to make it array when parse json from same xml

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

4 participants