List of usage examples for com.fasterxml.jackson.core JsonParser getText
public abstract String getText() throws IOException, JsonParseException;
From source file:org.nuxeo.client.test.marshallers.DocumentMarshaller.java
protected static Document readDocument(JsonParser jp) throws IOException { String uid = null;/* ww w. j a va2 s .co m*/ String type = null; String path = null; String state = null; String versionLabel = null; String isCheckedOut = null; String lockCreated = null; String lockOwner = null; String repository = null; String changeToken = null; boolean isProxy = false; JsonToken tok = jp.nextToken(); Map<String, Object> properties = new HashMap<>(); while (tok != null && tok != JsonToken.END_OBJECT) { tok = jp.nextToken(); String key = jp.getText(); tok = jp.nextToken(); if ("uid".equals(key)) { uid = jp.getText(); } else if ("path".equals(key)) { path = jp.getText(); } else if ("type".equals(key)) { type = jp.getText(); } else if ("state".equals(key)) { state = jp.getText(); } else if ("versionLabel".equals(key)) { versionLabel = jp.getText(); } else if ("isCheckedOut".equals(key)) { isCheckedOut = jp.getText(); } else if ("lock".equals(key)) { if (!JsonToken.VALUE_NULL.equals(jp.getCurrentToken())) { String[] lock = jp.getText().split(":"); lockOwner = lock[0]; lockCreated = lock[1]; } } else if ("lockCreated".equals(key)) { lockCreated = jp.getText(); } else if ("lockOwner".equals(key)) { lockOwner = jp.getText(); } else if ("repository".equals(key)) { repository = jp.getText(); } else if ("properties".equals(key)) { readProperties(jp, properties); } else if ("changeToken".equals(key)) { changeToken = jp.getText(); } else if ("isProxy".equals(key)) { isProxy = jp.getBooleanValue(); } } return new Document(uid, type, null, changeToken, path, state, lockOwner, lockCreated, repository, versionLabel, isCheckedOut, isProxy, properties, null); }
From source file:org.apache.lucene.server.handlers.AddDocumentHandler.java
/** Parses the current json token into the corresponding * java object. *///from w ww. j ava 2s. c om private static Object getNativeValue(FieldDef fd, JsonToken token, JsonParser p) throws IOException { Object o; if (token == JsonToken.VALUE_STRING) { o = p.getText(); } else if (token == JsonToken.VALUE_NUMBER_INT) { o = Long.valueOf(p.getLongValue()); } else if (token == JsonToken.VALUE_NUMBER_FLOAT) { o = Double.valueOf(p.getDoubleValue()); } else if (token == JsonToken.VALUE_TRUE) { o = Boolean.TRUE; } else if (token == JsonToken.VALUE_FALSE) { o = Boolean.FALSE; } else if (fd.faceted.equals("hierarchy") && token == JsonToken.START_ARRAY) { if (fd.multiValued == false) { List<String> values = new ArrayList<>(); while (true) { token = p.nextToken(); if (token == JsonToken.END_ARRAY) { break; } else if (token != JsonToken.VALUE_STRING) { if (token == JsonToken.START_ARRAY) { fail(fd.name, "expected array of strings, but saw array inside array"); } else { fail(fd.name, "expected array of strings, but saw " + token + " inside array"); } } values.add(p.getText()); } o = values; } else { List<List<String>> values = new ArrayList<>(); while (true) { token = p.nextToken(); if (token == JsonToken.END_ARRAY) { break; } else if (token == JsonToken.START_ARRAY) { List<String> sub = new ArrayList<>(); values.add(sub); while (true) { token = p.nextToken(); if (token == JsonToken.VALUE_STRING) { sub.add(p.getText()); } else if (token == JsonToken.END_ARRAY) { break; } else { fail(fd.name, "expected array of strings or array of array of strings, but saw " + token + " inside inner array"); } } } else if (token == JsonToken.VALUE_STRING) { List<String> sub = new ArrayList<>(); values.add(sub); sub.add(p.getText()); } else if (token == JsonToken.START_ARRAY) { fail(fd.name, "expected array of strings, but saw array inside array"); } else { fail(fd.name, "expected array of strings, but saw " + token + " inside array"); } } o = values; } } else if (fd.valueType == FieldDef.FieldValueType.LAT_LON) { if (token != JsonToken.START_ARRAY) { fail(fd.name, "latlon field must be [lat, lon] value; got " + token); } double[] latLon = new double[2]; token = p.nextToken(); if (token != JsonToken.VALUE_NUMBER_FLOAT) { fail(fd.name, "latlon field must be [lat, lon] value; got " + token); } latLon[0] = p.getDoubleValue(); token = p.nextToken(); if (token != JsonToken.VALUE_NUMBER_FLOAT) { fail(fd.name, "latlon field must be [lat, lon] value; got " + token); } latLon[1] = p.getDoubleValue(); token = p.nextToken(); if (token != JsonToken.END_ARRAY) { fail(fd.name, "latlon field must be [lat, lon] value; got " + token); } o = latLon; } else { String message; if (token == JsonToken.VALUE_NULL) { message = "null field value not supported; just omit this field from the document instead"; } else { message = "value in inner object field value should be string, int/long, float/double or boolean; got " + token; } fail(fd.name, message); // Dead code but compiler disagrees: o = null; } return o; }
From source file:com.github.heuermh.ensemblrestclient.JacksonLookupConverter.java
static Lookup parseLookup(final JsonFactory jsonFactory, final InputStream inputStream) throws IOException { JsonParser parser = null; try {/*w w w . j a v a2s .co m*/ parser = jsonFactory.createParser(inputStream); parser.nextToken(); String id = null; String species = null; String type = null; String database = null; String locationName = null; String coordinateSystem = "chromosome"; // reasonable default? int start = -1; int end = -1; int strand = -1; while (parser.nextToken() != JsonToken.END_OBJECT) { String field = parser.getCurrentName(); parser.nextToken(); if ("id".equals(field)) { id = parser.getText(); } else if ("species".equals(field)) { species = parser.getText(); } else if ("object_type".equals(field)) { type = parser.getText(); } else if ("db_type".equals(field)) { database = parser.getText(); } else if ("seq_region_name".equals(field)) { locationName = parser.getText(); } else if ("start".equals(field)) { start = Integer.parseInt(parser.getText()); } else if ("end".equals(field)) { end = Integer.parseInt(parser.getText()); } else if ("strand".equals(field)) { strand = Integer.parseInt(parser.getText()); } } return new Lookup(id, species, type, database, new Location(locationName, coordinateSystem, start, end, strand)); } finally { try { inputStream.close(); } catch (Exception e) { // ignored } try { parser.close(); } catch (Exception e) { // ignored } } }
From source file:com.github.heuermh.ensemblrestclient.JacksonArchivedSequenceConverter.java
static ArchivedSequence parseArchivedSequence(final JsonFactory jsonFactory, final InputStream inputStream) throws IOException { JsonParser parser = null; try {// w w w .j a v a 2s . c om parser = jsonFactory.createParser(inputStream); parser.nextToken(); String id = null; String type = null; String assembly = null; String release = null; String version = null; String latest = null; String peptide = null; boolean current = false; List<String> possibleReplacement = new ArrayList<String>(); while (parser.nextToken() != JsonToken.END_OBJECT) { String field = parser.getCurrentName(); parser.nextToken(); if ("id".equals(field)) { id = parser.getText(); } else if ("type".equals(field)) { type = parser.getText(); } else if ("assembly".equals(field)) { assembly = parser.getText(); } else if ("release".equals(field)) { release = parser.getText(); } else if ("version".equals(field)) { version = parser.getText(); } else if ("latest".equals(field)) { latest = parser.getText(); } else if ("peptide".equals(field)) { peptide = parser.getText(); } else if ("is_current".equals(field)) { current = (Integer.parseInt(parser.getText()) > 0); } else if ("possible_replacement".equals(field)) { while (parser.nextToken() != JsonToken.END_ARRAY) { possibleReplacement.add(parser.getText()); } } } return new ArchivedSequence(id, type, assembly, release, version, latest, peptide, current, possibleReplacement.toArray(new String[possibleReplacement.size()])); } finally { try { inputStream.close(); } catch (Exception e) { // ignored } try { parser.close(); } catch (Exception e) { // ignored } } }
From source file:org.datagator.api.client.MatrixDeserializer.java
private static ArrayList<ArrayList<Object>> parseRows(JsonParser jp, int bodyRow, int bodyColumn) throws IOException, JsonProcessingException { ArrayList<ArrayList<Object>> columnHeaders = new ArrayList<ArrayList<Object>>(); int rowIndex = 0; JsonToken token = jp.getCurrentToken(); // START_ARRAY if (!token.equals(JsonToken.START_ARRAY)) { throw new RuntimeException(String.format("Unexpected token %s", token)); }/*from w w w . ja v a 2s. c o m*/ token = jp.nextToken(); // START_ARRAY while (token.equals(JsonToken.START_ARRAY)) { int columnIndex = 0; if (rowIndex < bodyRow) { ArrayList<Object> rowBuffer = new ArrayList<Object>(); token = jp.nextToken(); while (!token.equals(JsonToken.END_ARRAY)) { rowBuffer.add(jp.getText()); columnIndex += 1; token = jp.nextToken(); } columnHeaders.add(rowBuffer); } else { token = jp.nextToken(); while (!token.equals(JsonToken.END_ARRAY)) { columnIndex += 1; token = jp.nextToken(); } } rowIndex += 1; token = jp.nextToken(); // START_ARRAY } return columnHeaders; }
From source file:com.github.heuermh.ensemblrestclient.JacksonOverlapConverter.java
static List<Variation> parseOverlap(final JsonFactory jsonFactory, final InputStream inputStream) throws IOException { JsonParser parser = null; try {/*from www . j av a 2 s. com*/ parser = jsonFactory.createParser(inputStream); parser.nextToken(); String id = null; String reference = null; List<String> alternateAlleles = new ArrayList<String>(); String locationName = null; String coordinateSystem = "chromosome"; int start = -1; int end = -1; int strand = -1; List<Variation> variationFeatures = new ArrayList<Variation>(); while (parser.nextToken() != JsonToken.END_ARRAY) { while (parser.nextToken() != JsonToken.END_OBJECT) { String field = parser.getCurrentName(); parser.nextToken(); if ("id".equals(field)) { id = parser.getText(); } else if ("seq_region_name".equals(field)) { locationName = parser.getText(); } else if ("start".equals(field)) { start = Integer.parseInt(parser.getText()); } else if ("end".equals(field)) { end = Integer.parseInt(parser.getText()); } else if ("strand".equals(field)) { strand = Integer.parseInt(parser.getText()); } else if ("alt_alleles".equals(field)) { int index = 0; while (parser.nextToken() != JsonToken.END_ARRAY) { if (index == 0) { reference = parser.getText(); } else if (index == 1) { alternateAlleles.add(parser.getText()); } index++; } } } variationFeatures.add(new Variation(id, reference, alternateAlleles, new Location(locationName, coordinateSystem, start, end, strand))); id = null; reference = null; alternateAlleles.clear(); locationName = null; start = -1; end = -1; strand = -1; } return variationFeatures; } finally { try { inputStream.close(); } catch (Exception e) { // ignored } try { parser.close(); } catch (Exception e) { // ignored } } }
From source file:com.github.heuermh.ensemblrestclient.JacksonVariationConverter.java
static Variation parseVariation(final JsonFactory jsonFactory, final InputStream inputStream) throws IOException { JsonParser parser = null; try {//from w w w.j a v a 2 s .c om parser = jsonFactory.createParser(inputStream); parser.nextToken(); String identifier = null; String referenceAllele = null; List<String> alternateAlleles = new ArrayList<String>(); String locationName = null; String coordinateSystem = "chromosome"; int start = -1; int end = -1; int strand = -1; while (parser.nextToken() != JsonToken.END_OBJECT) { String field = parser.getCurrentName(); parser.nextToken(); if ("name".equals(field)) { identifier = parser.getText(); } else if ("mappings".equals(field)) { // todo: will only catch last mapping while (parser.nextToken() != JsonToken.END_ARRAY) { while (parser.nextToken() != JsonToken.END_OBJECT) { String mappingsField = parser.getCurrentName(); parser.nextToken(); if ("seq_region_name".equals(mappingsField)) { locationName = parser.getText(); } else if ("start".equals(mappingsField)) { start = Integer.parseInt(parser.getText()); } else if ("end".equals(mappingsField)) { end = Integer.parseInt(parser.getText()); } else if ("strand".equals(mappingsField)) { strand = Integer.parseInt(parser.getText()); } else if ("allele_string".equals(mappingsField)) { String[] tokens = parser.getText().split("/"); // todo: check ref here against ancestral_allele referenceAllele = tokens[0]; for (int i = 1; i < tokens.length; i++) { alternateAlleles.add(tokens[i]); } } } } } else if ("synonyms".equals(field)) { while (parser.nextToken() != JsonToken.END_ARRAY) { // ignore } } else if ("evidence".equals(field)) { while (parser.nextToken() != JsonToken.END_ARRAY) { // ignore } } } return new Variation(identifier, referenceAllele, alternateAlleles, new Location(locationName, coordinateSystem, start, end, strand)); } finally { try { inputStream.close(); } catch (Exception e) { // ignored } try { parser.close(); } catch (Exception e) { // ignored } } }
From source file:org.apache.lucene.server.handlers.AddDocumentHandler.java
/** Parses the fields, which should look like {field1: * ..., field2: ..., ...} *//*from www. j a v a 2s .c o m*/ public static void parseFields(IndexState state, Document doc, JsonParser p) throws IOException { JsonToken token = p.nextToken(); if (token != JsonToken.START_OBJECT) { throw new IllegalArgumentException("fields should be an object"); } while (true) { token = p.nextToken(); if (token == JsonToken.END_OBJECT) { break; } assert token == JsonToken.FIELD_NAME; parseOneField(p, state, doc, p.getText()); } }
From source file:com.devcraftsman.blog.post.api.util.ISOLocalDateTimeDeserializer.java
@Override public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { if (p.getText() != null) return LocalDateTime.parse(p.getText(), DateTimeFormatter.ISO_DATE_TIME); else/*from ww w . j av a 2s . c om*/ return null; }
From source file:com.inversoft.json.ZoneIdDeserializer.java
@Override public ZoneId deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { return ZoneId.of(jp.getText()); }