json-lib icon indicating copy to clipboard operation
json-lib copied to clipboard

Strings that are also valid JSON arrays/objects are deserialized incorrectly

Open tofay opened this issue 8 years ago • 0 comments

This occurs when the string is nested and more than one level deep, e.g it occurs for {"a": {"b": "[c-d]"}} but not {"b": "[c-d]"}

I'd expect the string "{"a": {"b": "[c-d]"}}" to be deserialized to: { "a" : { "b" : "[c-d]" } }

It's actually deserialized to: { "a" : { "b" : ["c-d"] } }

Here's a test for this that currently fails:

   public void testElement_Object_nested() {
      JSONObject jsonObject1 = new JSONObject();
      jsonObject1.element( "str", "\"[]\"" );
      Assertions.assertTrue(jsonObject1.get( "str" ) instanceof String); // This assertion passes
      
      JSONObject jsonObject2 = new JSONObject();
      jsonObject2.element( "obj", jsonObject1);
      Assertions.assertTrue(jsonObject2.getJSONObject( "obj" ).get( "str" ) instanceof String); // This assertion fails
   }

I'm running version 2.4.

tofay avatar Oct 03 '17 08:10 tofay