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

json output #29

Open
MortalCatalyst opened this issue Feb 9, 2015 · 5 comments
Open

json output #29

MortalCatalyst opened this issue Feb 9, 2015 · 5 comments

Comments

@MortalCatalyst
Copy link

Just wondering why saxerator doesn't feature a :json output option?

@soulcutter
Copy link
Owner

Because nobody has contributed that feature yet!

But also there are issues with translating XML to JSON, because JSON does not have a direct corollary to element attributes. Saxerator gets around this by using extended classes for String and Hash which have attributes collections, but it's less-clear to me a way to represent this in JSON.

I'm open to proposals!

@Tails
Copy link

Tails commented Jan 25, 2018

similarly, I'm looking for a flat :string output so I can parse the result in my own classes!

@v3rmin
Copy link

v3rmin commented Feb 23, 2018

  • XML: Element names can contain letters, digits, hyphens, underscores, and periods.
  • JSON: Any valid string is a valid key

Proposal: Use @ char for attributes

XML:

<products>
  <product>
    <name>iPhone</name>
    <price currency="USD">1337</price>
  </product>
</products>

OUTPUT:

parser.for_tag('product').each do |item|
  p item # => { 'name' => 'iPhone', 'price' => 1337, 'price@currency' => 'USD' }
end

@fanantoxa
Copy link
Collaborator

@v3rmin With this flow ti'll require to parse attribute names and it'd hard to understand that this it actually relate to the same node.
What if we'll represent our custom strings and hashes like and object?

parser.for_tag('product').each do |item|
  p item # => {
     'name': 'iPhone',
     'price': {
       value: 1337,
       attributes: {
          'currency: 'USD'
       }
     }
   }
end

Also we're talking about JSON it should be a String, not a Hash.
@soulcutter how you like this?

@v3rmin
Copy link

v3rmin commented Feb 23, 2018

I think it must be a markup that doesn't collide with valid XML.
What if the XML looked like this:

xml = <<-XML
<product>
  <name>iPhone</name>
  <price currency="USD">
    <value>1337</value>
    <attributes>
      <currency>EUR</currency>
    </attributes>
  </price>
</product>
XML

require 'saxerator'

parser = Saxerator.parser(xml)
parser.for_tag('product').each do |item|
  p item # => {"name"=>"iPhone", "price"=>{"value"=>"1337", "attributes"=>{"currency"=>"EUR"}}}
end

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

5 participants