/** Unit test to check for regression of [JACKSON-18]. */ public void testSmallNumbers() throws Exception { ObjectMapper mapper = new ObjectMapper(); ArrayNode root = mapper.createArrayNode(); for (int i = -20; i <= 20; ++i) { JsonNode n = root.numberNode(i); root.add(n); // Hmmh. Not sure why toString() won't be triggered otherwise... assertEquals(String.valueOf(i), n.toString()); } // Loop over 2 different serialization methods for (int type = 0; type < 2; ++type) { StringWriter sw = new StringWriter(); if (type == 0) { JsonGenerator gen = new JsonFactory().createGenerator(sw); root.serialize(gen, null); gen.close(); } else { mapper.writeValue(sw, root); } String doc = sw.toString(); JsonParser p = new JsonFactory().createParser(new StringReader(doc)); assertEquals(JsonToken.START_ARRAY, p.nextToken()); for (int i = -20; i <= 20; ++i) { assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextToken()); assertEquals(i, p.getIntValue()); assertEquals("" + i, p.getText()); } assertEquals(JsonToken.END_ARRAY, p.nextToken()); p.close(); } }
// And then one more test just for Bytes-based symbol table public void testByteBasedSymbolTable() throws Exception { // combination of short, medium1/2, long names... final String JSON = aposToQuotes( "{'abc':1, 'abc\\u0000':2, '\\u0000abc':3, " // then some medium + "'abc123':4,'abcd1234':5," + "'abcd1234a':6,'abcd1234abcd':7," + "'abcd1234abcd1':8" + "}"); JsonFactory f = new JsonFactory(); JsonParser p = f.createParser(JSON.getBytes("UTF-8")); ByteQuadsCanonicalizer symbols = _findSymbols(p); assertEquals(0, symbols.size()); _streamThrough(p); assertEquals(8, symbols.size()); p.close(); // and, for fun, try again p = f.createParser(JSON.getBytes("UTF-8")); _streamThrough(p); symbols = _findSymbols(p); assertEquals(8, symbols.size()); p.close(); p = f.createParser(JSON.getBytes("UTF-8")); _streamThrough(p); symbols = _findSymbols(p); assertEquals(8, symbols.size()); p.close(); }
public void testEmptyArrayWrite() throws Exception { StringWriter sw = new StringWriter(); JsonGenerator gen = new JsonFactory().createGenerator(sw); JsonStreamContext ctxt = gen.getOutputContext(); assertTrue(ctxt.inRoot()); assertFalse(ctxt.inArray()); assertFalse(ctxt.inObject()); assertEquals(0, ctxt.getEntryCount()); assertEquals(0, ctxt.getCurrentIndex()); gen.writeStartArray(); ctxt = gen.getOutputContext(); assertFalse(ctxt.inRoot()); assertTrue(ctxt.inArray()); assertFalse(ctxt.inObject()); assertEquals(0, ctxt.getEntryCount()); assertEquals(0, ctxt.getCurrentIndex()); gen.writeEndArray(); ctxt = gen.getOutputContext(); assertTrue("Should be in root, was " + ctxt.getTypeDesc(), ctxt.inRoot()); assertFalse(ctxt.inArray()); assertFalse(ctxt.inObject()); assertEquals(1, ctxt.getEntryCount()); // Index won't yet move assertEquals(0, ctxt.getCurrentIndex()); gen.close(); String docStr = sw.toString(); JsonParser jp = createParserUsingReader(docStr); assertEquals(JsonToken.START_ARRAY, jp.nextToken()); assertEquals(JsonToken.END_ARRAY, jp.nextToken()); jp.close(); // Ok, then array with nested empty array sw = new StringWriter(); gen = new JsonFactory().createGenerator(sw); gen.writeStartArray(); gen.writeStartArray(); gen.writeEndArray(); gen.writeEndArray(); gen.close(); docStr = sw.toString(); jp = createParserUsingReader(docStr); assertEquals(JsonToken.START_ARRAY, jp.nextToken()); assertEquals(JsonToken.START_ARRAY, jp.nextToken()); assertEquals(JsonToken.END_ARRAY, jp.nextToken()); assertEquals(JsonToken.END_ARRAY, jp.nextToken()); assertEquals(null, jp.nextToken()); jp.close(); }
// [core#191]: similarly, but for "short" symbols: public void testShortNameCollisionsViaParser() throws Exception { JsonFactory f = new JsonFactory(); String json = _shortDoc191(); JsonParser p; // First: ensure that char-based is fine p = f.createParser(json); while (p.nextToken() != null) {} p.close(); // and then that byte-based p = f.createParser(json.getBytes("UTF-8")); while (p.nextToken() != null) {} p.close(); }
@Override public final MediaContent deserialize(byte[] array) throws IOException { JsonParser parser = constructParser(array); MediaContent mc = readMediaContent(parser); parser.close(); return mc; }
public void testUtf8Issue462() throws Exception { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); IOContext ioc = new IOContext(new BufferRecycler(), bytes, true); JsonGenerator gen = new UTF8JsonGenerator(ioc, 0, null, bytes); String str = "Natuurlijk is alles gelukt en weer een tevreden klant\uD83D\uDE04"; int length = 4000 - 38; for (int i = 1; i <= length; ++i) { gen.writeNumber(1); } gen.writeString(str); gen.flush(); gen.close(); // Also verify it's parsable? JsonFactory f = new JsonFactory(); JsonParser p = f.createParser(bytes.toByteArray()); for (int i = 1; i <= length; ++i) { assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); assertEquals(1, p.getIntValue()); } assertToken(JsonToken.VALUE_STRING, p.nextToken()); assertEquals(str, p.getText()); assertNull(p.nextToken()); p.close(); }
private TwitterEntry read(JsonParser jp) throws IOException { // First: verify that we got "Json Object": if (jp.nextToken() != JsonToken.START_OBJECT) { throw new IOException("Expected data to start with an Object"); } TwitterEntry result = new TwitterEntry(); // Iterate over object fields: while (jp.nextToken() != JsonToken.END_OBJECT) { String fieldName = jp.getCurrentName(); // Let's move to value jp.nextToken(); if (fieldName.equals("id")) { result.setId(jp.getLongValue()); } else if (fieldName.equals("text")) { result.setText(jp.getText()); } else if (fieldName.equals("fromUserId")) { result.setFromUserId(jp.getIntValue()); } else if (fieldName.equals("toUserId")) { result.setToUserId(jp.getIntValue()); } else if (fieldName.equals("languageCode")) { result.setLanguageCode(jp.getText()); } else { // ignore, or signal error throw new IOException("Unrecognized field '" + fieldName + "'"); } } jp.close(); // important to close both parser and underlying File reader return result; }
private void verifyFromMap(String input) throws Exception { JsonParser p = new JsonFactory().createParser(input); assertEquals(JsonToken.START_OBJECT, p.nextToken()); assertEquals(JsonToken.FIELD_NAME, p.nextToken()); assertEquals(FIELD4, getAndVerifyText(p)); assertEquals(JsonToken.VALUE_STRING, p.nextToken()); assertEquals(TEXT2, getAndVerifyText(p)); assertEquals(JsonToken.FIELD_NAME, p.nextToken()); assertEquals(FIELD3, getAndVerifyText(p)); assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextToken()); assertEquals(-1, p.getIntValue()); assertEquals(JsonToken.FIELD_NAME, p.nextToken()); assertEquals(FIELD2, getAndVerifyText(p)); assertEquals(JsonToken.START_ARRAY, p.nextToken()); assertEquals(JsonToken.END_ARRAY, p.nextToken()); assertEquals(JsonToken.FIELD_NAME, p.nextToken()); assertEquals(FIELD1, getAndVerifyText(p)); assertEquals(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken()); assertEquals(DOUBLE_VALUE, p.getDoubleValue(), 0); assertEquals(JsonToken.END_OBJECT, p.nextToken()); assertNull(p.nextToken()); p.close(); }
private void verifyFromArray(String input) throws Exception { JsonParser p = new JsonFactory().createParser(new StringReader(input)); assertEquals(JsonToken.START_ARRAY, p.nextToken()); assertEquals(JsonToken.VALUE_STRING, p.nextToken()); assertEquals(TEXT1, getAndVerifyText(p)); assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextToken()); assertEquals(3, p.getIntValue()); assertEquals(JsonToken.START_OBJECT, p.nextToken()); assertEquals(JsonToken.FIELD_NAME, p.nextToken()); assertEquals(FIELD1, getAndVerifyText(p)); assertEquals(JsonToken.VALUE_TRUE, p.nextToken()); assertEquals(JsonToken.FIELD_NAME, p.nextToken()); assertEquals(FIELD2, getAndVerifyText(p)); assertEquals(JsonToken.START_ARRAY, p.nextToken()); assertEquals(JsonToken.END_ARRAY, p.nextToken()); assertEquals(JsonToken.END_OBJECT, p.nextToken()); assertEquals(JsonToken.VALUE_FALSE, p.nextToken()); assertEquals(JsonToken.END_ARRAY, p.nextToken()); assertNull(p.nextToken()); p.close(); }
@Override public MediaContent[] deserializeItems(InputStream in, int numberOfItems) throws IOException { MediaContent[] result = new MediaContent[numberOfItems]; JsonParser parser = constructParser(in); for (int i = 0; i < numberOfItems; ++i) { result[i] = readMediaContent(parser); } parser.close(); return result; }
private void initializeParser(JsonParser paramJsonParser) throws IOException { boolean bool = true; if (wrapperKeys.isEmpty()) ; while (true) { return; try { if ((paramJsonParser.skipToKey(wrapperKeys) != null) && (paramJsonParser.getCurrentToken() != JsonToken.END_OBJECT)) ; while (true) { Preconditions.checkArgument( bool, "wrapper key(s) not found: %s", new Object[] {wrapperKeys}); if (0 == 0) break; paramJsonParser.close(); return; bool = false; } } finally { if (1 != 0) paramJsonParser.close(); } } }
protected JsonNode _bindAndCloseAsTree(JsonParser jp) throws IOException, JsonParseException, JsonMappingException { if (_schema != null) { jp.setSchema(_schema); } try { return _bindAsTree(jp); } finally { try { jp.close(); } catch (IOException ioe) { } } }
public void testBinary() throws Exception { ObjectMapper mapper = new ObjectMapper(); final int LENGTH = 13045; byte[] data = new byte[LENGTH]; for (int i = 0; i < LENGTH; ++i) { data[i] = (byte) i; } StringWriter sw = new StringWriter(); mapper.writeValue(sw, BinaryNode.valueOf(data)); JsonParser p = new JsonFactory().createParser(sw.toString()); // note: can't determine it's binary from json alone: assertToken(JsonToken.VALUE_STRING, p.nextToken()); assertArrayEquals(data, p.getBinaryValue()); p.close(); }
protected final double _testRawDeser(int reps, byte[] json, ObjectReader reader) throws IOException { long start = System.nanoTime(); final JsonFactory f = reader.getFactory(); while (--reps >= 0) { JsonParser p = f.createParser(new ByteArrayInputStream(json)); JsonToken t; while ((t = p.nextToken()) != null) { if (t == JsonToken.VALUE_STRING) { p.getText(); } else if (t.isNumeric()) { p.getNumberValue(); } ; } p.close(); } hash = f.hashCode(); return _msecsFromNanos(System.nanoTime() - start); }
public void testSimpleArrayWrite() throws Exception { StringWriter sw = new StringWriter(); JsonGenerator gen = new JsonFactory().createGenerator(sw); gen.writeStartArray(); gen.writeNumber(13); gen.writeBoolean(true); gen.writeString("foobar"); gen.writeEndArray(); gen.close(); String docStr = sw.toString(); JsonParser jp = createParserUsingReader(docStr); assertEquals(JsonToken.START_ARRAY, jp.nextToken()); assertEquals(JsonToken.VALUE_NUMBER_INT, jp.nextToken()); assertEquals(13, jp.getIntValue()); assertEquals(JsonToken.VALUE_TRUE, jp.nextToken()); assertEquals(JsonToken.VALUE_STRING, jp.nextToken()); assertEquals("foobar", jp.getText()); assertEquals(JsonToken.END_ARRAY, jp.nextToken()); assertEquals(null, jp.nextToken()); jp.close(); }
protected Object _bindAndClose(JsonParser jp) throws IOException, JsonParseException, JsonMappingException { if (_schema != null) { jp.setSchema(_schema); } try { Object result; JsonToken t = _initForReading(jp); if (t == JsonToken.VALUE_NULL) { if (_valueToUpdate == null) { result = _findRootDeserializer(_config, _valueType).getNullValue(); } else { result = _valueToUpdate; } } else if (t == JsonToken.END_ARRAY || t == JsonToken.END_OBJECT) { result = _valueToUpdate; } else { DeserializationContext ctxt = _createDeserializationContext(jp, _config); JsonDeserializer<Object> deser = _findRootDeserializer(_config, _valueType); if (_unwrapRoot) { result = _unwrapAndDeserialize(jp, ctxt, _valueType, deser); } else { if (_valueToUpdate == null) { result = deser.deserialize(jp, ctxt); } else { deser.deserialize(jp, ctxt, _valueToUpdate); result = _valueToUpdate; } } } return result; } finally { try { jp.close(); } catch (IOException ioe) { } } }