/** Read any JSON value, not just an object, from a file */ public static JsonValue readAny(String filename) { try { try (InputStream in = IO.openFileEx(filename)) { return JSON.parseAny(in) ; } } catch (FileNotFoundException ex) { throw new RuntimeException("File not found: " + filename, ex) ; } catch (IOException ex) { IO.exception("IOException: " + filename, ex) ; return null ; } }
/** Read any JSON value, not just an object, from a file */ public static JsonValue readAny(String filename) { try { InputStream in = IO.openFileEx(filename) ; try { return JSON.parseAny(in) ; } finally { in.close() ; } } catch (FileNotFoundException ex) { throw new RuntimeException("File not found: "+filename, ex) ; } catch (IOException ex) { IO.exception("IOException: "+filename, ex); return null ; } }
/** Round trip string->json->string->json, compare two JOSN steps */ public static void read(String string) { JsonValue v = JSON.parseAny(string) ; writeRead(v) ; }
/** Read-compare */ public static void read(String string, JsonValue expected) { JsonValue v = JSON.parseAny(string) ; assertEquals(expected, v) ; }
/** Round trip json->string->json */ public static void writeRead(JsonValue v) { String str2 = v.toString(); JsonValue v2 = JSON.parseAny(str2) ; assertEquals(v, v2) ; }
@Test public void jsonAPI_03() { JsonValue jv1 = JSON.parseAny("2") ; JsonValue jv2 = JSON.copy(jv1); assertSame(jv1, jv2); } }
@Test public void jsonBuildObject_02() { JsonValue x = JSON.parseAny("{ key1: 'value1', key2: 'value2' }") ; JsonObject obj = JsonBuilder.buildObject(b->{ b.pair("key1", "value1") .pair("key2", "value2"); }); assertEquals(x, obj); } }
@Override public JsonArray execJson() { checkNotClosed(); HttpQuery httpQuery = makeHttpQuery(); httpQuery.setAccept(WebContent.contentTypeJSON); JsonArray result = new JsonArray(); try(InputStream in = httpQuery.exec()) { JsonValue v = JSON.parseAny(in); if ( ! v.isArray() ) throw new QueryExecException("Return from a JSON query isn't an array"); result = v.getAsArray(); } catch (IOException e) { IO.exception(e); } finally { this.close(); } return result; }
@Test public void jsonBuild03() { JsonValue x = JSON.parseAny("[ ]") ; JsonBuilder builder = new JsonBuilder() ; builder.startArray() ; builder.finishArray() ; JsonValue v = builder.build() ; assertEquals(x,v) ; }
private void test(String queryString, String jsonExpected) { Query query = QueryFactory.create(queryString, Syntax.syntaxARQ); DatasetGraph dsg = new DatasetGraphZero(); Dataset ds = DatasetFactory.wrap(dsg); try ( QueryExecution qExec = QueryExecutionFactory.create(query, ds) ) { JsonValue jvGot = qExec.execJson() ; JsonValue jvExpected = JSON.parseAny(jsonExpected) ; assertEquals(jvExpected, jvGot); } } }
@Test public void jsonBuild01() { JsonValue x = JSON.parseAny("{ }") ; JsonBuilder builder = new JsonBuilder() ; builder.startObject("A") ; builder.finishObject("A") ; JsonValue v = builder.build() ; assertEquals(x,v) ; }
@Test public void jsonBuild06() { JsonValue x = JSON.parseAny("{ a: 'B'}") ; JsonBuilder builder = new JsonBuilder() ; builder.startObject().pair("a", "B").finishObject() ; JsonValue v = builder.build() ; assertEquals(x,v) ; }
@Test public void jsonBuild07() { JsonValue x = JSON.parseAny("{ a: 123}") ; JsonBuilder builder = new JsonBuilder() ; builder.startObject().pair("a", 123).finishObject() ; JsonValue v = builder.build() ; assertEquals(x,v) ; }
@Test public void jsonBuild08() { JsonValue x = JSON.parseAny("{ a: true}") ; JsonBuilder builder = new JsonBuilder() ; JsonValue jv = new JsonBoolean(true); builder.startObject().pair("a", jv).finishObject() ; JsonValue v = builder.build() ; assertEquals(x,v) ; }
@Test public void jsonBuild02() { JsonValue x = JSON.parseAny("{ a: 'A', b:'B'}") ; JsonBuilder builder = new JsonBuilder() ; builder.startObject("Obj1") ; builder.key("a").value("A") ; builder.key("b").value("B") ; builder.finishObject("Obj1") ; JsonValue v = builder.build() ; assertEquals(x,v) ; }
@Test public void jsonBuild05() { JsonValue x = JSON.parseAny("[ { a: 'B'} , 56]") ; JsonBuilder builder = new JsonBuilder() ; builder.startArray() ; builder.startObject().key("a").value("B").finishObject() ; builder.value(56) ; builder.finishArray() ; JsonValue v = builder.build() ; assertEquals(x,v) ; }
@Test public void jsonBuild04() { JsonValue x = JSON.parseAny("{ a: [1], b:'B'}") ; JsonBuilder builder = new JsonBuilder() ; builder.startObject() ; builder.key("a").startArray().value(1).finishArray() ; builder.key("b").value("B") ; builder.finishObject() ; JsonValue v = builder.build() ; assertEquals(x,v) ; }