List of usage examples for com.fasterxml.jackson.core JsonParser nextToken
public abstract JsonToken nextToken() throws IOException, JsonParseException;
From source file:com.cinnober.msgcodec.json.TypeScannerJsonParserTest.java
@Test public void test1() throws IOException { JsonParser p = new JsonFactory().createParser("{\"a\":1, \"$type\":\"x\", \"b\":1.1}"); p.nextToken(); // START_OBJECT p.nextToken(); // FIELD_NAME TypeScannerJsonParser p2 = new TypeScannerJsonParser(p); assertEquals("x", p2.findType()); assertNextField("a", p2); assertNextIntValue(1, p2);//from w w w. j a v a 2 s. c om assertNextField("b", p2); assertNextFloatValue(1.1, p2); assertEquals(JsonToken.END_OBJECT, p2.nextToken()); assertNull(p2.nextToken()); }
From source file:com.cinnober.msgcodec.json.TypeScannerJsonParserTest.java
@Test public void test2() throws IOException { JsonParser p = new JsonFactory().createParser("{\"a\":1, \"b\":1.1, \"$type\":\"x\"}"); p.nextToken(); // START_OBJECT p.nextToken(); // FIELD_NAME TypeScannerJsonParser p2 = new TypeScannerJsonParser(p); assertEquals("x", p2.findType()); assertNextField("a", p2); assertNextIntValue(1, p2);/*from w ww .j a v a2s .c o m*/ assertNextField("b", p2); assertNextFloatValue(1.1, p2); assertEquals(JsonToken.END_OBJECT, p2.nextToken()); assertNull(p2.nextToken()); }
From source file:com.sdl.odata.unmarshaller.json.JsonLinkUnmarshaller.java
@Override protected String getToEntityId(ODataRequestContext requestContext) throws ODataUnmarshallingException { // The body is expected to contain a single entity reference // See OData JSON specification chapter 13 String bodyText;//from w w w.jav a 2s .c o m try { bodyText = requestContext.getRequest().getBodyText(StandardCharsets.UTF_8.name()); } catch (UnsupportedEncodingException e) { throw new ODataSystemException("UTF-8 is not supported", e); } String idValue = null; try { JsonParser parser = new JsonFactory().createParser(bodyText); while (idValue == null && !parser.isClosed()) { JsonToken token = parser.nextToken(); if (token == null) { break; } if (token.equals(JsonToken.FIELD_NAME) && parser.getCurrentName().equals(JsonConstants.ID)) { token = parser.nextToken(); if (token.equals(JsonToken.VALUE_STRING)) { idValue = parser.getText(); } } } } catch (IOException e) { throw new ODataUnmarshallingException("Error while parsing JSON data", e); } if (isNullOrEmpty(idValue)) { throw new ODataUnmarshallingException("The JSON object in the body has no '@odata.id' value," + " or the value is empty. The JSON object in the body must have an '@odata.id' value" + " that refers to the entity to link to."); } return idValue; }
From source file:com.world.watch.worldwatchcron.util.WWCron.java
public long extractLatestTimeStamp(String newsJson) { long maxTS = 0; JsonFactory factory = new JsonFactory(); try {/* w w w . j a v a2 s. co m*/ JsonParser parser = factory.createParser(newsJson); while (!parser.isClosed()) { JsonToken token = parser.nextToken(); if (token == null) { break; } String fieldName = parser.getCurrentName(); if (fieldName != null && fieldName.equals("date")) { parser.nextToken(); long date = Long.parseLong(parser.getText()); if (maxTS < date) { maxTS = date; } } } } catch (JsonParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block //logger.error("File not found ", e); } return maxTS; }
From source file:org.apache.ode.jacob.soup.jackson.ChannelProxyDeserializer.java
@Override public Channel deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { String type = null;//from w w w .j a v a2 s . co m int id = -1; while (jp.nextToken() != JsonToken.END_OBJECT) { String fieldname = jp.getCurrentName(); if (jp.getCurrentToken() == JsonToken.FIELD_NAME) { // if we're not already on the field, advance by one. jp.nextToken(); } if ("channelType".equals(fieldname)) { type = jp.getText(); } else if ("channelId".equals(fieldname)) { id = jp.getIntValue(); } } if (type == null) { throw ctxt.mappingException(Channel.class); } if (id < 0) { throw ctxt.mappingException(Channel.class); } try { CommChannel cchannel = new CommChannel(ctxt.findClass(type)); cchannel.setId(id); return (Channel) ChannelFactory.createChannel(cchannel, cchannel.getType()); } catch (ClassNotFoundException e) { throw ctxt.instantiationException(Channel.class, e); } }
From source file:org.dbrain.data.jackson.JacksonSerializer.java
/** * Check that there is no more token on the wire. *//*from w w w. ja v a2 s . com*/ private void checkEof(JsonParser parser) throws IOException { JsonToken token = parser.nextToken(); if (token != null) { throw new ParseException("Unexpected json token: " + token.name()); } }
From source file:YDExtBattriReader.java
@Override protected void read(OpenRtb.BidRequest.Imp.Native.Builder message, JsonParser par) throws IOException { if (Constants.EXTEND_BATTRI_FIELD_NAME.equals(getCurrentName(par))) { List<Integer> battris = new ArrayList<Integer>(); for (startArray(par); endArray(par); par.nextToken()) { try { int battri = Integer.parseInt(par.getText()); battris.add(battri);/* w w w .jav a 2 s .c o m*/ } catch (Exception e) { logger.warn("battri is not a int value.", e); } } if (battris.isEmpty()) return; message.setExtension(OpenRtbYDExtForDsp.battri, battris); } }
From source file:com.basistech.rosette.dm.jackson.ListAttributeDeserializer.java
@SuppressWarnings("unchecked") private ListAttribute deserialize(JsonParser jp, DeserializationContext ctxt, TokenBuffer tb) throws IOException { jp.nextToken(); String keyName = jp.getText(); if (tb != null) { // need to put back skipped properties? jp = JsonParserSequence.createFlattened(tb.asParser(jp), jp); }// w ww.j a v a2 s .c om // Must point to the next value; tb had no current, jp pointed to VALUE_STRING: KnownAttribute attribute = KnownAttribute.getAttributeForKey(keyName); if (attribute == null) { attribute = KnownAttribute.UNKNOWN; } Class<? extends BaseAttribute> itemClass = attribute.attributeClass(); ListAttribute.Builder<BaseAttribute> builder = new ListAttribute.Builder<>(attribute.attributeClass()); List<BaseAttribute> items = Lists.newArrayList(); JsonToken nextToken; while ((nextToken = jp.nextToken()) != JsonToken.END_OBJECT) { if (nextToken != JsonToken.FIELD_NAME) { throw ctxt.wrongTokenException(jp, JsonToken.END_OBJECT, "Expected field name."); } else { String name = jp.getCurrentName(); if ("items".equals(name)) { // the actual list items. nextToken = jp.nextToken(); if (nextToken == JsonToken.VALUE_EMBEDDED_OBJECT) { Object o = jp.getEmbeddedObject(); if (o instanceof List) { // could it be an array, also?!? // when using JsonTree, sometimes Jackson just sticks the entire Java object in here. items.addAll((List) o); } else { throw ctxt.mappingException( "List contains VALUE_EMBEDDED_OBJECT for items, but it wasn't a list."); } } else if (nextToken != JsonToken.START_ARRAY) { // what about nothing? throw ctxt.wrongTokenException(jp, JsonToken.START_ARRAY, "Expected array of items"); } else { // the START_ARRAY case, which is _normal_. Read the elements. while (jp.nextToken() != JsonToken.END_ARRAY) { items.add(jp.readValueAs(itemClass)); } } } else { nextToken = jp.nextToken(); Object value; if (nextToken == JsonToken.VALUE_EMBEDDED_OBJECT) { value = jp.getEmbeddedObject(); } else { value = jp.readValueAs(Object.class); } builder.extendedProperty(name, value); } } } builder.setItems(items); return builder.build(); }
From source file:org.jsfr.json.JacksonParserTest.java
@Test public void testLargeJsonJackson() throws Exception { final AtomicLong counter = new AtomicLong(); ObjectMapper om = new ObjectMapper(); JsonFactory f = new JsonFactory(); JsonParser jp = f.createParser(read("allthethings.json")); long start = System.currentTimeMillis(); jp.nextToken(); jp.nextToken();/*from w w w . j a va2s . c om*/ jp.nextToken(); while (jp.nextToken() == JsonToken.FIELD_NAME) { if (jp.nextToken() == JsonToken.START_OBJECT) { TreeNode tree = om.readTree(jp); counter.incrementAndGet(); LOGGER.trace("value: {}", tree); } } jp.close(); LOGGER.info("Jackson processes {} value in {} millisecond", counter.get(), System.currentTimeMillis() - start); }
From source file:com.pursuer.reader.easyrss.data.parser.TagJSONParser.java
public void parse() throws JsonParseException, IOException, IllegalStateException { final JsonFactory factory = new JsonFactory(); final JsonParser parser = factory.createJsonParser(input); Tag tag = new Tag(); int level = 0; boolean found = false; while (parser.nextToken() != null) { final String name = parser.getCurrentName(); switch (parser.getCurrentToken()) { case START_OBJECT: case START_ARRAY: level++;//from w w w. j a v a 2 s .co m break; case END_OBJECT: case END_ARRAY: level--; break; case VALUE_STRING: if (level == 3) { if ("id".equals(name)) { tag.setUid(parser.getText()); } else if ("sortid".equals(name)) { tag.setSortId(parser.getText()); } } case FIELD_NAME: if (level == 1 && "tags".equals(name)) { found = true; } default: } if (level == 2) { if (tag.getUid() != null && listener != null) { listener.onTagRetrieved(tag); } tag = new Tag(); } } parser.close(); if (!found) { throw new IllegalStateException("Invalid JSON input"); } }