Deserialize string value by JSON-lib (net.sf.json)

744 views Asked by At

I have discovered problem in JSON-Lib with deserialization of JSON string data in nested objects. Following code in Groovy demonstrates problem, you can test it in Groovy Console:

@Grab('net.sf.json-lib:json-lib:2.3:jdk15')
import net.sf.json.*

def s = '''\
{
  "attributes": "'{test:1}'",
  "nested": { "attributes": "'{test:1}'" }
}'''

def j = JSONSerializer.toJSON(s)

println j
assert j.attributes == "{test:1}" // OK
assert j.nested.attributes == "{test:1}" // Failed

In the example above both attributes in JSON have the same string value "{test:1}". JSONSerializer deserialize the first one into String value but second one into (maybe) JSONObject. I'm not sure because class of that object returns null.

For different string value (which doesn't look like JSON data, e.g. "blah") it works.

How can I change this behavior?

UPDATE

I have tried to quote string values based on JSON-Lib documentation here (thanks to @GemK for pointing there), but with same result. Quoting string values doesn't work in nested objects :-(

0

There are 0 answers