tags:

views:

380

answers:

1

I am currently deserializing JSON using XStream, and it has been working great. However, when I have JSON string like the following

{
    key1: { an_object: { something: 'foobar' } },
    key2: { another_object: { data: 'hi' }
}

most notably it doesn't have a root node, I'm not sure how to parse it. Basically, I want the opposite of DROP_ROOT_NODE for the deserialization.

+1  A: 

The short answer is "you can't".

XStream needs to know what class to instantiate, it gets that knowledge from JSON (or XML) data. Class name can be aliased, but it can not be omitted. You can work around by:

  1. Manually wrapping your JSON string with root node containing your class name (or alias)
  2. Writing your own reader that would do it for you. However, in this case you'll still need to pass your class name (alias) to that reader either explicitly or by convention (e.g. always prepend 'root' but then configure it as alias to your class in XStream instance) - so I don't think this is any cleaner than #1.
ChssPly76