I am designing an API for my webapp.
I was thinking to support only JSON responses (not XML) because more streamlined.
But I have just bumped to this XML:
edit
A similar method is advocated on http://www.jsonml.org/. They coined the term json markup language.
You can pick any mapping you like, but if you map
txt
to
{"el":{"attr":"value","content":"txt"}}
then how would you map:
txt2
I'd make use of the fact that some attibute names are forbidden.
{"el":{"attr":"value", "content":"txt1", "":["txt"]}
Or a more compex example:
on
I just put some text here
main_window
500
500
250mm
250
center
could map to:
{"widget":{"":[
{"debug":{"":["on"]}},
{"window":{"title":"Sample Konfabulator Widget", "": [
"I just put some text here",
{"name":{"":["main window"]}},
{"width":{"":["500"]}},
{"height":{"":["500"]}}
]},
{"image":{"src":"Images/Sun.png", "name":"sun1", "":[
{"hOffset":{"":["250",{"unit":{"":["mm"]}}]}},
{"vOffset":{"":["250"]}},
{"alignment":{"":["center"]}}
}
]}
The rules for this conversion are unambiguous:
To safe space, there is a way to unambigously simplify mentioned mapping:
{"widget":{"":[
{"debug":"on"},
{"window":{"title":"Sample Konfabulator Widget", "": [
"I just put some text here",
{"name":"main window"},
{"width":"500"},
{"height":"500"}
]},
{"image":{"src":"Images/Sun.png", "name":"sun1", "":[
{"hOffset":["250",{"unit":"mm"}]},
{"vOffset":"250"},
{"alignment":"center"}
}
]}
If an element doesn't have any attibutes, the value object (containing the special empty string mapping to an array) is replaced by the array directly. So instead of:
{"hOffset":{"":["250",{"unit":{"":["mm"]}}]}}
you get
{"hOffset":["250",{"unit":["mm"]}]}
If the element content is just text, the array containing the string value is replaced by the string value directly, so you get:
{"hOffset":["250",{"unit":"mm"}]}
This way, there will always be exactly one way to map the jml (json markup language) back to xml (or html)