List of usage examples for com.fasterxml.jackson.core JsonParser getValueAsLong
public long getValueAsLong() throws IOException, JsonParseException
From source file:org.ng200.openolympus.DurationJacksonModule.java
public DurationJacksonModule() { addSerializer(new JsonSerializer<Duration>() { @Override/*from w w w.ja v a2 s . c o m*/ public void serialize(Duration duration, JsonGenerator gen, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { gen.writeNumber(duration.toMillis()); } @Override public Class<Duration> handledType() { return Duration.class; } }); addDeserializer(Duration.class, new JsonDeserializer<Duration>() { @Override public Duration deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { long l = jp.getValueAsLong(); return Duration.ofMillis(l); } @Override public Class<Duration> handledType() { return Duration.class; } }); }
From source file:org.springframework.data.couchbase.core.convert.translation.JacksonTranslationService.java
/** * Helper method to decode and assign a primitive. * * @param token the type of token./*w w w . j a va 2 s . co m*/ * @param parser the parser with the content. * * @return the decoded primitve. * * @throws IOException */ private Object decodePrimitive(final JsonToken token, final JsonParser parser) throws IOException { switch (token) { case VALUE_TRUE: case VALUE_FALSE: return parser.getValueAsBoolean(); case VALUE_STRING: return parser.getValueAsString(); case VALUE_NUMBER_INT: try { return parser.getValueAsInt(); } catch (final JsonParseException e) { return parser.getValueAsLong(); } case VALUE_NUMBER_FLOAT: return parser.getValueAsDouble(); case VALUE_NULL: return null; default: throw new MappingException("Could not decode primitve value " + token); } }
From source file:com.adobe.communities.ugc.migration.importer.UGCImportHelper.java
public static void extractTally(final Resource post, final JsonParser jsonParser, final ModifyingResourceProvider srp, final TallyOperationsService tallyOperationsService) throws IOException { jsonParser.nextToken(); // should be start object, but would be end array if no objects were present while (!jsonParser.getCurrentToken().equals(JsonToken.END_ARRAY)) { Long timestamp = null;//w ww. j a va 2s . c o m String userIdentifier = null; String response = null; String tallyType = null; jsonParser.nextToken(); // should make current token by "FIELD_NAME" but could be END_OBJECT if this were // an empty object while (!jsonParser.getCurrentToken().equals(JsonToken.END_OBJECT)) { final String label = jsonParser.getCurrentName(); jsonParser.nextToken(); // should be FIELD_VALUE if (label.equals(TallyConstants.TIMESTAMP_PROPERTY)) { timestamp = jsonParser.getValueAsLong(); } else { final String responseValue = jsonParser.getValueAsString(); if (label.equals("response")) { response = URLDecoder.decode(responseValue, "UTF-8"); } else if (label.equals("userIdentifier")) { userIdentifier = URLDecoder.decode(responseValue, "UTF-8"); } else if (label.equals("tallyType")) { tallyType = responseValue; } } jsonParser.nextToken(); // should make current token be "FIELD_NAME" unless we're at the end of our // loop and it's now "END_OBJECT" instead } if (timestamp != null && userIdentifier != null && response != null && tallyType != null) { createTally(srp, post, tallyType, userIdentifier, timestamp, response, tallyOperationsService); } jsonParser.nextToken(); // may advance to "START_OBJECT" if we're not finished yet, but might be // "END_ARRAY" now } }
From source file:com.adobe.communities.ugc.migration.importer.UGCImportHelper.java
public static Map<String, Object> extractSubmap(final JsonParser jsonParser) throws IOException { jsonParser.nextToken(); // skip the START_OBJECT token final Map<String, Object> subMap = new HashMap<String, Object>(); while (!jsonParser.getCurrentToken().equals(JsonToken.END_OBJECT)) { final String label = jsonParser.getCurrentName(); // get the current label final JsonToken token = jsonParser.nextToken(); // get the current value if (!token.isScalarValue()) { if (token.equals(JsonToken.START_OBJECT)) { // if the next token starts a new object, recurse into it subMap.put(label, extractSubmap(jsonParser)); } else if (token.equals(JsonToken.START_ARRAY)) { final List<String> subArray = new ArrayList<String>(); jsonParser.nextToken(); // skip the START_ARRAY token while (!jsonParser.getCurrentToken().equals(JsonToken.END_ARRAY)) { subArray.add(jsonParser.getValueAsString()); jsonParser.nextToken(); }// www.j av a2 s .co m subMap.put(label, subArray); jsonParser.nextToken(); // skip the END_ARRAY token } } else { // either a string, boolean, or long value if (token.isNumeric()) { subMap.put(label, jsonParser.getValueAsLong()); } else { final String value = jsonParser.getValueAsString(); if (value.equals("true") || value.equals("false")) { subMap.put(label, jsonParser.getValueAsBoolean()); } else { subMap.put(label, value); } } } jsonParser.nextToken(); // next token will either be an "END_OBJECT" or a new label } jsonParser.nextToken(); // skip the END_OBJECT token return subMap; }
From source file:com.adobe.communities.ugc.migration.importer.UGCImportHelper.java
protected void extractEvent(final JsonParser jsonParser, final Resource resource) throws IOException { String author = null;//from w ww .j ava2 s .co m final JSONObject requestParams = new JSONObject(); try { jsonParser.nextToken(); JsonToken token = jsonParser.getCurrentToken(); while (token.equals(JsonToken.FIELD_NAME)) { String field = jsonParser.getCurrentName(); jsonParser.nextToken(); if (field.equals(PN_START) || field.equals(PN_END)) { final Long value = jsonParser.getValueAsLong(); final Calendar calendar = new GregorianCalendar(); calendar.setTimeInMillis(value); final Date date = calendar.getTime(); final TimeZone tz = TimeZone.getTimeZone("UTC"); // this is the ISO-8601 format expected by the CalendarOperations object final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm+00:00"); df.setTimeZone(tz); if (field.equals(PN_START)) { requestParams.put(CalendarRequestConstants.START_DATE, df.format(date)); } else { requestParams.put(CalendarRequestConstants.END_DATE, df.format(date)); } } else if (field.equals(CalendarRequestConstants.TAGS)) { List<String> tags = new ArrayList<String>(); if (jsonParser.getCurrentToken().equals(JsonToken.START_ARRAY)) { token = jsonParser.nextToken(); while (!token.equals(JsonToken.END_ARRAY)) { tags.add(URLDecoder.decode(jsonParser.getValueAsString(), "UTF-8")); token = jsonParser.nextToken(); } requestParams.put(CalendarRequestConstants.TAGS, tags); } else { LOG.warn("Tags field came in without an array of tags in it - not processed"); // do nothing, just log the error } } else if (field.equals("jcr:createdBy")) { author = URLDecoder.decode(jsonParser.getValueAsString(), "UTF-8"); } else if (field.equals(ContentTypeDefinitions.LABEL_TIMESTAMP_FIELDS)) { jsonParser.skipChildren(); // we do nothing with these because they're going to be put into json } else { try { final String value = URLDecoder.decode(jsonParser.getValueAsString(), "UTF-8"); requestParams.put(field, value); } catch (NullPointerException e) { throw e; } } token = jsonParser.nextToken(); } } catch (final JSONException e) { throw new IOException("Unable to build a JSON object with the inputs provided", e); } try { Map<String, Object> eventParams = new HashMap<String, Object>(); eventParams.put("event", requestParams.toString()); calendarOperations.create(resource, author, eventParams, null, resource.getResourceResolver().adaptTo(Session.class)); } catch (final OperationException e) { //probably caused by creating a folder that already exists. We ignore it, but still log the event. LOG.info("There was an operation exception while creating an event"); } }
From source file:org.quantumbadger.redreader.jsonwrap.JsonValue.java
protected JsonValue(final JsonParser jp, final JsonToken firstToken) throws IOException { switch (firstToken) { case START_OBJECT: type = TYPE_OBJECT;//from w w w .j a v a2s. c o m value = new JsonBufferedObject(); this.jp = jp; break; case START_ARRAY: type = TYPE_ARRAY; value = new JsonBufferedArray(); this.jp = jp; break; case VALUE_FALSE: type = TYPE_BOOLEAN; value = false; break; case VALUE_TRUE: type = TYPE_BOOLEAN; value = true; break; case VALUE_NULL: type = TYPE_NULL; value = null; break; case VALUE_STRING: type = TYPE_STRING; value = jp.getValueAsString(); break; case VALUE_NUMBER_FLOAT: //noinspection FloatingPointEquality,UnnecessaryExplicitNumericCast if (jp.getValueAsDouble() == (double) jp.getValueAsLong()) { type = TYPE_INTEGER; value = jp.getValueAsLong(); } else { type = TYPE_FLOAT; value = jp.getValueAsDouble(); } break; case VALUE_NUMBER_INT: type = TYPE_INTEGER; value = jp.getValueAsLong(); break; default: throw new JsonParseException("Expecting an object, literal, or array", jp.getCurrentLocation()); } }
From source file:com.ryan.ryanreader.jsonwrap.JsonValue.java
protected JsonValue(final JsonParser jp, final JsonToken firstToken) throws IOException { switch (firstToken) { case START_OBJECT: type = Type.OBJECT;/*from w w w . j a v a2 s . co m*/ value = new JsonBufferedObject(); this.jp = jp; break; case START_ARRAY: type = Type.ARRAY; value = new JsonBufferedArray(); this.jp = jp; break; case VALUE_FALSE: type = Type.BOOLEAN; value = false; break; case VALUE_TRUE: type = Type.BOOLEAN; value = true; break; case VALUE_NULL: type = Type.NULL; value = null; break; case VALUE_STRING: type = Type.STRING; value = jp.getValueAsString(); break; case VALUE_NUMBER_FLOAT: //noinspection FloatingPointEquality,UnnecessaryExplicitNumericCast if (jp.getValueAsDouble() == (double) jp.getValueAsLong()) { type = Type.INTEGER; value = jp.getValueAsLong(); } else { type = Type.FLOAT; value = jp.getValueAsDouble(); } break; case VALUE_NUMBER_INT: type = Type.INTEGER; value = jp.getValueAsLong(); break; default: throw new JsonParseException("Expecting an object, literal, or array", jp.getCurrentLocation()); } }
From source file:name.gumartinm.weather.information.parser.JPOSCurrentParser.java
private void getCurrentWeatherDataObjects(final Current currentWeatherData, final JsonParser jParser, final String fieldname) throws JsonParseException, IOException { if ("coord".equals(fieldname)) { while (jParser.nextToken() != JsonToken.END_OBJECT) { final String namefield = jParser.getCurrentName(); jParser.nextToken(); // move to value if ("lon".equals(namefield)) { currentWeatherData.getCoord().setLon(jParser.getDoubleValue()); }//w w w . java 2s . com if ("lat".equals(namefield)) { currentWeatherData.getCoord().setLat(jParser.getDoubleValue()); } } } if ("sys".equals(fieldname)) { while (jParser.nextToken() != JsonToken.END_OBJECT) { final String namefield = jParser.getCurrentName(); jParser.nextToken(); // move to value if ("message".equals(namefield)) { currentWeatherData.getSys().setMessage(jParser.getDoubleValue()); } if ("country".equals(namefield)) { currentWeatherData.getSys().setCountry(jParser.getValueAsString()); } if ("sunrise".equals(namefield)) { currentWeatherData.getSys().setSunrise(jParser.getValueAsLong()); } if ("sunset".equals(namefield)) { currentWeatherData.getSys().setSunset(jParser.getValueAsLong()); } } } if ("weather".equals(fieldname)) { final Weather weather = new Weather(); currentWeatherData.getWeather().add(weather); while (jParser.nextToken() != JsonToken.END_OBJECT) { final String namefield = jParser.getCurrentName(); jParser.nextToken(); // move to value if ("id".equals(namefield)) { weather.setId(jParser.getIntValue()); } if ("main".equals(namefield)) { weather.setMain(jParser.getText()); } if ("description".equals(namefield)) { weather.setDescription(jParser.getText()); } if ("icon".equals(namefield)) { weather.setIcon(jParser.getText()); } } } if ("base".equals(fieldname)) { currentWeatherData.setBase(jParser.getText()); } if ("main".equals(fieldname)) { while (jParser.nextToken() != JsonToken.END_OBJECT) { final String namefield = jParser.getCurrentName(); jParser.nextToken(); // move to value if ("temp".equals(namefield)) { currentWeatherData.getMain().setTemp(jParser.getDoubleValue()); } if ("temp_min".equals(namefield)) { currentWeatherData.getMain().setTemp_min(jParser.getDoubleValue()); } if ("temp_max".equals(namefield)) { currentWeatherData.getMain().setTemp_max(jParser.getDoubleValue()); } if ("pressure".equals(namefield)) { currentWeatherData.getMain().setPressure(jParser.getDoubleValue()); } if ("sea_level".equals(namefield)) { currentWeatherData.getMain().setSea_level(jParser.getDoubleValue()); } if ("grnd_level".equals(namefield)) { currentWeatherData.getMain().setGrnd_level(jParser.getDoubleValue()); } if ("humidity".equals(namefield)) { currentWeatherData.getMain().setHumidity(jParser.getDoubleValue()); } } } if ("wind".equals(fieldname)) { while (jParser.nextToken() != JsonToken.END_OBJECT) { final String namefield = jParser.getCurrentName(); jParser.nextToken(); // move to value if ("speed".equals(namefield)) { currentWeatherData.getWind().setSpeed(jParser.getDoubleValue()); } if ("deg".equals(namefield)) { currentWeatherData.getWind().setDeg(jParser.getDoubleValue()); } } } if ("clouds".equals(fieldname)) { while (jParser.nextToken() != JsonToken.END_OBJECT) { final String namefield = jParser.getCurrentName(); jParser.nextToken(); // move to value if ("all".equals(namefield)) { currentWeatherData.getClouds().setAll(jParser.getDoubleValue()); } } } if ("dt".equals(fieldname)) { currentWeatherData.setDt(jParser.getLongValue()); } if ("rain".equals(fieldname)) { while (jParser.nextToken() != JsonToken.END_OBJECT) { final String namefield = jParser.getCurrentName(); jParser.nextToken(); // move to value if ("3h".equals(namefield)) { currentWeatherData.getRain().set3h(jParser.getDoubleValue()); } } } if ("snow".equals(fieldname)) { while (jParser.nextToken() != JsonToken.END_OBJECT) { final String namefield = jParser.getCurrentName(); jParser.nextToken(); // move to value if ("3h".equals(namefield)) { currentWeatherData.getSnow().set3h(jParser.getDoubleValue()); } } } if ("id".equals(fieldname)) { currentWeatherData.setId(jParser.getLongValue()); } if ("name".equals(fieldname)) { currentWeatherData.setName(jParser.getText()); } if ("cod".equals(fieldname)) { currentWeatherData.setCod(jParser.getIntValue()); } }
From source file:invar.lib.data.DataParserJson.java
private void parse(JsonParser parser, DataNode root) throws IOException { String fieldName = null;// w ww. j a va2 s. c o m DataNode parent = root; while (!parser.isClosed()) { JsonToken token = parser.nextToken(); if (token == null) { continue; } switch (token) { case START_ARRAY: parent.addChild(parent = DataNode.createArray().setFieldName(fieldName)); fieldName = null; break; case END_ARRAY: parent = parent.getParent(); fieldName = null; break; case START_OBJECT: parent.addChild(parent = DataNode.createObject().setFieldName(fieldName)); fieldName = null; break; case END_OBJECT: parent = parent.getParent(); fieldName = null; break; case VALUE_TRUE: parent.addChild(DataNode.createBoolean().setValue(true).setFieldName(fieldName)); fieldName = null; break; case VALUE_FALSE: parent.addChild(DataNode.createBoolean().setValue(false).setFieldName(fieldName)); fieldName = null; break; case VALUE_NULL: parent.addChild(DataNode.createNull().setValue(null).setFieldName(fieldName)); fieldName = null; break; case VALUE_STRING: parent.addChild( DataNode.createString().setFieldName(fieldName).setValue(parser.getValueAsString())); fieldName = null; break; case VALUE_NUMBER_INT: try { Long v = parser.getValueAsLong(); parent.addChild(DataNode.createLong().setFieldName(fieldName).setValue(v)); } catch (JsonParseException e) { BigInteger v = parser.getBigIntegerValue(); parent.addChild(DataNode.createBigInt().setFieldName(fieldName).setValue(v)); } fieldName = null; break; case VALUE_NUMBER_FLOAT: parent.addChild( DataNode.createDouble().setFieldName(fieldName).setValue(parser.getValueAsDouble())); fieldName = null; break; case FIELD_NAME: fieldName = parser.getValueAsString(); break; default: fieldName = null; break; } } }
From source file:com.adobe.communities.ugc.migration.importer.UGCImportHelper.java
protected void extractTopic(final JsonParser jsonParser, final Resource resource, final ResourceResolver resolver, final CommentOperations operations) throws IOException, ServletException { if (jsonParser.getCurrentToken().equals(JsonToken.END_OBJECT)) { return; // replies could just be an empty object (i.e. "ugc:replies":{} ) in which case, do nothing }/*w w w .j a va2 s . com*/ final Map<String, Object> properties = new HashMap<String, Object>(); properties.put("social:key", jsonParser.getCurrentName()); Resource post = null; jsonParser.nextToken(); if (jsonParser.getCurrentToken().equals(JsonToken.START_OBJECT)) { jsonParser.nextToken(); String author = null; List<DataSource> attachments = new ArrayList<DataSource>(); while (!jsonParser.getCurrentToken().equals(JsonToken.END_OBJECT)) { final String label = jsonParser.getCurrentName(); JsonToken token = jsonParser.nextToken(); if (jsonParser.getCurrentToken().isScalarValue()) { // either a string, boolean, or long value if (token.isNumeric()) { properties.put(label, jsonParser.getValueAsLong()); } else { final String value = jsonParser.getValueAsString(); if (value.equals("true") || value.equals("false")) { properties.put(label, jsonParser.getValueAsBoolean()); } else { final String decodedValue = URLDecoder.decode(value, "UTF-8"); if (label.equals("language")) { properties.put("mtlanguage", decodedValue); } else { properties.put(label, decodedValue); if (label.equals("userIdentifier")) { author = decodedValue; } else if (label.equals("jcr:description")) { properties.put("message", decodedValue); } } } } } else if (label.equals(ContentTypeDefinitions.LABEL_ATTACHMENTS)) { attachments = getAttachments(jsonParser); } else if (label.equals(ContentTypeDefinitions.LABEL_REPLIES) || label.equals(ContentTypeDefinitions.LABEL_TALLY) || label.equals(ContentTypeDefinitions.LABEL_TRANSLATION) || label.equals(ContentTypeDefinitions.LABEL_SUBNODES)) { // replies and sub-nodes ALWAYS come after all other properties and attachments have been listed, // so we can create the post now if we haven't already, and then dive in if (post == null) { try { post = createPost(resource, author, properties, attachments, resolver.adaptTo(Session.class), operations); resProvider = SocialResourceUtils.getSocialResource(post).getResourceProvider(); } catch (Exception e) { throw new ServletException(e.getMessage(), e); } } if (label.equals(ContentTypeDefinitions.LABEL_REPLIES)) { if (token.equals(JsonToken.START_OBJECT)) { jsonParser.nextToken(); while (!token.equals(JsonToken.END_OBJECT)) { extractTopic(jsonParser, post, resolver, operations); token = jsonParser.nextToken(); } } else { throw new IOException("Expected an object for the subnodes"); } } else if (label.equals(ContentTypeDefinitions.LABEL_SUBNODES)) { if (token.equals(JsonToken.START_OBJECT)) { token = jsonParser.nextToken(); try { while (!token.equals(JsonToken.END_OBJECT)) { final String subnodeType = jsonParser.getCurrentName(); token = jsonParser.nextToken(); if (token.equals(JsonToken.START_OBJECT)) { jsonParser.skipChildren(); token = jsonParser.nextToken(); } } } catch (final IOException e) { throw new IOException("unable to skip child of sub-nodes", e); } } else { final String field = jsonParser.getValueAsString(); throw new IOException("Expected an object for the subnodes. Instead: " + field); } } else if (label.equals(ContentTypeDefinitions.LABEL_TALLY)) { UGCImportHelper.extractTally(post, jsonParser, resProvider, tallyOperationsService); } else if (label.equals(ContentTypeDefinitions.LABEL_TRANSLATION)) { importTranslation(jsonParser, post); resProvider.commit(post.getResourceResolver()); } } else if (jsonParser.getCurrentToken().equals(JsonToken.START_OBJECT)) { properties.put(label, UGCImportHelper.extractSubmap(jsonParser)); } else if (jsonParser.getCurrentToken().equals(JsonToken.START_ARRAY)) { jsonParser.nextToken(); // skip the START_ARRAY token if (label.equals(ContentTypeDefinitions.LABEL_TIMESTAMP_FIELDS)) { while (!jsonParser.getCurrentToken().equals(JsonToken.END_ARRAY)) { final String timestampLabel = jsonParser.getValueAsString(); if (properties.containsKey(timestampLabel) && properties.get(timestampLabel) instanceof Long) { final Calendar calendar = new GregorianCalendar(); calendar.setTimeInMillis((Long) properties.get(timestampLabel)); properties.put(timestampLabel, calendar.getTime()); } jsonParser.nextToken(); } } else { final List<String> subArray = new ArrayList<String>(); while (!jsonParser.getCurrentToken().equals(JsonToken.END_ARRAY)) { subArray.add(jsonParser.getValueAsString()); jsonParser.nextToken(); } String[] strings = new String[subArray.size()]; for (int i = 0; i < subArray.size(); i++) { strings[i] = subArray.get(i); } properties.put(label, strings); } } jsonParser.nextToken(); } if (post == null) { try { post = createPost(resource, author, properties, attachments, resolver.adaptTo(Session.class), operations); if (null == resProvider) { resProvider = SocialResourceUtils.getSocialResource(post).getResourceProvider(); } // resProvider.commit(resolver); } catch (Exception e) { throw new ServletException(e.getMessage(), e); } } } else { throw new IOException("Improperly formed JSON - expected an OBJECT_START token, but got " + jsonParser.getCurrentToken().toString()); } }