private void perfTest (JsonParser p) { try { for (;;) { Event e = p.next (); switch (e) { case KEY_NAME: case VALUE_NUMBER: case VALUE_STRING: p.getString (); break; default: break; } } } catch (NoSuchElementException ex) { } }
@Test public void testBinary () throws IOException { File file = new File ("../tests/data/binary.bson".replace ('/', File.separatorChar)); CookJsonProvider provider = new CookJsonProvider (); HashMap<String, Object> config = new HashMap<String, Object> (); config.put (CookJsonProvider.FORMAT, CookJsonProvider.FORMAT_BSON); config.put (CookJsonProvider.BINARY_FORMAT, CookJsonProvider.BINARY_FORMAT_HEX); JsonReader r = provider.createReaderFactory (config).createReader (new FileInputStream (file)); JsonStructure v = r.read (); r.close (); CookJsonParser p = new JsonStructureParser (v); while (p.hasNext ()) { if (p.next () == Event.VALUE_STRING) { if (p.isBinary ()) { Assert.assertEquals (p.getString (), Hex.encodeHexString (p.getBytes ())); } } } p.close (); }
@Test public void testGetLong () throws IOException { File file = new File ("../tests/data/types.json".replace ('/', File.separatorChar)); CookJsonProvider provider = new CookJsonProvider (); JsonReader r = provider.createReader (new FileInputStream (file)); JsonStructure v = r.read (); r.close (); CookJsonParser p = new JsonStructureParser (v); long[] longs = new long[9]; int count = 0; while (p.hasNext ()) { if (p.next () == Event.VALUE_NUMBER) { Assert.assertTrue (p.getValue () instanceof JsonNumber); longs[count++] = p.getLong (); } } p.close (); Assert.assertEquals (sum (new long[]{ 1234, 12345678901234L, 7888426545362939890L, 12345, 1234, 12345678901234L, 7888426545362939890L, 12345, 1 }), sum (longs)); }
@Test public void testGetBigInteger () throws IOException { File file = new File ("../tests/data/types.json".replace ('/', File.separatorChar)); CookJsonParser p = getJsonParser (file); BigInteger[] bigints = new BigInteger[9]; int count = 0; while (p.hasNext ()) { if (p.next () == Event.VALUE_NUMBER) { Assert.assertTrue (p.getValue () instanceof JsonNumber); bigints[count++] = p.getBigDecimal ().toBigInteger (); } } p.close (); Assert.assertEquals (sum (new BigInteger[]{ new BigInteger ("1234"), new BigInteger ("12345678901234"), new BigInteger ("1234567890123412345678901234"), new BigInteger ("12345"), new BigInteger ("1234"), new BigInteger ("12345678901234"), new BigInteger ("1234567890123412345678901234"), new BigInteger ("12345"), new BigInteger ("1") }), sum (bigints)); }
@Test public void testGetDecimal () throws IOException { File file = new File ("../tests/data/types.json".replace ('/', File.separatorChar)); CookJsonProvider provider = new CookJsonProvider (); JsonReader r = provider.createReader (new FileInputStream (file)); JsonStructure v = r.read (); r.close (); CookJsonParser p = new JsonStructureParser (v); BigDecimal[] decimals = new BigDecimal[9]; int count = 0; while (p.hasNext ()) { if (p.next () == Event.VALUE_NUMBER) { Assert.assertTrue (p.getValue () instanceof JsonNumber); decimals[count++] = p.getBigDecimal (); } } p.close (); Assert.assertEquals (sum (new BigDecimal[]{ new BigDecimal (1234), new BigDecimal (12345678901234L), new BigDecimal ("1234567890123412345678901234"), new BigDecimal (12345.5), new BigDecimal (1234), new BigDecimal (12345678901234L), new BigDecimal ("1234567890123412345678901234"), new BigDecimal (12345.5), new BigDecimal (1) }), sum (decimals)); }
@Test public void testGetInt () throws IOException { File file = new File ("../tests/data/types.json".replace ('/', File.separatorChar)); JsonParser p = getJsonParser (file, 0); int[] ints = new int[9]; int count = 0; while (p.hasNext ()) { if (p.next () == Event.VALUE_NUMBER) { ints[count++] = p.getInt (); } } p.close (); Assert.assertArrayEquals (new int[] { 1234, 1942892530, -115429390, 12345, 1234, 1942892530, -115429390, 12345, 1 }, ints); }
@Test public void testGetInt_2 () throws IOException { File file = new File ("../tests/data/types.json".replace ('/', File.separatorChar)); JsonParser p = getJsonParser (file, 1); int[] ints = new int[9]; int count = 0; while (p.hasNext ()) { if (p.next () == Event.VALUE_NUMBER) { ints[count++] = p.getInt (); } } p.close (); Assert.assertArrayEquals (new int[] { 1234, 1942892530, -115429390, 12345, 1234, 1942892530, -115429390, 12345, 1 }, ints); }
@Test public void testGetLong () throws IOException { File file = new File ("../tests/data/types.json".replace ('/', File.separatorChar)); JsonParser p = getJsonParser (file, 0); long[] longs = new long[9]; int count = 0; while (p.hasNext ()) { if (p.next () == Event.VALUE_NUMBER) { longs[count++] = p.getLong (); } } p.close (); Assert.assertArrayEquals (new long[] { 1234, 12345678901234L, 7888426545362939890L, 12345, 1234, 12345678901234L, 7888426545362939890L, 12345, 1 }, longs); }
@Test public void testGetLong_2 () throws IOException { File file = new File ("../tests/data/types.json".replace ('/', File.separatorChar)); JsonParser p = getJsonParser (file, 1); long[] longs = new long[9]; int count = 0; while (p.hasNext ()) { if (p.next () == Event.VALUE_NUMBER) { longs[count++] = p.getLong (); } } p.close (); Assert.assertArrayEquals (new long[] { 1234, 12345678901234L, 7888426545362939890L, 12345, 1234, 12345678901234L, 7888426545362939890L, 12345, 1 }, longs); }
@Test public void testGetDecimal () throws IOException { File file = new File ("../tests/data/types.json".replace ('/', File.separatorChar)); JsonParser p = getJsonParser (file, 0); BigDecimal[] decimals = new BigDecimal[9]; int count = 0; while (p.hasNext ()) { if (p.next () == Event.VALUE_NUMBER) { decimals[count++] = p.getBigDecimal (); } } p.close (); Assert.assertArrayEquals (new BigDecimal[] { new BigDecimal (1234), new BigDecimal (12345678901234L), new BigDecimal ("1234567890123412345678901234"), new BigDecimal (12345.5), new BigDecimal (1234), new BigDecimal (12345678901234L), new BigDecimal ("1234567890123412345678901234"), new BigDecimal (12345.5), new BigDecimal (1) }, decimals); }
@Test public void testGetInt () throws IOException { File file = new File ("../tests/data/types.json".replace ('/', File.separatorChar)); JsonParser p = getJsonParser (file, 0); int[] ints = new int[9]; int count = 0; while (p.hasNext ()) { if (p.next () == Event.VALUE_NUMBER) { ints[count++] = p.getInt (); } } p.close (); Assert.assertArrayEquals (new int[]{ 1234, 1942892530, -115429390, 12345, 1234, 1942892530, -115429390, 12345, 1 }, ints); }
@Test public void testGetInt_2 () throws IOException { File file = new File ("../tests/data/types.json".replace ('/', File.separatorChar)); JsonParser p = getJsonParser (file, 1); int[] ints = new int[9]; int count = 0; while (p.hasNext ()) { if (p.next () == Event.VALUE_NUMBER) { ints[count++] = p.getInt (); } } p.close (); Assert.assertArrayEquals (new int[]{ 1234, 1942892530, -115429390, 12345, 1234, 1942892530, -115429390, 12345, 1 }, ints); }
@Test public void testGetLong () throws IOException { File file = new File ("../tests/data/types.json".replace ('/', File.separatorChar)); JsonParser p = getJsonParser (file, 0); long[] longs = new long[9]; int count = 0; while (p.hasNext ()) { if (p.next () == Event.VALUE_NUMBER) { longs[count++] = p.getLong (); } } p.close (); Assert.assertArrayEquals (new long[]{ 1234, 12345678901234L, 7888426545362939890L, 12345, 1234, 12345678901234L, 7888426545362939890L, 12345, 1 }, longs); }
@Test public void testGetLong_2 () throws IOException { File file = new File ("../tests/data/types.json".replace ('/', File.separatorChar)); JsonParser p = getJsonParser (file, 1); long[] longs = new long[9]; int count = 0; while (p.hasNext ()) { if (p.next () == Event.VALUE_NUMBER) { longs[count++] = p.getLong (); } } p.close (); Assert.assertArrayEquals (new long[]{ 1234, 12345678901234L, 7888426545362939890L, 12345, 1234, 12345678901234L, 7888426545362939890L, 12345, 1 }, longs); }
@Test public void testGetDecimal () throws IOException { File file = new File ("../tests/data/types.json".replace ('/', File.separatorChar)); JsonParser p = getJsonParser (file, 0); BigDecimal[] decimals = new BigDecimal[9]; int count = 0; while (p.hasNext ()) { if (p.next () == Event.VALUE_NUMBER) { decimals[count++] = p.getBigDecimal (); } } p.close (); Assert.assertArrayEquals (new BigDecimal[]{ new BigDecimal (1234), new BigDecimal (12345678901234L), new BigDecimal ("1234567890123412345678901234"), new BigDecimal (12345.5), new BigDecimal (1234), new BigDecimal (12345678901234L), new BigDecimal ("1234567890123412345678901234"), new BigDecimal (12345.5), new BigDecimal (1) }, decimals); }
@Test public void testGetInt () throws IOException { File file = new File ("../tests/data/types.bson".replace ('/', File.separatorChar)); JsonParser p = new BsonParser (new FileInputStream (file)); int[] ints = new int[9]; int count = 0; while (p.hasNext ()) { if (p.next () == Event.VALUE_NUMBER) { ints[count++] = p.getInt (); } } p.close (); Assert.assertArrayEquals (new int[]{ 1234, 1942892530, 2147483647, 12345, 1234, 1942892530, 2147483647, 12345, 1 }, ints); }
@Test public void testGetLong () throws IOException { File file = new File ("../tests/data/types.bson".replace ('/', File.separatorChar)); JsonParser p = new BsonParser (new FileInputStream (file)); long[] longs = new long[9]; int count = 0; while (p.hasNext ()) { if (p.next () == Event.VALUE_NUMBER) { longs[count++] = p.getLong (); } } p.close (); Assert.assertArrayEquals (new long[]{ 1234, 12345678901234L, 9223372036854775807L, 12345, 1234, 12345678901234L, 9223372036854775807L, 12345, 1 }, longs); }
@Test public void testGetDecimal () throws IOException { File file = new File ("../tests/data/types.bson".replace ('/', File.separatorChar)); JsonParser p = new BsonParser (new FileInputStream (file)); BigDecimal[] decimals = new BigDecimal[9]; int count = 0; while (p.hasNext ()) { if (p.next () == Event.VALUE_NUMBER) { decimals[count++] = p.getBigDecimal (); } } p.close (); BigDecimal d = new BigDecimal (new BigInteger ("1234567890123412345678901234").doubleValue ()); Assert.assertArrayEquals (new BigDecimal[]{ new BigDecimal (1234), new BigDecimal (12345678901234L), d, new BigDecimal (12345.5), new BigDecimal (1234), new BigDecimal (12345678901234L), d, new BigDecimal (12345.5), new BigDecimal (1) }, decimals); }
@Test public void threeLiterals() { final JsonParser parser = Json.createParserFactory(new HashMap<String, Object>() {{ put(JsonParserFactoryImpl.MAX_STRING_LENGTH, 10); }}).createParser(new ByteArrayInputStream("{\"a\":true,\"b\":null,\"c\":false,\"arr\":[false, true, null]}".getBytes())); parser.next(); parser.next(); assertEquals(JsonParser.Event.VALUE_TRUE, parser.next()); parser.next(); assertEquals(JsonParser.Event.VALUE_NULL, parser.next()); parser.next(); assertEquals(JsonParser.Event.VALUE_FALSE, parser.next()); parser.next(); parser.next(); assertEquals(JsonParser.Event.VALUE_FALSE, parser.next()); assertEquals(JsonParser.Event.VALUE_TRUE, parser.next()); assertEquals(JsonParser.Event.VALUE_NULL, parser.next()); parser.close(); }
@Override protected void interpretConstraints(String constraints) { JsonParser parser = Json.createParser(new StringReader(constraints)); while (parser.hasNext()) { Event e = parser.next(); if (e == Event.KEY_NAME) if (parser.getString().equalsIgnoreCase("constraint")) { parser.next(); if (parser.getString().equalsIgnoreCase("AssertTrue")) { setAssertTrue(true); break; } else if (parser.getString().equalsIgnoreCase("AssertFalse")) { setAssertFalse(true); break; } } } }
@Override protected void interpretConstraints(String constraints) { super.interpretConstraints(constraints); JsonParser parser = Json.createParser(new StringReader(constraints)); while (parser.hasNext()) { Event e = parser.next(); if (e == Event.KEY_NAME) if (parser.getString().equalsIgnoreCase("constraint")) { parser.next(); if (parser.getString().equalsIgnoreCase("List")) { parser.next(); pickList = new HashSet<>(); while (parser.hasNext()) { Event item = parser.next(); if (item == Event.VALUE_STRING) pickList.add(parser.getString()); } } } } parser.close(); }
@Override protected void interpretConstraints(String constraints) { JsonParser parser = Json.createParser(new StringReader(constraints)); while (parser.hasNext()) { Event e = parser.next(); if (e == Event.KEY_NAME) if (parser.getString().equalsIgnoreCase("constraint")) { parser.next(); if (parser.getString().equalsIgnoreCase("Pattern")) { parser.next(); parser.next(); setPattern(parser.getString()); } else if (parser.getString().equalsIgnoreCase("Valid")) { setValid(true); } else if (parser.getString().equalsIgnoreCase("Past")) { setPast(true); } else if (parser.getString().equalsIgnoreCase("Future")) { setFuture(true); } } } parser.close(); }
public String parseJson() { StringReader stringReader = new StringReader(jsonStr); JsonParser jsonParser = Json.createParser(stringReader); Map<String, Object> jsonMap = new HashMap<>(); String jsonKeyNm = null; Object jsonVal = null; while (jsonParser.hasNext()) { JsonParser.Event event = jsonParser.next(); if (event.equals(Event.KEY_NAME)) { jsonKeyNm = jsonParser.getString(); } else if (event.equals(Event.VALUE_STRING)) { jsonVal = jsonParser.getString(); } else if (event.equals(Event.VALUE_NUMBER)) { jsonVal = jsonParser.getInt(); } jsonMap.put(jsonKeyNm, jsonVal); } person.setFirstName((String) jsonMap.get("firstName")); person.setMiddleName((String) jsonMap.get("middleName")); person.setLastName((String) jsonMap.get("lastName")); person.setGender((String) jsonMap.get("gender")); person.setAge((Integer) jsonMap.get("age")); return "display_populated_obj"; }
@Override public JsonStructure read () { if (m_read) throw new IllegalStateException (); m_read = true; if (!m_p.hasNext ()) throw new IllegalStateException (); Event e = m_p.next (); if (e != Event.START_ARRAY && e != Event.START_OBJECT) throw new IllegalStateException (); return (JsonStructure)Utils.getStructure (m_p); }
@Override public JsonObject readObject () { if (m_read) throw new IllegalStateException (); m_read = true; if (!m_p.hasNext ()) throw new IllegalStateException (); Event e = m_p.next (); if (e != Event.START_OBJECT) throw new IllegalStateException (); return (JsonObject)Utils.getStructure (m_p); }
@Override public JsonArray readArray () { if (m_read) throw new IllegalStateException (); m_read = true; if (!m_p.hasNext ()) throw new IllegalStateException (); Event e = m_p.next (); if (e != Event.START_ARRAY && e != Event.START_OBJECT) throw new IllegalStateException (); return (JsonArray)Utils.getStructure (m_p); }
@Test public void testGetInt () throws IOException { File file = new File ("../tests/data/types.json".replace ('/', File.separatorChar)); CookJsonProvider provider = new CookJsonProvider (); JsonReader r = provider.createReader (new FileInputStream (file)); JsonStructure v = r.read (); r.close (); CookJsonParser p = new JsonStructureParser (v); int[] ints = new int[9]; int count = 0; while (p.hasNext ()) { if (p.next () == Event.VALUE_NUMBER) { Assert.assertEquals (Event.VALUE_NUMBER, p.getEvent ()); Assert.assertTrue (p.getValue () instanceof JsonNumber); Assert.assertEquals (p.getString (), ((JsonNumber)p.getValue ()).toString ()); ints[count++] = p.getInt (); Assert.assertEquals (JsonLocationImpl.Unknown, p.getLocation ()); } } p.close (); Assert.assertEquals (sum (new int[]{ 1234, 1942892530, -115429390, 12345, 1234, 1942892530, -115429390, 12345, 1 }), sum (ints)); }
private void eventCheck (String fileName, int expectedCount, boolean rootAsArray) throws IOException { File file = new File (fileName.replace ('/', File.separatorChar)); BsonParser p = new BsonParser (new FileInputStream (file)); p.setRootAsArray (rootAsArray); int count = 0; try { for (;;) { Event e = p.next (); // Debug.debug ("READ: " + e); switch (e) { case KEY_NAME: { ++count; break; } default: ++count; } } } catch (NoSuchElementException ex) { // expected end. } p.close (); Assert.assertEquals (expectedCount, count); }
@Test public void testLargeCstring () throws IOException { int length = 0; BsonParser p = new BsonParser (new FileInputStream ("../tests/data/largecstring.bson".replace ('/', File.separatorChar))); while (p.hasNext ()) { Event e = p.next (); if (e == Event.KEY_NAME) length = p.getString ().length (); } p.close (); Assert.assertEquals (8554, length); }
@Test public void testBinaryFormatBase64 () throws IOException { File bsonFile = testFolder.newFile (); BsonGenerator g = new BsonGenerator (new FileOutputStream (bsonFile)); g.writeStartArray (); g.write (new byte[] { (byte) 0xde, (byte) 0xad, (byte) 0xbe, (byte) 0xef }); g.writeEnd (); g.close (); CookJsonProvider provider = new CookJsonProvider (); HashMap<String, Object> config = new HashMap<String, Object> (); config.put (CookJsonProvider.FORMAT, CookJsonProvider.FORMAT_BSON); config.put (CookJsonProvider.BINARY_FORMAT, CookJsonProvider.BINARY_FORMAT_BASE64); BsonParser p = (BsonParser) provider.createParserFactory (config).createParser (new FileInputStream (bsonFile)); Assert.assertEquals (BinaryFormat.BINARY_FORMAT_BASE64, p.getBinaryFormat ()); String str = null; while (p.hasNext ()) { if (p.next () == Event.VALUE_STRING) { str = p.getString (); } } p.close (); // now verify that our original data written was correct. Assert.assertEquals ("3q2+7w==", str); }
@Test public void testBinaryFormatHex () throws IOException { File bsonFile = testFolder.newFile (); BsonGenerator g = new BsonGenerator (new FileOutputStream (bsonFile)); g.writeStartArray (); g.write (new byte[] { (byte) 0xde, (byte) 0xad, (byte) 0xbe, (byte) 0xef }); g.writeEnd (); g.close (); CookJsonProvider provider = new CookJsonProvider (); HashMap<String, Object> config = new HashMap<String, Object> (); config.put (CookJsonProvider.FORMAT, CookJsonProvider.FORMAT_BSON); config.put (CookJsonProvider.BINARY_FORMAT, CookJsonProvider.BINARY_FORMAT_HEX); BsonParser p = (BsonParser) provider.createParserFactory (config).createParser (new FileInputStream (bsonFile)); Assert.assertEquals (BinaryFormat.BINARY_FORMAT_HEX, p.getBinaryFormat ()); String str = null; while (p.hasNext ()) { if (p.next () == Event.VALUE_STRING) { str = p.getString (); } } p.close (); // now verify that our original data written was correct. Assert.assertEquals ("deadbeef", str); }
@Test(expected=IllegalStateException.class) public void isIntegralThrowsISE() { final JsonParser parser = Json.createParser(Thread.currentThread().getContextClassLoader().getResourceAsStream("json/bigdecimal.json")); assertNotNull(parser); assertTrue(parser.hasNext()); final JsonParser.Event event = parser.next(); assertNotNull(event); assertEquals(JsonParser.Event.START_OBJECT, event); assertFalse(parser.isIntegralNumber()); }
@Test public void hasNext() { final JsonParser parser = Json.createParserFactory(new HashMap<String, Object>() {{ put(JsonParserFactoryImpl.MAX_STRING_LENGTH, 10); }}).createParser(new ByteArrayInputStream("{}".getBytes())); assertTrue(parser.hasNext()); assertTrue(parser.hasNext()); assertTrue(parser.hasNext()); assertTrue(parser.hasNext()); assertEquals(JsonParser.Event.START_OBJECT, parser.next()); parser.close(); }
@Test public void testEmptyArray() { JsonParser parser = Json.createParser(new ByteArrayInputStream("[]".getBytes())); assertEquals(Event.START_ARRAY, parser.next()); assertEquals(Event.END_ARRAY, parser.next()); assertEquals(false, parser.hasNext()); try { parser.next(); fail("Should have thrown a NoSuchElementException"); } catch (NoSuchElementException ne) { //expected } }
@Test public void stringescapeVariousBufferSizesBogus() { StringBuilder sb = new StringBuilder(); sb.append("\t\"special-\":" + "\"" + "\\\\f\\n\\r\\t\\u6565\uDC00\uD800" + "\",\n"); sb.append("\t\"unicode-\\u0000- \":\"\\u5656\uDC00\uD800\"\n"); String s = "{"+sb.toString()+"}"; for (int i = 1; i < s.length()+100; i++) { final String value = String.valueOf(i); final JsonParser parser = Json.createParserFactory(new HashMap<String, Object>() { { put("org.apache.johnzon.default-char-buffer", value); } }).createParser(new ByteArrayInputStream(s.getBytes())); assertNotNull(parser); while(parser.hasNext()) { Event e = parser.next(); if(e==null) { fail(); } } assertTrue(!parser.hasNext()); parser.close(); } }
@Test(expected = JsonParsingException.class) // TODO read key and : in once public void invalidArrayMissingSeparator() { final JsonParser parser = Json.createParser(new StringReader("{\"a\":5, \"a\"[1,2,3,4,5,[2,2,3,4,5]], \"z\":8}")); assertNotNull(parser); assertTrue(parser.next() == Event.START_OBJECT); assertTrue(parser.next() == Event.KEY_NAME); assertTrue(parser.next() == Event.VALUE_NUMBER); assertTrue(parser.next() == Event.KEY_NAME); parser.next(); }
@Test(expected = JsonParsingException.class) public void invalidArray() { final JsonParser parser = Json.createParser(new StringReader("{\"a\":5, [1,2,3,4,5,[2,2,3,4,5]], \"z\":8}")); assertNotNull(parser); assertTrue(parser.next() == Event.START_OBJECT); assertTrue(parser.next() == Event.KEY_NAME); assertTrue(parser.next() == Event.VALUE_NUMBER); parser.next(); }
@Test(expected = JsonParsingException.class) public void invalidEmptyObject() { final JsonParser parser = Json.createParser(new StringReader("{\"a\":5, {}, \"z\":8}")); assertNotNull(parser); assertTrue(parser.next() == Event.START_OBJECT); assertTrue(parser.next() == Event.KEY_NAME); assertTrue(parser.next() == Event.VALUE_NUMBER); parser.next(); }
@Test(expected = JsonParsingException.class) public void invalidObject() { final JsonParser parser = Json.createParser(new StringReader("{\"a\":5, {\"w\":1}, \"z\":8}")); assertNotNull(parser); assertTrue(parser.next() == Event.START_OBJECT); assertTrue(parser.next() == Event.KEY_NAME); assertTrue(parser.next() == Event.VALUE_NUMBER); parser.next(); }
@Test(expected = JsonParsingException.class) public void invalidLiteral() { final JsonParser parser = Json.createParser(new StringReader("{\"a\":5, true, \"z\":8}")); assertNotNull(parser); assertTrue(parser.next() == Event.START_OBJECT); assertTrue(parser.next() == Event.KEY_NAME); assertTrue(parser.next() == Event.VALUE_NUMBER); parser.next(); }