/** * Method that fetches next token (as if calling {@link #nextToken}) and * verifies whether it is {@link JsonToken#FIELD_NAME}; if it is, * returns same as {@link #getCurrentName()}, otherwise null. * * @since 2.5 */ public String nextFieldName() throws IOException { return (nextToken() == JsonToken.FIELD_NAME) ? getCurrentName() : null; }
/** * Method that fetches next token (as if calling {@link #nextToken}) and * if it is {@link JsonToken#VALUE_STRING} returns contained String value; * otherwise returns null. * It is functionally equivalent to: *<pre> * return (nextToken() == JsonToken.VALUE_STRING) ? getText() : null; *</pre> * but may be faster for parser to process, and can therefore be used if caller * expects to get a String value next from input. */ public String nextTextValue() throws IOException { return (nextToken() == JsonToken.VALUE_STRING) ? getText() : null; }
@Override public String readStringUTF() throws IOException { if ( unknownFallbackReadFieldName != null ) { String unkReadAhead = unknownFallbackReadFieldName; unknownFallbackReadFieldName = null; return unkReadAhead; } JsonToken jsonToken = input.nextToken(); if ( jsonToken == JsonToken.VALUE_NULL ) return null; if ( jsonToken == JsonToken.FIELD_NAME ) return input.getCurrentName(); return input.getText(); }
protected final Object deserializeWithView(JsonParser p, DeserializationContext ctxt, Object bean, Class<?> activeView) throws IOException { JsonToken t = p.getCurrentToken(); for (; t == JsonToken.FIELD_NAME; t = p.nextToken()) { String propName = p.getCurrentName(); // Skip field name: p.nextToken(); SettableBeanProperty prop = _beanProperties.find(propName); if (prop != null) { if (!prop.visibleInView(activeView)) { p.skipChildren(); continue; } try { bean = prop.deserializeSetAndReturn(p, ctxt, bean); } catch (Exception e) { wrapAndThrow(e, bean, propName, ctxt); } continue; } handleUnknownVanilla(p, ctxt, bean, propName); } return bean; }
/** * Parse a map of objects from a JsonParser. * * @param jsonParser The JsonParser, preconfigured to be at the START_ARRAY token. */ public Map<String, T> parseMap(JsonParser jsonParser) throws IOException { HashMap<String, T> map = new HashMap<String, T>(); while (jsonParser.nextToken() != JsonToken.END_OBJECT) { String key = jsonParser.getText(); jsonParser.nextToken(); if (jsonParser.getCurrentToken() == JsonToken.VALUE_NULL) { map.put(key, null); } else{ map.put(key, parse(jsonParser)); } } return map; }
/** * Helper method that may be used to support fallback for Empty String / Empty Array * non-standard representations; usually for things serialized as JSON Objects. * * @since 2.5 */ @SuppressWarnings("unchecked") protected T _deserializeFromEmpty(JsonParser p, DeserializationContext ctxt) throws IOException { JsonToken t = p.getCurrentToken(); if (t == JsonToken.START_ARRAY) { if (ctxt.isEnabled(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT)) { t = p.nextToken(); if (t == JsonToken.END_ARRAY) { return null; } return (T) ctxt.handleUnexpectedToken(handledType(), p); } } else if (t == JsonToken.VALUE_STRING) { if (ctxt.isEnabled(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)) { String str = p.getText().trim(); if (str.isEmpty()) { return null; } } } return (T) ctxt.handleUnexpectedToken(handledType(), p); }
protected Object mapObject(JsonParser p, DeserializationContext ctxt, Map<Object,Object> m) throws IOException { JsonToken t = p.getCurrentToken(); if (t == JsonToken.START_OBJECT) { t = p.nextToken(); } if (t == JsonToken.END_OBJECT) { return m; } // NOTE: we are guaranteed to point to FIELD_NAME String key = p.getCurrentName(); do { p.nextToken(); // and possibly recursive merge here Object old = m.get(key); Object newV; if (old != null) { newV = deserialize(p, ctxt, old); } else { newV = deserialize(p, ctxt); } if (newV != old) { m.put(key, newV); } } while ((key = p.nextFieldName()) != null); return m; }
@Override public List<Object> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { List<Object> result = new ArrayList<>(); JsonDeserializer<Object> deser = ctxt.findRootValueDeserializer(contentType); Object object; // links is an object, so we parse till we find its end. while (!JsonToken.END_OBJECT.equals(jp.nextToken())) { if (!JsonToken.FIELD_NAME.equals(jp.getCurrentToken())) { throw new JsonParseException(jp, "Expected relation name"); } if (JsonToken.START_ARRAY.equals(jp.nextToken())) { while (!JsonToken.END_ARRAY.equals(jp.nextToken())) { object = deser.deserialize(jp, ctxt); result.add(object); } } else { object = deser.deserialize(jp, ctxt); result.add(object); } } return result; }
case JsonTokenId.ID_START_OBJECT: JsonToken t = p.nextToken(); if (t == JsonToken.END_OBJECT) { return new LinkedHashMap<String,Object>(2); case JsonTokenId.ID_START_ARRAY: JsonToken t = p.nextToken(); if (t == JsonToken.END_ARRAY) { // and empty one too if (ctxt.isEnabled(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY)) { return NO_OBJECTS; if (ctxt.isEnabled(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY)) { return mapArrayToArray(p, ctxt); return p.getEmbeddedObject(); case JsonTokenId.ID_STRING: return p.getText(); if (ctxt.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) { return p.getDecimalValue();
protected static void expectField(String name, JsonParser p) throws IOException, JsonParseException { if (p.getCurrentToken() != JsonToken.FIELD_NAME) { throw new JsonParseException(p, "expected field name, but was: " + p.getCurrentToken()); } if (!name.equals(p.getCurrentName())) { throw new JsonParseException(p, "expected field '" + name + "', but was: '" + p.getCurrentName() + "'"); } p.nextToken(); }
@Override public T deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { // Ok: must point to START_OBJECT or FIELD_NAME JsonToken t = jp.getCurrentToken(); if (t == JsonToken.START_OBJECT) { // If START_OBJECT, move to next; may also be END_OBJECT t = jp.nextToken(); } if (t != JsonToken.FIELD_NAME && t != JsonToken.END_OBJECT) { throw ctxt.mappingException(_mapType.getRawClass()); } return _deserializeEntries(jp, ctxt); }
protected JsonToken switchAndReturnNext() throws IOException { while (_nextParserIndex < _parsers.length) { delegate = _parsers[_nextParserIndex++]; if (_checkForExistingToken && delegate.hasCurrentToken()) { return delegate.getCurrentToken(); } JsonToken t = delegate.nextToken(); if (t != null) { return t; } } return null; } }
throw new JsonParseException(jsonParser, "Unexpected end of value"); if (jsonParser.getCurrentToken() == START_ARRAY) { long length = 0; while (true) { JsonToken token = jsonParser.nextToken(); if (token == null) { return null; return length; jsonParser.skipChildren(); if (jsonParser.getCurrentToken() == START_OBJECT) { long length = 0; while (true) { JsonToken token = jsonParser.nextToken(); if (token == null) { return null; jsonParser.skipChildren();
@Override public Applications deserialize(JsonParser jp, DeserializationContext context) throws IOException { if (Thread.currentThread().isInterrupted()) { throw new JsonParseException(jp, "processing aborted"); while((jsonToken = jp.nextToken()) != JsonToken.END_OBJECT){ String fieldName = jp.getCurrentName(); jsonToken = jp.nextToken(); ObjectReader applicationReader = DeserializerStringCache.init(mapper.readerFor(Application.class), context); if (jsonToken == JsonToken.START_ARRAY) { while (jp.nextToken() != JsonToken.END_ARRAY) { apps.addApplication(applicationReader.readValue(jp));
@Override public LocalFile deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { JsonToken t = jp.getCurrentToken(); if (t == JsonToken.START_OBJECT) { t = jp.nextToken(); LocalFile result; String keyName = jp.getCurrentName(); if ("content".equals(keyName)) { jp.nextToken(); result = LocalFile.ofContent(jp.getValueAsString()); } else if ("base64".equals(keyName)) { jp.nextToken(); ByteArrayOutputStream out = new ByteArrayOutputStream(); jp.readBinaryValue(ctxt.getBase64Variant(), out); result = LocalFile.ofContent(out.toByteArray()); } else { throw ctxt.mappingException("Unknown key '" + keyName + "' to deserialize LocalFile"); t = jp.nextToken(); if (t != JsonToken.END_OBJECT) { throw ctxt.mappingException("Unexpected extra map keys to LocalFile"); case START_ARRAY: case END_ARRAY: throw ctxt.mappingException("Attempted unexpected map or array to LocalFile");
while ((jsonToken = jp.nextToken()) != JsonToken.END_OBJECT) { InstanceInfoField instanceInfoField = InstanceInfoField.lookup.find(jp); jsonToken = jp.nextToken(); if (instanceInfoField != null && jsonToken != JsonToken.VALUE_NULL) { switch(instanceInfoField) { break; case PORT: while ((jsonToken = jp.nextToken()) != JsonToken.END_OBJECT) { PortField field = PortField.lookup.find(jp); switch(field) { case PORT: if (jsonToken == JsonToken.FIELD_NAME) jp.nextToken(); builder.setPort(jp.getValueAsInt()); break; case ENABLED: if (jsonToken == JsonToken.FIELD_NAME) jp.nextToken(); builder.enablePort(PortType.UNSECURE, jp.getValueAsBoolean()); break; while ((jsonToken = jp.nextToken()) != JsonToken.END_OBJECT) { PortField field = PortField.lookup.find(jp); switch(field) { case PORT: if (jsonToken == JsonToken.FIELD_NAME) jp.nextToken(); builder.setSecurePort(jp.getValueAsInt()); break; case ENABLED: if (jsonToken == JsonToken.FIELD_NAME) jp.nextToken();
@Override public Application deserialize(JsonParser jp, DeserializationContext context) throws IOException { if (Thread.currentThread().isInterrupted()) { throw new JsonParseException(jp, "processing aborted"); while((jsonToken = jp.nextToken()) != JsonToken.END_OBJECT){ if(JsonToken.FIELD_NAME == jsonToken){ ApplicationField field = ApplicationField.lookup.find(jp); jsonToken = jp.nextToken(); if (field != null) { switch(field) { case NAME: application.setName(jp.getText()); break; case INSTANCE: if (jsonToken == JsonToken.START_ARRAY) { while (jp.nextToken() != JsonToken.END_ARRAY) { application.addInstance(instanceInfoReader.readValue(jp));
if (!nested) { token = parser.nextToken(); if (!nested && token != JsonToken.START_OBJECT) { throw new IOException("Expected data to start with an Object, but was " + token); token = parser.nextToken(); while (token != JsonToken.END_OBJECT) { switch (token) { case FIELD_NAME: fieldName = parser.getCurrentName(); break; case START_OBJECT: case FLOAT: if (handleFloatNumbersAsText) { doc.setString(fieldName, parser.getText()); case DOUBLE: if (handleFloatNumbersAsText) { doc.setString(fieldName, parser.getText()); case BIG_DECIMAL: if (handleFloatNumbersAsText) { doc.setString(fieldName, parser.getText()); token = parser.nextToken();
while (!JsonToken.END_OBJECT.equals(jp.nextToken())) { if (!JsonToken.FIELD_NAME.equals(jp.getCurrentToken())) { throw new JsonParseException(jp, "Expected relation name"); relation = jp.getText(); if (JsonToken.START_ARRAY.equals(jp.nextToken())) { while (!JsonToken.END_ARRAY.equals(jp.nextToken())) { link = jp.readValueAs(Link.class); result.add(new Link(link.getHref(), relation)