List of usage examples for com.fasterxml.jackson.core JsonParser readValueAsTree
@SuppressWarnings("unchecked") public <T extends TreeNode> T readValueAsTree() throws IOException, JsonProcessingException
From source file:com.amazon.speech.json.ContextDeserializer.java
@Override public Context deserialize(JsonParser parser, DeserializationContext context) throws IOException { ObjectMapper underlyingMapper = ((ObjectMapper) parser.getCodec()); JsonNode messageNode = parser.readValueAsTree(); Context.Builder contextBuilder = Context.builder(); for (SerializedInterface interfaceName : SerializedInterface.values()) { if (messageNode.has(interfaceName.name())) { State<?> state = underlyingMapper.convertValue(messageNode.get(interfaceName.name()), interfaceName.getStateClass()); contextBuilder.addState(state); }/* w w w. j a v a 2 s.c o m*/ } return contextBuilder.build(); }
From source file:com.amazon.speech.json.SupportedInterfacesDeserializer.java
@Override public SupportedInterfaces deserialize(JsonParser parser, DeserializationContext context) throws IOException { ObjectMapper underlyingMapper = ((ObjectMapper) parser.getCodec()); JsonNode messageNode = parser.readValueAsTree(); SupportedInterfaces.Builder supportedInterfacesBuilder = SupportedInterfaces.builder(); for (SerializedInterface interfaceName : SerializedInterface.values()) { if (messageNode.has(interfaceName.name())) { Interface supportedInterface = underlyingMapper.convertValue(messageNode.get(interfaceName.name()), interfaceName.getInterfaceClass()); supportedInterfacesBuilder.addSupportedInterface(supportedInterface); }//from ww w.java2 s .c o m } return supportedInterfacesBuilder.build(); }
From source file:org.mongojack.TestCustomObjectMapper.java
private ObjectMapper createObjectMapper() { SimpleModule module = new SimpleModule("MySimpleModule", new Version(1, 0, 0, null, "", "")); module.addDeserializer(Custom.class, new JsonDeserializer<Custom>() { @Override/* www.j a v a2 s . c o m*/ public Custom deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { JsonNode node = jp.readValueAsTree(); return new Custom(node.get("v1").asText(), node.get("v2").asText()); } }); module.addSerializer(Custom.class, new JsonSerializer<Custom>() { @Override public void serialize(Custom value, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeStartObject(); jgen.writeFieldName("v1"); jgen.writeString(value.value1); jgen.writeFieldName("v2"); jgen.writeString(value.value2); jgen.writeEndObject(); } }); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.registerModule(module); MongoJackModule.configure(objectMapper); return objectMapper; }
From source file:alpine.json.TrimmedStringArrayDeserializer.java
@Override public String[] deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException { final List<String> list = new ArrayList<>(); final JsonNode node = jsonParser.readValueAsTree(); if (node.isArray()) { final Iterator elements = node.elements(); while (elements.hasNext()) { final JsonNode childNode = (JsonNode) elements.next(); final String value = StringUtils.trimToNull(childNode.asText()); if (value != null) { list.add(value);//from w w w. j ava 2 s . co m } } } if (list.size() == 0) { return null; } else { return list.toArray(new String[list.size()]); } }
From source file:com.github.jonpeterson.jackson.module.versioning.VersionedModelDeserializer.java
@Override public T deserialize(JsonParser parser, DeserializationContext context) throws IOException { JsonNode jsonNode = parser.readValueAsTree(); if (!(jsonNode instanceof ObjectNode)) throw context.mappingException("value must be a JSON object"); ObjectNode modelData = (ObjectNode) jsonNode; JsonNode modelVersionNode = modelData.remove(jsonVersionedModel.propertyName()); String modelVersion = null;// ww w. j a v a 2s. com if (modelVersionNode != null) modelVersion = modelVersionNode.asText(); if (modelVersion == null) modelVersion = jsonVersionedModel.defaultDeserializeToVersion(); if (modelVersion.isEmpty()) throw context.mappingException("'" + jsonVersionedModel.propertyName() + "' property was null and defaultDeserializeToVersion was not set"); // convert the model if converter specified and model needs converting if (converter != null && (jsonVersionedModel.alwaysConvert() || !modelVersion.equals(jsonVersionedModel.currentVersion()))) modelData = converter.convert(modelData, modelVersion, jsonVersionedModel.currentVersion(), context.getNodeFactory()); // set the serializeToVersionProperty value to the source model version if the defaultToSource property is true if (serializeToVersionAnnotation != null && serializeToVersionAnnotation.defaultToSource()) modelData.put(serializeToVersionProperty.getName(), modelVersion); JsonParser postInterceptionParser = new TreeTraversingParser(modelData, parser.getCodec()); postInterceptionParser.nextToken(); return delegate.deserialize(postInterceptionParser, context); }
From source file:com.ga2sa.utils.ArrayToStringDeserializer.java
@Override public String deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException, JsonProcessingException { List<String> array = new ArrayList<String>(); JsonNode tree = jsonParser.readValueAsTree(); if (tree.isArray()) { tree.forEach(obj -> array.add(obj.textValue())); } else {//from w ww .j av a2 s . c o m array.add(tree.textValue()); } return StringUtils.join(array, ","); }
From source file:org.agorava.twitter.jackson.TweetDeserializer.java
@Override public Tweet deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonNode tree = jp.readValueAsTree(); long id = tree.get("id").asLong(); String text = tree.get("text").asText(); JsonNode fromUserNode = tree.get("user"); String fromScreenName = null; long fromId = 0; String fromImageUrl = null;/*from w ww.ja v a2s. c om*/ String dateFormat = TIMELINE_DATE_FORMAT; if (fromUserNode != null) { fromScreenName = fromUserNode.get("screen_name").asText(); fromId = fromUserNode.get("id").asLong(); fromImageUrl = fromUserNode.get("profile_image_url").asText(); } else { fromScreenName = tree.get("from_user").asText(); fromId = tree.get("from_user_id").asLong(); fromImageUrl = tree.get("profile_image_url").asText(); dateFormat = SEARCH_DATE_FORMAT; } Date createdAt = toDate(tree.get("created_at").asText(), new SimpleDateFormat(dateFormat, Locale.ENGLISH)); String source = tree.get("source").asText(); JsonNode toUserIdNode = tree.get("in_reply_to_user_id"); Long toUserId = toUserIdNode != null ? toUserIdNode.longValue() : null; JsonNode languageCodeNode = tree.get("iso_language_code"); String languageCode = languageCodeNode != null ? languageCodeNode.asText() : null; Tweet tweet = new Tweet(id, text, createdAt, fromScreenName, fromImageUrl, toUserId, fromId, languageCode, source); JsonNode inReplyToStatusIdNode = tree.get("in_reply_to_status_id"); Long inReplyToStatusId = inReplyToStatusIdNode != null && !inReplyToStatusIdNode.isNull() ? inReplyToStatusIdNode.longValue() : null; tweet.setInReplyToStatusId(inReplyToStatusId); JsonNode retweetCountNode = tree.get("retweet_count"); Integer retweetCount = retweetCountNode != null && !retweetCountNode.isNull() ? retweetCountNode.intValue() : null; tweet.setRetweetCount(retweetCount); JsonNode favoritedNode = tree.get("favorited"); boolean favorited = favoritedNode != null && !favoritedNode.isNull() ? favoritedNode.booleanValue() : false; tweet.setFavorited(favorited); jp.skipChildren(); return tweet; }
From source file:org.thingsplode.synapse.serializers.jackson.adapters.ParameterWrapperDeserializer.java
@Override public ParameterWrapper deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonNode node = jp.readValueAsTree(); if (node != null && node.size() > 0 && node.isContainerNode()) { ParameterWrapper pw = ParameterWrapper.create(); ArrayNode paramsNode = (ArrayNode) node.get("params"); Iterator<JsonNode> elemIterator = paramsNode.elements(); while (elemIterator.hasNext()) { JsonNode currentNode = elemIterator.next(); if (currentNode.getNodeType() == JsonNodeType.OBJECT) { try { String paramid = ((ObjectNode) currentNode).get("paramid").asText(); String typeName = ((ObjectNode) currentNode).get("type").asText(); Class paramType = null; if (null != typeName) switch (typeName) { case "long": paramType = Long.TYPE; break; case "byte": paramType = Byte.TYPE; break; case "short": paramType = Short.TYPE; break; case "int": paramType = Integer.TYPE; break; case "float": paramType = Float.TYPE; break; case "double": paramType = Double.TYPE; break; case "boolean": paramType = Boolean.TYPE; break; case "char": paramType = Character.TYPE; break; default: paramType = Class.forName(typeName); break; }// w w w . j av a 2 s . c o m Object parameterObject = jp.getCodec().treeToValue(currentNode.get("value"), paramType); pw.add(paramid, paramType, parameterObject); } catch (ClassNotFoundException ex) { throw new JsonParseException(jp, ex.getMessage()); } } } return pw; } else { return null; } }
From source file:com.github.tomakehurst.wiremock.matching.StringValuePatternJsonDeserializer.java
@Override public StringValuePattern deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException { JsonNode rootNode = parser.readValueAsTree(); if (isAbsent(rootNode)) { return StringValuePattern.ABSENT; }/*from w w w .j a v a2 s . c o m*/ Class<? extends StringValuePattern> patternClass = findPatternClass(rootNode); if (patternClass.equals(EqualToJsonPattern.class)) { return deserializeEqualToJson(rootNode); } else if (patternClass.equals(MatchesXPathPattern.class)) { return deserialiseMatchesXPathPattern(rootNode); } Constructor<? extends StringValuePattern> constructor = findConstructor(patternClass); Map.Entry<String, JsonNode> entry = rootNode.fields().next(); String operand = entry.getValue().textValue(); try { return constructor.newInstance(operand); } catch (Exception e) { return throwUnchecked(e, StringValuePattern.class); } }
From source file:com.strandls.alchemy.rest.client.exception.ExceptionPayloadDeserializer.java
@SuppressWarnings("unchecked") @Override/*from w w w . ja v a2s. co m*/ public ExceptionPayload deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException { final ObjectMapper sourceObjectMapper = ((ObjectMapper) jp.getCodec()); final ObjectNode tree = jp.readValueAsTree(); String message = null; if (tree.has("exceptionMessage")) { message = tree.get("exceptionMessage").asText(); } Throwable exception = null; try { final String className = tree.get("exceptionClassFQN").asText(); final Class<? extends Throwable> clazz = (Class<? extends Throwable>) ReflectionUtils .forName(className); exception = sourceObjectMapper.treeToValue(tree.get("exception"), clazz); } catch (final Throwable t) { log.warn("Error deserializing exception class", t); exception = new InternalServerErrorException(message); } return new ExceptionPayload(exception.getClass().getName(), message, exception); }