Description
Originally filed by darron.schall on 2006-12-29T21:02:09
What steps will reproduce the problem?
- 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 commentedon Jul 28, 2010
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 commentedon Jul 28, 2010
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 commentedon Jan 31, 2012
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:
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 commentedon Feb 1, 2012
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.