@Override public T extract(JsonParser jsonParser) throws IOException { if (jsonParser.getCurrentToken() == START_OBJECT) { return processJsonObject(jsonParser); } if (jsonParser.getCurrentToken() == START_ARRAY) { return processJsonArray(jsonParser); } throw new JsonParseException(jsonParser, "Expected a JSON object or array"); }
public T processJsonObject(JsonParser jsonParser) throws IOException { while (!jsonParser.nextFieldName(fieldName)) { if (!jsonParser.hasCurrentToken()) { throw new JsonParseException(jsonParser, "Unexpected end of object"); } if (jsonParser.getCurrentToken() == END_OBJECT) { // Unable to find matching field return null; } jsonParser.skipChildren(); // Skip nested structure if currently at the start of one } jsonParser.nextToken(); // Shift to first token of the value return delegate.extract(jsonParser); }
@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));
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(); }
public T processJsonArray(JsonParser jsonParser) throws IOException { int currentIndex = 0; while (true) { JsonToken token = jsonParser.nextToken(); if (token == null) { throw new JsonParseException(jsonParser, "Unexpected end of array"); } if (token == END_ARRAY) { // Index out of bounds if (exceptionOnOutOfBounds) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Index out of bounds"); } return null; } if (currentIndex == index) { break; } currentIndex++; jsonParser.skipChildren(); // Skip nested structure if currently at the start of one } return delegate.extract(jsonParser); } }
@Override public ItemPathType deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { if (jp.getCurrentToken() != JsonToken.VALUE_STRING){ throw new JsonParseException("Cannot parse path value. Expected that the value will be string but it is: " + jp.getCurrentTokenId(), jp.getCurrentLocation()); } String path = jp.getText(); if (StringUtils.isBlank(path)){ throw new IllegalStateException("Error while deserializing path. No path specified."); } return new ItemPathType(ItemPathParserTemp.parseFromString(path)); }
/*** * Reserved for internal use. Asserts that the current name of the parser equals the expected value * * @param parser * The {@link JsonParser} whose current token to check. * @param expectedValue * The expected current name of the parser's current token. */ public static void assertIsExpectedFieldName(final JsonParser parser, final String expectedValue) throws JsonParseException, IOException { final String actualValue = parser.getCurrentName(); if (expectedValue == null) { if (actualValue != null) { throw new JsonParseException(String.format(SR.UNEXPECTED_FIELD_NAME, expectedValue, actualValue), parser.getCurrentLocation()); } } else { if (!expectedValue.equals(actualValue)) { throw new JsonParseException(String.format(SR.UNEXPECTED_FIELD_NAME, expectedValue, actualValue), parser.getCurrentLocation()); } } }
private static List<String> readAttachmentsObject(JsonParser jsonParser) throws IOException { List<String> attachmentNameList = new ArrayList<String>(); while(jsonParser.nextToken() != JsonToken.END_OBJECT) { String attachmentName = jsonParser.getCurrentName(); jsonParser.nextToken(); if(jsonParser.getCurrentToken() != JsonToken.START_OBJECT) { String message = CouchDbDocument.ATTACHMENTS_NAME + " contains an invalid object."; throw new JsonParseException(message, jsonParser.getCurrentLocation()); } readIgnoreObject(jsonParser); attachmentNameList.add(attachmentName); } return attachmentNameList; }
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; if (jsonParser.getCurrentToken() == START_OBJECT) { long length = 0; while (true) { JsonToken token = jsonParser.nextToken(); if (token == null) { return null;
public InstanceInfo deserialize(JsonParser jp, DeserializationContext context) throws IOException { if (Thread.currentThread().isInterrupted()) { throw new JsonParseException(jp, "processing aborted"); 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) { break; default: autoUnmarshalEligible(jp.getCurrentName(), jp.getValueAsString(), builder.getRawInstance()); autoUnmarshalEligible(jp.getCurrentName(), jp.getValueAsString(), builder.getRawInstance());
@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) { if (jsonToken == JsonToken.START_ARRAY) { while (jp.nextToken() != JsonToken.END_ARRAY) { application.addInstance(instanceInfoReader.readValue(jp));
@Override public Slice extract(JsonParser jsonParser) throws IOException { JsonToken token = jsonParser.getCurrentToken(); if (token == null) { throw new JsonParseException(jsonParser, "Unexpected end of value"); } if (!token.isScalarValue() || token == VALUE_NULL) { return null; } return utf8Slice(jsonParser.getText()); } }
/** * Parses generic object. * * @return number of elements written * @throws IOException if parse input/output fails * @throws SAXException if parse object fails */ private int parseObject() throws SAXException, IOException { int elementsWritten = 0; while (jsonParser.nextToken() != null && jsonParser.getCurrentToken() != END_OBJECT) { if (FIELD_NAME.equals(jsonParser.getCurrentToken())) { String elementName = jsonParser.getCurrentName(); jsonParser.nextToken(); parseElement(elementName); elementsWritten++; } else { throw new JsonParseException(jsonParser, "expected field name, but got " + jsonParser.getCurrentToken(), jsonParser.getCurrentLocation()); } } return elementsWritten; }
@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; }
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: throw new JsonParseException(parser, "Non-blocking parsers are not supported", parser.getCurrentLocation()); case END_ARRAY: throw new JsonParseException(parser, "Not expecting an END_ARRAY token", parser.getCurrentLocation()); case END_OBJECT: throw new JsonParseException(parser, "Not expecting an END_OBJECT token", parser.getCurrentLocation()); token = parser.nextToken();
if (!nested) { token = parser.nextToken(); if (!nested && token != JsonToken.START_ARRAY) { throw new IOException("Expected data to start with an Array, but was " + token); token = parser.nextToken(); while (token != JsonToken.END_ARRAY) { switch (token) { throw new JsonParseException(parser, "Non-blocking parsers are not supported", parser.getCurrentLocation()); case FIELD_NAME: throw new JsonParseException(parser, "Not expecting a FIELD_NAME token", parser.getCurrentLocation()); case END_ARRAY: throw new JsonParseException(parser, "Not expecting an END_ARRAY token", parser.getCurrentLocation()); case END_OBJECT: throw new JsonParseException(parser, "Not expecting an END_OBJECT token", parser.getCurrentLocation()); token = parser.nextToken();
public Shape read(JsonParser jp, ShapeFactory factory) throws IOException { if(!jp.getCurrentToken().isStructStart()) { throw new JsonParseException(jp, "Expect the start of GeoJSON Geometry object"); } return read( (ObjectNode)jp.getCodec().readTree(jp), factory ); }
/** * Parses a single Resource. * * @return a single resource * @throws IOException */ private Resource parseResource() throws IOException { //current token is ready consumed by parseResources if (jsonParser.getCurrentToken() != JsonToken.START_OBJECT) { throw new JsonParseException("Not a Resource object", jsonParser.getCurrentLocation()); } Resource resource = new Resource(); while (jsonParser.nextToken() != JsonToken.END_OBJECT) { String key = jsonParser.getCurrentName(); switch (key) { default: resource.add(key, parseDefaultValue(key)); break; } } return resource; }
@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; }
/** * Parses attributes as a Map, used in both parseWebIdentitySessionContext and parseSessionContext * * @return attributes for either session context or web identity session context * @throws IOException */ private Map<String, String> parseAttributes() throws IOException { if (jsonParser.nextToken() != JsonToken.START_OBJECT) { throw new JsonParseException("Not a Attributes object", jsonParser.getCurrentLocation()); } Map<String, String> attributes = new HashMap<>(); while (jsonParser.nextToken() != JsonToken.END_OBJECT) { String key = jsonParser.getCurrentName(); String value = jsonParser.nextTextValue(); attributes.put(key, value); } return attributes; }