diff --git a/src/main/java/com/alibaba/fastjson/parser/DefaultJSONParser.java b/src/main/java/com/alibaba/fastjson/parser/DefaultJSONParser.java index 559547abbf..e64e9a67f7 100755 --- a/src/main/java/com/alibaba/fastjson/parser/DefaultJSONParser.java +++ b/src/main/java/com/alibaba/fastjson/parser/DefaultJSONParser.java @@ -432,7 +432,7 @@ public final Object parseObject(final Map object, Object fieldName) { final boolean parentIsArray = fieldName != null && fieldName.getClass() == Integer.class; - JSONObject input = new JSONObject(); + JSONObject input = new JSONObject(isEnabled(Feature.OrderedField)); ParseContext ctxLocal = null; if (!parentIsArray) { @@ -991,7 +991,7 @@ public Object parseObject(final Map object) { } public JSONObject parseObject() { - JSONObject object = new JSONObject(); + JSONObject object = new JSONObject(isEnabled(Feature.OrderedField)); parseObject(object); return object; } @@ -1067,7 +1067,7 @@ public final void parseArray(final Collection array, Object fieldName) { lexer.nextToken(JSONToken.COMMA); break; case LBRACE: - JSONObject object = new JSONObject(); + JSONObject object = new JSONObject(isEnabled(Feature.OrderedField)); value = parseObject(object, i); break; case LBRACKET: diff --git a/src/test/java/com/alibaba/json/bvt/bug/Issue248_orderedField.java b/src/test/java/com/alibaba/json/bvt/bug/Issue248_orderedField.java index fb7d9c188d..5b09112daf 100755 --- a/src/test/java/com/alibaba/json/bvt/bug/Issue248_orderedField.java +++ b/src/test/java/com/alibaba/json/bvt/bug/Issue248_orderedField.java @@ -35,4 +35,47 @@ public void test_1() throws Exception { } + public void test_2() throws Exception { + String text = "{\"k1\":\"v1\",\"k3\":\"v3\",\"k2\":\"v2\",\"map\":{\"k1\":\"v1\",\"k3\":\"v3\",\"k2\":\"v2\",\"map\":{\"k1\":\"v1\",\"k3\":\"v3\",\"k2\":\"v2\"}}}"; + System.out.println(JSON.parseObject(text)); + + JSONObject object = JSON.parseObject(text, Feature.OrderedField); + System.out.println(object); + + Assert.assertEquals("k1", object.keySet().toArray()[0]); + Assert.assertEquals("k3", object.keySet().toArray()[1]); + Assert.assertEquals("k2", object.keySet().toArray()[2]); + Assert.assertEquals("map", object.keySet().toArray()[3]); + + Assert.assertEquals("k1", object.getJSONObject("map").keySet().toArray()[0]); + Assert.assertEquals("k3", object.getJSONObject("map").keySet().toArray()[1]); + Assert.assertEquals("k2", object.getJSONObject("map").keySet().toArray()[2]); + Assert.assertEquals("map", object.getJSONObject("map").keySet().toArray()[3]); + + Assert.assertEquals("k1", object.getJSONObject("map").getJSONObject("map").keySet().toArray()[0]); + Assert.assertEquals("k3", object.getJSONObject("map").getJSONObject("map").keySet().toArray()[1]); + Assert.assertEquals("k2", object.getJSONObject("map").getJSONObject("map").keySet().toArray()[2]); + } + + public void test_3() throws Exception { + String text = "{\"k1\":\"v1\",\"k3\":\"v3\",\"k2\":\"v2\",\"list\":[\"v1\",\"v3\",\"v2\",{\"map\":{\"k1\":\"v1\",\"k3\":\"v3\",\"k2\":\"v2\"}}]}"; + System.out.println(JSON.parseObject(text)); + + JSONObject object = JSON.parseObject(text, Feature.OrderedField); + System.out.println(object); + + Assert.assertEquals("k1", object.keySet().toArray()[0]); + Assert.assertEquals("k3", object.keySet().toArray()[1]); + Assert.assertEquals("k2", object.keySet().toArray()[2]); + Assert.assertEquals("list", object.keySet().toArray()[3]); + + Assert.assertEquals("k1", + object.getJSONArray("list").getJSONObject(3).getJSONObject("map").keySet().toArray()[0]); + Assert.assertEquals("k3", + object.getJSONArray("list").getJSONObject(3).getJSONObject("map").keySet().toArray()[1]); + Assert.assertEquals("k2", + object.getJSONArray("list").getJSONObject(3).getJSONObject("map").keySet().toArray()[2]); + + } + }