fangyidong / json-simple

A simple Java toolkit for JSON. You can use json-simple to encode or decode JSON text.
Apache License 2.0
746 stars 338 forks source link

Added way to efficiently navigate through already known JSON schema #149

Closed semenishchev closed 2 years ago

semenishchev commented 2 years ago

With my change all users of the lib would be able to efficiently navigate through JSON in Builder-like methods. Example (from the Test)

ParseResult result = JSONParser.of("{"first": 123, "second": [{"k1":{"id":"id1"}}, 4, 5, 6, {"id": 123}], "third": 789, "id": null}");
assertEquals(result.get("first").asInt(), 123);
assertEquals(result.get("second").get(0).get("k1").get("id").asString(), "id1");
assertEquals(result.get("second").get(4).get("id").asInt(), 123);
assertEquals(result.get("second").get(1).asInt(), 4);
assertTrue(result.get("id").isNull());

Added Accessor interface to represent methods on JSONObject and JSONArray Added 2 static methods in JSONParser to create navigator from String or reader Added tests for them (at the bottom of method testDecode in Test class)

These methods should be only used when user knows what JSON they will be receiving.