List of usage examples for com.fasterxml.jackson.core JsonParser getCurrentName
public abstract String getCurrentName() throws IOException, JsonParseException;
From source file:com.msopentech.odatajclient.engine.metadata.edm.EntityTypeDeserializer.java
@Override protected AbstractEntityType doDeserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException { final AbstractEntityType entityType = ODataVersion.V3 == client.getWorkingVersion() ? new com.msopentech.odatajclient.engine.metadata.edm.v3.EntityType() : new com.msopentech.odatajclient.engine.metadata.edm.v4.EntityType(); for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) { final JsonToken token = jp.getCurrentToken(); if (token == JsonToken.FIELD_NAME) { if ("Name".equals(jp.getCurrentName())) { entityType.setName(jp.nextTextValue()); } else if ("Abstract".equals(jp.getCurrentName())) { entityType.setAbstractEntityType(BooleanUtils.toBoolean(jp.nextTextValue())); } else if ("BaseType".equals(jp.getCurrentName())) { entityType.setBaseType(jp.nextTextValue()); } else if ("OpenType".equals(jp.getCurrentName())) { entityType.setOpenType(BooleanUtils.toBoolean(jp.nextTextValue())); } else if ("HasStream".equals(jp.getCurrentName())) { entityType.setHasStream(BooleanUtils.toBoolean(jp.nextTextValue())); } else if ("Key".equals(jp.getCurrentName())) { jp.nextToken();/* w ww . j a v a 2 s . co m*/ entityType.setKey(jp.getCodec().readValue(jp, EntityKey.class)); } else if ("Property".equals(jp.getCurrentName())) { jp.nextToken(); if (entityType instanceof com.msopentech.odatajclient.engine.metadata.edm.v3.EntityType) { ((com.msopentech.odatajclient.engine.metadata.edm.v3.EntityType) entityType).getProperties() .add(jp.getCodec().readValue(jp, com.msopentech.odatajclient.engine.metadata.edm.v3.Property.class)); } else { ((com.msopentech.odatajclient.engine.metadata.edm.v4.EntityType) entityType).getProperties() .add(jp.getCodec().readValue(jp, com.msopentech.odatajclient.engine.metadata.edm.v4.Property.class)); } } else if ("NavigationProperty".equals(jp.getCurrentName())) { jp.nextToken(); if (entityType instanceof com.msopentech.odatajclient.engine.metadata.edm.v3.EntityType) { ((com.msopentech.odatajclient.engine.metadata.edm.v3.EntityType) entityType) .getNavigationProperties().add(jp.getCodec().readValue(jp, com.msopentech.odatajclient.engine.metadata.edm.v3.NavigationProperty.class)); } else { ((com.msopentech.odatajclient.engine.metadata.edm.v4.EntityType) entityType) .getNavigationProperties().add(jp.getCodec().readValue(jp, com.msopentech.odatajclient.engine.metadata.edm.v4.NavigationProperty.class)); } } else if ("Annotation".equals(jp.getCurrentName())) { jp.nextToken(); ((com.msopentech.odatajclient.engine.metadata.edm.v4.EntityType) entityType) .setAnnotation(jp.getCodec().readValue(jp, Annotation.class)); } } } return entityType; }
From source file:org.mongojack.internal.object.BsonObjectGenerator.java
@Override public void copyCurrentStructure(JsonParser jp) throws IOException { JsonToken t = jp.getCurrentToken();/*from www . j av a 2 s. c o m*/ // Let'string handle field-name separately first if (t == JsonToken.FIELD_NAME) { writeFieldName(jp.getCurrentName()); t = jp.nextToken(); // fall-through to copy the associated value } switch (t) { case START_ARRAY: writeStartArray(); while (jp.nextToken() != JsonToken.END_ARRAY) { copyCurrentStructure(jp); } writeEndArray(); break; case START_OBJECT: writeStartObject(); while (jp.nextToken() != JsonToken.END_OBJECT) { copyCurrentStructure(jp); } writeEndObject(); break; default: // others are simple: copyCurrentEvent(jp); } }
From source file:com.microsoft.azure.storage.table.CEKReturn.java
/** * Reserved for internal use. Parses the operation response as a collection of entities. Reads entity data from the * specified input stream using the specified class type and optionally projects each entity result with the * specified resolver into an {@link ODataPayload} containing a collection of {@link TableResult} objects. * /* w ww.j a va 2 s . com*/ * @param inStream * The <code>InputStream</code> to read the data to parse from. * @param clazzType * The class type <code>T</code> implementing {@link TableEntity} for the entities returned. Set to * <code>null</code> to ignore the returned entities and copy only response properties into the * {@link TableResult} objects. * @param resolver * An {@link EntityResolver} instance to project the entities into instances of type <code>R</code>. Set * to <code>null</code> to return the entities as instances of the class type <code>T</code>. * @param options * A {@link TableRequestOptions} object that specifies execution options such as retry policy and timeout * settings for the operation. * @param opContext * An {@link OperationContext} object used to track the execution of the operation. * @return * An {@link ODataPayload} containing a collection of {@link TableResult} objects with the parsed operation * response. * @throws InstantiationException * if an error occurs while constructing the result. * @throws IllegalAccessException * if an error occurs in reflection while parsing the result. * @throws StorageException * if a storage service error occurs. * @throws IOException * if an error occurs while accessing the stream. * @throws JsonParseException * if an error occurs while parsing the stream. */ @SuppressWarnings("unchecked") static <T extends TableEntity, R> ODataPayload<?> parseQueryResponse(final InputStream inStream, final TableRequestOptions options, final Class<T> clazzType, final EntityResolver<R> resolver, final OperationContext opContext) throws JsonParseException, IOException, InstantiationException, IllegalAccessException, StorageException { ODataPayload<T> corePayload = null; ODataPayload<R> resolvedPayload = null; ODataPayload<?> commonPayload = null; JsonParser parser = Utility.getJsonParser(inStream); try { if (resolver != null) { resolvedPayload = new ODataPayload<R>(); commonPayload = resolvedPayload; } else { corePayload = new ODataPayload<T>(); commonPayload = corePayload; } if (!parser.hasCurrentToken()) { parser.nextToken(); } JsonUtilities.assertIsStartObjectJsonToken(parser); // move into data parser.nextToken(); // if there is a clazz type and if JsonNoMetadata, create a classProperties dictionary to use for type inference once // instead of querying the cache many times HashMap<String, PropertyPair> classProperties = null; if (options.getTablePayloadFormat() == TablePayloadFormat.JsonNoMetadata && clazzType != null) { classProperties = PropertyPair.generatePropertyPairs(clazzType); } while (parser.getCurrentToken() != null) { if (parser.getCurrentToken() == JsonToken.FIELD_NAME && parser.getCurrentName().equals(ODataConstants.VALUE)) { // move to start of array parser.nextToken(); JsonUtilities.assertIsStartArrayJsonToken(parser); // go to properties parser.nextToken(); while (parser.getCurrentToken() == JsonToken.START_OBJECT) { final TableResult res = parseJsonEntity(parser, clazzType, classProperties, resolver, options, opContext); if (corePayload != null) { corePayload.tableResults.add(res); } if (resolver != null) { resolvedPayload.results.add((R) res.getResult()); } else { corePayload.results.add((T) res.getResult()); } parser.nextToken(); } JsonUtilities.assertIsEndArrayJsonToken(parser); } parser.nextToken(); } } finally { parser.close(); } return commonPayload; }
From source file:com.joliciel.jochre.search.highlight.Snippet.java
private void read(JsonParser jsonParser) { try {//from ww w. j a va2 s. c o m if (jsonParser.getCurrentToken() != JsonToken.START_OBJECT) throw new RuntimeException("Expected START_OBJECT, but was " + jsonParser.getCurrentToken() + " at " + jsonParser.getCurrentLocation()); while (jsonParser.nextToken() != JsonToken.END_OBJECT) { String fieldName = jsonParser.getCurrentName(); if (fieldName.equals("docId")) { this.docId = jsonParser.nextIntValue(0); } else if (fieldName.equals("field")) { this.field = jsonParser.nextTextValue(); } else if (fieldName.equals("start")) { this.startOffset = jsonParser.nextIntValue(0); } else if (fieldName.equals("end")) { this.endOffset = jsonParser.nextIntValue(0); } else if (fieldName.equals("score")) { jsonParser.nextValue(); this.score = jsonParser.getDoubleValue(); this.scoreCalculated = true; } else if (fieldName.equals("terms")) { if (jsonParser.nextToken() != JsonToken.START_ARRAY) throw new RuntimeException("Expected START_ARRAY, but was " + jsonParser.getCurrentToken() + " at " + jsonParser.getCurrentLocation()); while (jsonParser.nextToken() != JsonToken.END_ARRAY) { if (jsonParser.getCurrentToken() != JsonToken.START_OBJECT) throw new RuntimeException("Expected START_OBJECT, but was " + jsonParser.getCurrentToken() + " at " + jsonParser.getCurrentLocation()); int termDocId = docId; String termField = field; int termStart = 0; int termEnd = 0; int pageIndex = 0; int imageIndex = 0; double weight = 0.0; while (jsonParser.nextToken() != JsonToken.END_OBJECT) { String termFieldName = jsonParser.getCurrentName(); if (termFieldName.equals("docId")) { termDocId = jsonParser.nextIntValue(0); } else if (termFieldName.equals("field")) { termField = jsonParser.nextTextValue(); } else if (termFieldName.equals("start")) { termStart = jsonParser.nextIntValue(0); } else if (termFieldName.equals("end")) { termEnd = jsonParser.nextIntValue(0); } else if (termFieldName.equals("pageIndex")) { pageIndex = jsonParser.nextIntValue(0); } else if (termFieldName.equals("imageIndex")) { imageIndex = jsonParser.nextIntValue(0); } else if (termFieldName.equals("weight")) { jsonParser.nextValue(); weight = jsonParser.getDoubleValue(); } else { throw new RuntimeException("Unexpected term field name: " + termFieldName + " at " + jsonParser.getCurrentLocation()); } } HighlightTerm highlightTerm = new HighlightTerm(termDocId, termField, termStart, termEnd, imageIndex, pageIndex); highlightTerm.setWeight(weight); this.highlightTerms.add(highlightTerm); } } else { throw new RuntimeException( "Unexpected field name: " + fieldName + " at " + jsonParser.getCurrentLocation()); } } } catch (JsonParseException e) { LOG.error(e); throw new RuntimeException(e); } catch (IOException e) { LOG.error(e); throw new RuntimeException(e); } }
From source file:com.msopentech.odatajclient.engine.data.metadata.edm.EntityTypeDeserializer.java
@Override protected AbstractEntityType doDeserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException { final AbstractEntityType entityType = ODataVersion.V3 == client.getWorkingVersion() ? new com.msopentech.odatajclient.engine.data.metadata.edm.v3.EntityType() : new com.msopentech.odatajclient.engine.data.metadata.edm.v4.EntityType(); for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) { final JsonToken token = jp.getCurrentToken(); if (token == JsonToken.FIELD_NAME) { if ("Name".equals(jp.getCurrentName())) { entityType.setName(jp.nextTextValue()); } else if ("Abstract".equals(jp.getCurrentName())) { entityType.setAbstractEntityType(BooleanUtils.toBoolean(jp.nextTextValue())); } else if ("BaseType".equals(jp.getCurrentName())) { entityType.setBaseType(jp.nextTextValue()); } else if ("OpenType".equals(jp.getCurrentName())) { entityType.setOpenType(BooleanUtils.toBoolean(jp.nextTextValue())); } else if ("HasStream".equals(jp.getCurrentName())) { entityType.setHasStream(BooleanUtils.toBoolean(jp.nextTextValue())); } else if ("Key".equals(jp.getCurrentName())) { jp.nextToken();/*from w w w .j av a 2 s .c o m*/ entityType.setKey(jp.getCodec().readValue(jp, EntityKey.class)); } else if ("Property".equals(jp.getCurrentName())) { jp.nextToken(); if (entityType instanceof com.msopentech.odatajclient.engine.data.metadata.edm.v3.EntityType) { ((com.msopentech.odatajclient.engine.data.metadata.edm.v3.EntityType) entityType) .getProperties().add(jp.getCodec().readValue(jp, com.msopentech.odatajclient.engine.data.metadata.edm.v3.Property.class)); } else { ((com.msopentech.odatajclient.engine.data.metadata.edm.v4.EntityType) entityType) .getProperties().add(jp.getCodec().readValue(jp, com.msopentech.odatajclient.engine.data.metadata.edm.v4.Property.class)); } } else if ("NavigationProperty".equals(jp.getCurrentName())) { jp.nextToken(); if (entityType instanceof com.msopentech.odatajclient.engine.data.metadata.edm.v3.EntityType) { ((com.msopentech.odatajclient.engine.data.metadata.edm.v3.EntityType) entityType) .getNavigationProperties().add(jp.getCodec().readValue(jp, com.msopentech.odatajclient.engine.data.metadata.edm.v3.NavigationProperty.class)); } else { ((com.msopentech.odatajclient.engine.data.metadata.edm.v4.EntityType) entityType) .getNavigationProperties().add(jp.getCodec().readValue(jp, com.msopentech.odatajclient.engine.data.metadata.edm.v4.NavigationProperty.class)); } } else if ("Annotation".equals(jp.getCurrentName())) { jp.nextToken(); ((com.msopentech.odatajclient.engine.data.metadata.edm.v4.EntityType) entityType) .setAnnotation(jp.getCodec().readValue(jp, Annotation.class)); } } } return entityType; }
From source file:org.graylog2.gelfclient.encoder.GelfMessageJsonEncoderTest.java
@Test public void testEncode() throws Exception { byte[] bytes = readBytes(); JsonFactory json = new JsonFactory(); JsonParser parser = json.createParser(bytes); String version = null;/*from w ww . ja v a 2 s .co m*/ Number timestamp = null; String host = null; String short_message = null; String full_message = null; Number level = null; Number _foo = null; Number _bar = null; String _baz = null; while (parser.nextToken() != JsonToken.END_OBJECT) { String key = parser.getCurrentName(); if (key == null) { continue; } parser.nextToken(); switch (key) { case "version": version = parser.getText(); break; case "timestamp": timestamp = parser.getNumberValue(); break; case "host": host = parser.getText(); break; case "short_message": short_message = parser.getText(); break; case "full_message": full_message = parser.getText(); break; case "level": level = parser.getNumberValue(); break; case "_foo": _foo = parser.getNumberValue(); break; case "_bar": _bar = parser.getNumberValue(); break; case "_baz": _baz = parser.getText(); break; default: throw new Exception("Found unexpected field in JSON payload: " + key); } } assertEquals(message.getVersion().toString(), version); assertEquals(message.getTimestamp(), timestamp); assertEquals(message.getHost(), host); assertEquals(message.getMessage(), short_message); assertEquals(message.getFullMessage(), full_message); assertEquals(message.getLevel().getNumericLevel(), level); assertEquals(1.0, _foo); assertEquals(128, _bar); assertEquals("a value", _baz); }
From source file:com.ntsync.shared.RawContact.java
private static List<RawAddressData> readAddressList(String rowId, List<RawAddressData> addresses, JsonParser jp) throws IOException { List<RawAddressData> newAddresses = addresses; while (jp.nextToken() != JsonToken.END_ARRAY) { AddressType type = null;/* w w w . j a va2 s .co m*/ String label = null; String street = null; String city = null; String postcode = null; String country = null; String region = null; String pobox = null; String neighborhood = null; boolean isSuperPrimary = false; boolean isPrimary = false; while (jp.nextToken() != JsonToken.END_OBJECT) { String namefield = jp.getCurrentName(); // move to value if (jp.nextToken() == null) { throw new JsonParseException("Invalid JSON-Structure. End of Array missing.", jp.getCurrentLocation()); } if (ContactConstants.NEIGHBORHOOD.equals(namefield)) { neighborhood = jp.getValueAsString(); } else if (ContactConstants.TYPE.equals(namefield)) { type = AddressType.fromVal(jp.getValueAsInt()); } else if (ContactConstants.SUPERPRIMARY.equals(namefield)) { isSuperPrimary = jp.getValueAsBoolean(); } else if (ContactConstants.PRIMARY.equals(namefield)) { isPrimary = jp.getValueAsBoolean(); } else if (ContactConstants.LABEL.equals(namefield)) { label = jp.getValueAsString(); } else if (ContactConstants.STREET.equals(namefield)) { street = jp.getValueAsString(); } else if (ContactConstants.REGION.equals(namefield)) { region = jp.getValueAsString(); } else if (ContactConstants.CITY.equals(namefield)) { city = jp.getValueAsString(); } else if (ContactConstants.POSTCODE.equals(namefield)) { postcode = jp.getValueAsString(); } else if (ContactConstants.COUNTRY.equals(namefield)) { country = jp.getValueAsString(); } else if (ContactConstants.REGION.equals(namefield)) { region = jp.getValueAsString(); } else if (ContactConstants.POBOX.equals(namefield)) { pobox = jp.getValueAsString(); } else { LOG.error(JSON_FIELDNOTRECOGNIZED + rowId + " Fieldname:" + namefield); } } if (newAddresses == null) { newAddresses = new ArrayList<RawAddressData>(); } if (type == null) { type = AddressType.TYPE_OTHER; } newAddresses.add(new RawAddressData(type, label, isPrimary, isSuperPrimary, street, pobox, neighborhood, city, region, postcode, country)); } return newAddresses; }
From source file:com.tlongdev.bktf.interactor.TlongdevPriceListInteractor.java
private int parseJson(InputStream inputStream) throws IOException { //Create a parser from the input stream for fast parsing and low impact on memory JsonFactory factory = new JsonFactory(); JsonParser parser = factory.createParser(inputStream); Vector<ContentValues> cVVector = new Vector<>(); int retVal = 0; int count = 0; //Not a JSON if it doesn't start with START OBJECT if (parser.nextToken() != JsonToken.START_OBJECT) { return -1; }//from w ww .j a v a 2 s. c o m while (parser.nextToken() != JsonToken.END_OBJECT) { String name = parser.getCurrentName(); parser.nextToken(); switch (name) { case "success": if (parser.getIntValue() == 0) { retVal = 1; } break; case "message": errorMessage = parser.getText(); break; case "count": count = parser.getIntValue(); break; case "prices": while (parser.nextToken() != JsonToken.END_ARRAY) { ContentValues values = buildContentValues(parser); cVVector.add(values); } if (cVVector.size() > 0) { ContentValues[] cvArray = new ContentValues[cVVector.size()]; cVVector.toArray(cvArray); //Insert all the data into the database rowsInserted = mContext.getContentResolver().bulkInsert(PriceEntry.CONTENT_URI, cvArray); Log.v(LOG_TAG, "inserted " + rowsInserted + " rows into prices table"); } break; } } parser.close(); return retVal; }
From source file:net.floodlightcontroller.loadbalancer.MembersResource.java
protected LBMember jsonToMember(String json) throws IOException { MappingJsonFactory f = new MappingJsonFactory(); JsonParser jp; LBMember member = new LBMember(); try {// w w w .j a v a2 s.com jp = f.createJsonParser(json); } catch (JsonParseException e) { throw new IOException(e); } jp.nextToken(); if (jp.getCurrentToken() != JsonToken.START_OBJECT) { throw new IOException("Expected START_OBJECT"); } while (jp.nextToken() != JsonToken.END_OBJECT) { if (jp.getCurrentToken() != JsonToken.FIELD_NAME) { throw new IOException("Expected FIELD_NAME"); } String n = jp.getCurrentName(); jp.nextToken(); if (jp.getText().equals("")) continue; if (n.equals("id")) { member.id = jp.getText(); continue; } else if (n.equals("address")) { member.address = IPv4.toIPv4Address(jp.getText()); continue; } else if (n.equals("port")) { member.port = Short.parseShort(jp.getText()); continue; } else if (n.equals("connection_limit")) { member.connectionLimit = Integer.parseInt(jp.getText()); continue; } else if (n.equals("admin_state")) { member.adminState = Short.parseShort(jp.getText()); continue; } else if (n.equals("status")) { member.status = Short.parseShort(jp.getText()); continue; } else if (n.equals("pool_id")) { member.poolId = jp.getText(); continue; } log.warn("Unrecognized field {} in " + "parsing Members", jp.getText()); } jp.close(); return member; }
From source file:org.helm.notation2.wsadapter.MonomerWSLoader.java
/** * Private routine to deserialize a JSON containing attachment data. This is * done manually to give more freedom regarding data returned by the * webservice./* ww w. ja va 2 s . com*/ * * @param parser the JSONParser containing JSONData. * @param attachmentDB the attachments stored in the Toolkit * @return List containing attachments * * @throws JsonParseException * @throws IOException */ private List<Attachment> deserializeAttachmentList(JsonParser parser, Map<String, Attachment> attachmentDB) throws JsonParseException, IOException { List<Attachment> attachments = new ArrayList<Attachment>(); Attachment currentAttachment = null; while (!JsonToken.END_ARRAY.equals(parser.nextToken())) { String fieldName = parser.getCurrentName(); JsonToken token = parser.getCurrentToken(); if (JsonToken.START_OBJECT.equals(token)) { currentAttachment = new Attachment(); } else if (JsonToken.END_OBJECT.equals(token)) { currentAttachment.setCapGroupSMILES( attachmentDB.get(currentAttachment.getAlternateId()).getCapGroupSMILES()); attachments.add(currentAttachment); } if (fieldName != null) { switch (fieldName) { case "id": parser.nextToken(); currentAttachment.setId(Integer.parseInt(parser.getText())); break; case "alternateId": parser.nextToken(); currentAttachment.setAlternateId(parser.getText()); break; case "label": parser.nextToken(); currentAttachment.setLabel(parser.getText()); break; case "capGroupName": parser.nextToken(); currentAttachment.setCapGroupName(parser.getText()); break; case "capGroupSMILES": parser.nextToken(); currentAttachment.setCapGroupSMILES(parser.getText()); break; default: break; } } } return attachments; }