Skip to content

JSON does not encode XML instances into anything useful #6

Open
@darronschall

Description

@darronschall

Originally filed by darron.schall on 2006-12-29T21:02:09

What steps will reproduce the problem?

  1. trace( JSON.encode( ) );

What is the expected output? What do you see instead?

Like Date instance (see issue #2), there is no standard XML encoding syntax
defined for JSON. Because E4X is a core part of ECMAScript Edition 4, I
would suspect that the JSON-encoded XML would just use <> like an XML literal.

So, the above trace would produce the following JSON string:

"<test attr1="test" attr2="42"><child1 childAttr1="false" />"

Along the same lines, the following:

var arr:Arary = new Array();
arr.push( );
arr.push( true );
trace( JSON.encode( arr ) );

... should probably output: "[,true]"

The current output is {}, which is clearly not good, no matter what the
expected output should be.

Please use labels and text to provide additional information.

There needs to be a consensus on what the expected output should be before
this is addressed.

This could spring into a religious debate, "Why on earth would anyone want
to encode XML in JSON? It's apples and oranges!"... but rather than get
into politics, I'd rather just agree on a universal solution / standard for
dealing with XML instances in JSON encoding.

Activity

darronschall

darronschall commented on Jul 28, 2010

@darronschall
ContributorAuthor

Updated by darron.schall on 2006-12-29T21:02:55

Changed issue title (accidentally left it as default when I entered it).

Title changed from 'JSON does not encode XML instances into anything useful' to 'JSON does not encode XML instances into anything useful'

darronschall

darronschall commented on Jul 28, 2010

@darronschall
ContributorAuthor

Updated by mikechambers on 2007-01-10T04:41:34

I believe Firefox 2 has support for e4x. Might be useful to see how they handle it.

demauk

demauk commented on Jan 31, 2012

@demauk

For me, converting XML to JSON has three major issues to be resolved: a) what do to about the XML root element name? b) how to encode attributes? c) how to encode text nodes when the parent element has attributes or other element children?

To that end, I added an xmlToString() function to JSONEncoder and a JSONEncoderXMLOptions class as an optional parameter to the JSONEncoder constructor. Let's say you have this XML:

<root>
  <category>Sci-Fi</category>
  <books>
    <book id="1">Text for Book 1</book>
    <book id="2">Text for Book 2</book>
  </books>
</root>

Option omitRootElement:Boolean;

Sometimes you want the JSON version to simply be { "books" : [ etc. ] }. And sometimes you want everything: { "root" : { "books" : [ etc. ] } }.

Option useAttributeAtSymbol:Boolean

Defaults to true. Determines whether or not attributes should be encoded with an @ sign at the beginning, like "@id" : "1" I think this should be standard, but I've set it as an option because I've seen implementations where they don't use @. IMO, if you can't differentiate attributes, you can't turn the JSON back into XML.

Option defaultSimpleContentLabel:String

Defaults to #text. A simple element like <category> can have its text node as its only value, like "category" : "Sci-Fi". But the text node of <book> needs its own property name, which in this case would look like: "book" : { "@id" : "1", "#text" : "Text for Book 1" }.

Other implementations use "label" but that's already a common property name for many actionscript objects.

I'll see if I can offer a patch, at least as a reference.

demauk

demauk commented on Feb 1, 2012

@demauk

I've pushed my changes to my fork: https://github.com/demauk/as3corelib/tree/master/src/com/adobe/serialization/json

There are kind of a lot of changes in that one commit. Sorry about that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @darronschall@demauk

        Issue actions

          JSON does not encode XML instances into anything useful · Issue #6 · mikechambers/as3corelib