saxerator icon indicating copy to clipboard operation
saxerator copied to clipboard

json output

Open MortalCatalyst opened this issue 10 years ago • 5 comments

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

MortalCatalyst avatar Feb 09 '15 05:02 MortalCatalyst

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!

soulcutter avatar Feb 11 '15 16:02 soulcutter

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

Tails avatar Jan 25 '18 11:01 Tails

  • 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

v3rmin avatar Feb 23 '18 11:02 v3rmin

@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?

fanantoxa avatar Feb 23 '18 11:02 fanantoxa

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

v3rmin avatar Feb 23 '18 12:02 v3rmin