Skip to content

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

@tofay

Description

@tofay

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions