Skip to content

Commit

Permalink
fix issue alibaba#255
Browse files Browse the repository at this point in the history
  • Loading branch information
Yako authored and Yako committed Jan 14, 2015
1 parent 412144b commit f657c38
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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:
Expand Down
43 changes: 43 additions & 0 deletions src/test/java/com/alibaba/json/bvt/bug/Issue248_orderedField.java
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

}

}

0 comments on commit f657c38

Please sign in to comment.