XStream

Strip root node generated JSON

Details

  • Type: Improvement Improvement
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.3
  • Fix Version/s: 1.3.1
  • Component/s: Core
  • Labels:
    None

Description

I want to use a resulting JSON formatted XStreamed object in a browser  
in JS.

The way JSON libraries typically works are to push the resulting  
object into a closure :

function (data) {
   // do something with data. like .....
   alert(data.field1)
}

Well that would be the case, but for objects that come from the two  
JSON capable drivers in XStream, they are doing to look more like

function (data) {
   alert(data.rootNode.field1)
}

'data' is prepended by the JSON libraries.  rootNode (more typically  
the Java type name of the root node) is made by XStream.

Could we have a mode of operation for the two JSON types that allowed  
for "no root node" ?  Like so ..

   {"name" : "Fred", "sex" : "Male"}

instead of ..

   {"MyPerson":{"name" : "Fred", "sex" : "Male"}}

Issue Links

Activity

Hide
Paul Hammant added a comment -

As an option, this is not needed. It should be like this all the time.

Patch supplied. It works perfectly, but you could judge it to be a bit hacky. There's some schenanigans to ensure there is always an opening brace.

Show
Paul Hammant added a comment - As an option, this is not needed. It should be like this all the time. Patch supplied. It works perfectly, but you could judge it to be a bit hacky. There's some schenanigans to ensure there is always an opening brace.
Hide
Paul Hammant added a comment -

Actually, the patch adds more tests around the returning of single primative too.

These are fine, and are now tested for :

{1} {"hello"}
Show
Paul Hammant added a comment - Actually, the patch adds more tests around the returning of single primative too. These are fine, and are now tested for : {1} {"hello"}
Hide
Paul Hammant added a comment -

Grr, darn formatting :

  {1} 
  {"hello"}
Show
Paul Hammant added a comment - Grr, darn formatting :
  {1} 
  {"hello"}
Hide
Joerg Schaible added a comment -

Renamed issue, it is different to the original one, that I have cloned to XSTR-531.

Show
Joerg Schaible added a comment - Renamed issue, it is different to the original one, that I have cloned to XSTR-531.
Hide
Joerg Schaible added a comment -

I've committed a modified version of your patch, you may have a look at this preliminary version. What do you think?

Your patch produced invalid JSON in two cases:

  1. It is simply not possible to convert a single value object without a root node. The current code will throw a ConversionException.
  2. Arrays are root elements. It is not valid to place an array into an object without a label.

I implemented the no-root-mode as option, since

  1. we should not change the produced JSON format for a minor release anyway
  2. IMHO it should not be always like this, look at http://www.json.org/example.html (you know this example)

BTW: http://www.jslint.com/ is your friend

Show
Joerg Schaible added a comment - I've committed a modified version of your patch, you may have a look at this preliminary version. What do you think? Your patch produced invalid JSON in two cases:
  1. It is simply not possible to convert a single value object without a root node. The current code will throw a ConversionException.
  2. Arrays are root elements. It is not valid to place an array into an object without a label.
I implemented the no-root-mode as option, since
  1. we should not change the produced JSON format for a minor release anyway
  2. IMHO it should not be always like this, look at http://www.json.org/example.html (you know this example)
BTW: http://www.jslint.com/ is your friend
Hide
Joerg Schaible added a comment -

I've finalized this functionality now.

Show
Joerg Schaible added a comment - I've finalized this functionality now.
Hide
Joerg Schaible added a comment -

Set correct fix version.

Show
Joerg Schaible added a comment - Set correct fix version.
Hide
Joerg Schaible added a comment -

Fixed for upcoming release.

Show
Joerg Schaible added a comment - Fixed for upcoming release.

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: