diff --git a/src/main/java/net/sf/json/JSONSerializer.java b/src/main/java/net/sf/json/JSONSerializer.java index 904cb151..3d87fb0d 100644 --- a/src/main/java/net/sf/json/JSONSerializer.java +++ b/src/main/java/net/sf/json/JSONSerializer.java @@ -103,6 +103,8 @@ public static JSON toJSON( Object object, JsonConfig jsonConfig ) { json = toJSON( (String) object, jsonConfig ); }else if( JSONUtils.isArray( object ) ){ json = JSONArray.fromObject( object, jsonConfig ); + }else if( object instanceof JSONObject){ + json = (JSONObject)object; }else{ try{ json = JSONObject.fromObject( object, jsonConfig ); diff --git a/src/test/java/net/sf/json/TestJSONObject.java b/src/test/java/net/sf/json/TestJSONObject.java index 719e0698..0ac11da0 100644 --- a/src/test/java/net/sf/json/TestJSONObject.java +++ b/src/test/java/net/sf/json/TestJSONObject.java @@ -376,6 +376,16 @@ public void testElement_Object() { jsonObject.getJSONObject( "bean" ) ); } + public void testElement_Object_nested() { + JSONObject jsonObject1 = new JSONObject(); + jsonObject1.element( "str", "\"[]\"" ); + Assertions.assertTrue(jsonObject1.get( "str" ) instanceof String); + + JSONObject jsonObject2 = new JSONObject(); + jsonObject2.element( "obj", jsonObject1); + Assertions.assertTrue(jsonObject2.getJSONObject( "obj" ).get( "str" ) instanceof String); + } + public void testElement_String() { JSONObject jsonObject = new JSONObject(); jsonObject.element( "str", "json" );