List of usage examples for com.fasterxml.jackson.core JsonParser getCodec
public abstract ObjectCodec getCodec();
From source file:org.apache.olingo.client.core.serialization.JsonPropertyDeserializer.java
protected ResWrap<Property> doDeserialize(final JsonParser parser) throws IOException { final ObjectNode tree = parser.getCodec().readTree(parser); final String metadataETag; final URI contextURL; final Property property = new Property(); if (tree.hasNonNull(Constants.JSON_METADATA_ETAG)) { metadataETag = tree.get(Constants.JSON_METADATA_ETAG).textValue(); tree.remove(Constants.JSON_METADATA_ETAG); } else {/*w w w. j a va2s . c o m*/ metadataETag = null; } if (tree.hasNonNull(Constants.JSON_CONTEXT)) { contextURL = URI.create(tree.get(Constants.JSON_CONTEXT).textValue()); property.setName(StringUtils.substringAfterLast(contextURL.toASCIIString(), "/")); tree.remove(Constants.JSON_CONTEXT); } else if (tree.hasNonNull(Constants.JSON_METADATA)) { contextURL = URI.create(tree.get(Constants.JSON_METADATA).textValue()); property.setType(new EdmTypeInfo.Builder() .setTypeExpression(StringUtils.substringAfterLast(contextURL.toASCIIString(), "#")).build() .internal()); tree.remove(Constants.JSON_METADATA); } else { contextURL = null; } if (tree.has(Constants.JSON_TYPE)) { property.setType(new EdmTypeInfo.Builder().setTypeExpression(tree.get(Constants.JSON_TYPE).textValue()) .build().internal()); tree.remove(Constants.JSON_TYPE); } if (tree.has(Constants.JSON_NULL) && tree.get(Constants.JSON_NULL).asBoolean()) { property.setValue(ValueType.PRIMITIVE, null); tree.remove(Constants.JSON_NULL); } if (property.getValue() == null) { try { value(property, tree.has(Constants.VALUE) ? tree.get(Constants.VALUE) : tree, parser.getCodec()); } catch (final EdmPrimitiveTypeException e) { throw new IOException(e); } tree.remove(Constants.VALUE); } Set<String> toRemove = new HashSet<String>(); // any remaining entry is supposed to be an annotation or is ignored for (final Iterator<Map.Entry<String, JsonNode>> itor = tree.fields(); itor.hasNext();) { final Map.Entry<String, JsonNode> field = itor.next(); if (field.getKey().charAt(0) == '@') { final Annotation annotation = new Annotation(); annotation.setTerm(field.getKey().substring(1)); try { value(annotation, field.getValue(), parser.getCodec()); } catch (final EdmPrimitiveTypeException e) { throw new IOException(e); } property.getAnnotations().add(annotation); } else if (field.getKey().charAt(0) == '#') { final Operation operation = new Operation(); operation.setMetadataAnchor(field.getKey()); final ObjectNode opNode = (ObjectNode) tree.get(field.getKey()); operation.setTitle(opNode.get(Constants.ATTR_TITLE).asText()); operation.setTarget(URI.create(opNode.get(Constants.ATTR_TARGET).asText())); property.getOperations().add(operation); toRemove.add(field.getKey()); } } tree.remove(toRemove); return new ResWrap<Property>(contextURL, metadataETag, property); }
From source file:org.apache.nifi.registry.client.impl.BucketItemDeserializer.java
@Override public BucketItem[] deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { final JsonNode arrayNode = jsonParser.getCodec().readTree(jsonParser); final List<BucketItem> bucketItems = new ArrayList<>(); final Iterator<JsonNode> nodeIter = arrayNode.elements(); while (nodeIter.hasNext()) { final JsonNode node = nodeIter.next(); final String type = node.get("type").asText(); if (StringUtils.isBlank(type)) { throw new IllegalStateException("BucketItem type cannot be null or blank"); }// w w w . j av a 2 s. co m final BucketItemType bucketItemType; try { bucketItemType = BucketItemType.valueOf(type); } catch (Exception e) { throw new IllegalStateException("Unknown type for BucketItem: " + type, e); } switch (bucketItemType) { case Flow: final VersionedFlow versionedFlow = jsonParser.getCodec().treeToValue(node, VersionedFlow.class); bucketItems.add(versionedFlow); break; case Extension_Bundle: final ExtensionBundle extensionBundle = jsonParser.getCodec().treeToValue(node, ExtensionBundle.class); bucketItems.add(extensionBundle); break; default: throw new IllegalStateException("Unknown type for BucketItem: " + bucketItemType); } } return bucketItems.toArray(new BucketItem[bucketItems.size()]); }
From source file:com.youtube.serializer.YoutubeVideoDeserializer.java
/** * Because the Youtube Video object contains complex objects within its hierarchy, we have to use * a custom deserializer//from w ww .jav a 2 s . co m * * @param jsonParser * @param deserializationContext * @return The deserialized {@link com.google.api.services.youtube.YouTube.Videos} object * @throws java.io.IOException * @throws com.fasterxml.jackson.core.JsonProcessingException */ @Override public Video deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { JsonNode node = jsonParser.getCodec().readTree(jsonParser); Video video = new Video(); try { video.setId(node.get("id").asText()); video.setEtag(node.get("etag").asText()); video.setKind(node.get("kind").asText()); video.setSnippet(buildSnippet(node)); video.setStatistics(buildStatistics(node)); } catch (Exception e) { LOGGER.error("Exception while trying to deserialize a Video object: {}", e); } return video; }
From source file:org.apache.olingo.commons.core.serialization.JsonEntitySetDeserializer.java
protected ResWrap<EntitySet> doDeserialize(final JsonParser parser) throws IOException { final ObjectNode tree = (ObjectNode) parser.getCodec().readTree(parser); if (!tree.has(Constants.VALUE)) { return null; }//from w w w . j ava 2 s.c o m final EntitySetImpl entitySet = new EntitySetImpl(); URI contextURL; if (tree.hasNonNull(Constants.JSON_CONTEXT)) { contextURL = URI.create(tree.get(Constants.JSON_CONTEXT).textValue()); tree.remove(Constants.JSON_CONTEXT); } else if (tree.hasNonNull(Constants.JSON_METADATA)) { contextURL = URI.create(tree.get(Constants.JSON_METADATA).textValue()); tree.remove(Constants.JSON_METADATA); } else { contextURL = null; } if (contextURL != null) { entitySet.setBaseURI(StringUtils.substringBefore(contextURL.toASCIIString(), Constants.METADATA)); } final String metadataETag; if (tree.hasNonNull(Constants.JSON_METADATA_ETAG)) { metadataETag = tree.get(Constants.JSON_METADATA_ETAG).textValue(); tree.remove(Constants.JSON_METADATA_ETAG); } else { metadataETag = null; } if (tree.hasNonNull(jsonCount)) { entitySet.setCount(tree.get(jsonCount).asInt()); tree.remove(jsonCount); } if (tree.hasNonNull(jsonNextLink)) { entitySet.setNext(URI.create(tree.get(jsonNextLink).textValue())); tree.remove(jsonNextLink); } if (tree.hasNonNull(jsonDeltaLink)) { entitySet.setDeltaLink(URI.create(tree.get(jsonDeltaLink).textValue())); tree.remove(jsonDeltaLink); } if (tree.hasNonNull(Constants.VALUE)) { final JsonEntityDeserializer entityDeserializer = new JsonEntityDeserializer(version, serverMode); for (JsonNode jsonNode : tree.get(Constants.VALUE)) { entitySet.getEntities() .add(entityDeserializer.doDeserialize(jsonNode.traverse(parser.getCodec())).getPayload()); } tree.remove(Constants.VALUE); } // any remaining entry is supposed to be an annotation or is ignored for (final Iterator<Map.Entry<String, JsonNode>> itor = tree.fields(); itor.hasNext();) { final Map.Entry<String, JsonNode> field = itor.next(); if (field.getKey().charAt(0) == '@') { final Annotation annotation = new AnnotationImpl(); annotation.setTerm(field.getKey().substring(1)); try { value(annotation, field.getValue(), parser.getCodec()); } catch (final EdmPrimitiveTypeException e) { throw new IOException(e); } entitySet.getAnnotations().add(annotation); } } return new ResWrap<EntitySet>(contextURL, metadataETag, entitySet); }
From source file:com.sample.citybikesnyc.BikeStationDeserializer.java
@Override public Object deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException { final BikeStation bikeStation = new BikeStation(); final JsonNode rootNode = jp.getCodec().readTree(jp); final String stationName = rootNode.get("stationName").textValue(); bikeStation.setStationName(stationName); final int availableDocks = (Integer) (rootNode.get("availableDocks")).numberValue(); bikeStation.setAvailableDocks(availableDocks); final int status = (Integer) (rootNode.get("status")).numberValue(); bikeStation.setStatus(BikeStationStatus.valueOf(status)); final ObjectNode locationNode = (ObjectNode) rootNode.get("location"); final JavaType locationType = ctxt.constructType(Location.class); final JsonParser locationNodeParser = jp.getCodec().getFactory().createParser(locationNode.toString()); final Location locationValue = (Location) ctxt.findNonContextualValueDeserializer(locationType) .deserialize(locationNodeParser, ctxt); bikeStation.setLocation(locationValue); return bikeStation; }
From source file:org.mycontroller.standalone.api.jaxrs.mixins.GatewayMixin.java
@Override public GatewayTable deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectCodec objectCodec = jp.getCodec(); JsonNode node = objectCodec.readTree(jp); if (node.get("id") == null) { return null; }/* w w w.j ava 2 s . co m*/ return GatewayTable.builder().id(node.get("id").asInt()).build(); }
From source file:com.google.gplus.serializer.util.GPlusCommentDeserializer.java
/** * Because the GooglePlus Comment object {@link com.google.api.services.plus.model.Comment} contains complex objects * within its hierarchy, we have to use a custom deserializer * * @param jsonParser//from w ww. ja va 2 s. c o m * @param deserializationContext * @return The deserialized {@link com.google.api.services.plus.model.Comment} object * @throws java.io.IOException * @throws com.fasterxml.jackson.core.JsonProcessingException */ @Override public Comment deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { JsonNode node = jsonParser.getCodec().readTree(jsonParser); ObjectMapper objectMapper = StreamsJacksonMapper.getInstance(); Comment comment = new Comment(); try { comment.setEtag(node.get("etag").asText()); comment.setVerb(node.get("verb").asText()); comment.setId(node.get("id").asText()); comment.setPublished(DateTime.parseRfc3339(node.get("published").asText())); comment.setUpdated(DateTime.parseRfc3339(node.get("updated").asText())); Comment.Actor actor = new Comment.Actor(); JsonNode actorNode = node.get("actor"); actor.setDisplayName(actorNode.get("displayName").asText()); actor.setUrl(actorNode.get("url").asText()); Comment.Actor.Image image = new Comment.Actor.Image(); JsonNode imageNode = actorNode.get("image"); image.setUrl(imageNode.get("url").asText()); actor.setImage(image); comment.setObject(objectMapper.readValue(objectMapper.writeValueAsString(node.get("object")), Comment.PlusObject.class)); comment.setSelfLink(node.get("selfLink").asText()); List<Comment.InReplyTo> replies = Lists.newArrayList(); for (JsonNode reply : node.get("inReplyTo")) { Comment.InReplyTo r = objectMapper.readValue(objectMapper.writeValueAsString(reply), Comment.InReplyTo.class); replies.add(r); } comment.setInReplyTo(replies); Comment.Plusoners plusoners = new Comment.Plusoners(); JsonNode plusonersNode = node.get("plusoners"); plusoners.setTotalItems(plusonersNode.get("totalItems").asLong()); comment.setPlusoners(plusoners); } catch (Exception e) { LOGGER.error("Exception while trying to deserialize activity object: {}", e); } return comment; }
From source file:org.dswarm.graph.json.deserializer.ResourceDeserializer.java
@Override public Resource deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException { final ObjectCodec oc = jp.getCodec(); if (oc == null) { return null; }/*from w ww. ja v a 2 s.com*/ final JsonNode node = oc.readTree(jp); if (node == null) { return null; } final Iterator<String> resourceUriFieldNames = node.fieldNames(); if (resourceUriFieldNames == null || !resourceUriFieldNames.hasNext()) { return null; } final String resourceUri = resourceUriFieldNames.next(); if (resourceUri == null) { return null; } final JsonNode resourceNode = node.get(resourceUri); if (resourceNode == null) { return null; } if (!ArrayNode.class.isInstance(resourceNode)) { throw new JsonParseException("expected a JSON array full of statement objects of the resource", jp.getCurrentLocation()); } final Resource resource = new Resource(resourceUri); if (resourceNode.size() <= 0) { return resource; } for (final JsonNode statementNode : resourceNode) { final Statement statement = statementNode.traverse(oc).readValueAs(Statement.class); resource.addStatement(statement); } return resource; }
From source file:org.jasig.portlet.survey.service.dto.ResponseAnswerDtoDeserializer.java
@Override public ResponseAnswerDTO deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException { log.debug("deserializing responseAnswer JSON"); JsonNode node = parser.getCodec().readTree(parser); ResponseAnswerDTO dto = new ResponseAnswerDTO(); JsonNode questionNode = node.get("question"); log.debug(questionNode.toString());/*from ww w . j av a 2 s . c om*/ if (questionNode != null && questionNode.canConvertToLong()) { Long questionId = questionNode.asLong(); dto.setQuestion(questionId); } else { throw new IllegalArgumentException("ResponseAnswer Json missing/bad question field"); } JsonNode answerNode = node.get("answer"); if (answerNode == null) { throw new IllegalArgumentException("ResponseAnswer Json missing answer field"); } if (answerNode.canConvertToLong()) { dto.addAnswerId(answerNode.asLong()); } else if (answerNode.isObject()) { for (Iterator<Entry<String, JsonNode>> fields = answerNode.fields(); fields.hasNext();) { Entry<String, JsonNode> field = fields.next(); Long answerId = Long.parseLong(field.getKey()); assert (field.getValue().isBoolean()); boolean answerSelected = field.getValue().asBoolean(false); if (answerSelected) { dto.addAnswerId(answerId); } } } else { throw new IllegalArgumentException("ResponseAnswer Json bad answer argument field"); } return dto; }
From source file:org.apache.olingo.client.core.serialization.JsonEntitySetDeserializer.java
protected ResWrap<EntityCollection> doDeserialize(final JsonParser parser) throws IOException { final ObjectNode tree = (ObjectNode) parser.getCodec().readTree(parser); if (!tree.has(Constants.VALUE)) { return null; }/* w ww . ja v a 2 s . c o m*/ final EntityCollection entitySet = new EntityCollection(); URI contextURL; if (tree.hasNonNull(Constants.JSON_CONTEXT)) { contextURL = URI.create(tree.get(Constants.JSON_CONTEXT).textValue()); tree.remove(Constants.JSON_CONTEXT); } else if (tree.hasNonNull(Constants.JSON_METADATA)) { contextURL = URI.create(tree.get(Constants.JSON_METADATA).textValue()); tree.remove(Constants.JSON_METADATA); } else { contextURL = null; } if (contextURL != null) { entitySet.setBaseURI( URI.create(StringUtils.substringBefore(contextURL.toASCIIString(), Constants.METADATA))); } final String metadataETag; if (tree.hasNonNull(Constants.JSON_METADATA_ETAG)) { metadataETag = tree.get(Constants.JSON_METADATA_ETAG).textValue(); tree.remove(Constants.JSON_METADATA_ETAG); } else { metadataETag = null; } if (tree.hasNonNull(Constants.JSON_COUNT)) { entitySet.setCount(tree.get(Constants.JSON_COUNT).asInt()); tree.remove(Constants.JSON_COUNT); } if (tree.hasNonNull(Constants.JSON_NEXT_LINK)) { entitySet.setNext(URI.create(tree.get(Constants.JSON_NEXT_LINK).textValue())); tree.remove(Constants.JSON_NEXT_LINK); } if (tree.hasNonNull(Constants.JSON_DELTA_LINK)) { entitySet.setDeltaLink(URI.create(tree.get(Constants.JSON_DELTA_LINK).textValue())); tree.remove(Constants.JSON_DELTA_LINK); } if (tree.hasNonNull(Constants.VALUE)) { final JsonEntityDeserializer entityDeserializer = new JsonEntityDeserializer(serverMode); for (JsonNode jsonNode : tree.get(Constants.VALUE)) { entitySet.getEntities() .add(entityDeserializer.doDeserialize(jsonNode.traverse(parser.getCodec())).getPayload()); } tree.remove(Constants.VALUE); } final Set<String> toRemove = new HashSet<String>(); // any remaining entry is supposed to be an annotation or is ignored for (final Iterator<Map.Entry<String, JsonNode>> itor = tree.fields(); itor.hasNext();) { final Map.Entry<String, JsonNode> field = itor.next(); if (field.getKey().charAt(0) == '@') { final Annotation annotation = new Annotation(); annotation.setTerm(field.getKey().substring(1)); try { value(annotation, field.getValue(), parser.getCodec()); } catch (final EdmPrimitiveTypeException e) { throw new IOException(e); } entitySet.getAnnotations().add(annotation); } else if (field.getKey().charAt(0) == '#') { final Operation operation = new Operation(); operation.setMetadataAnchor(field.getKey()); final ObjectNode opNode = (ObjectNode) tree.get(field.getKey()); operation.setTitle(opNode.get(Constants.ATTR_TITLE).asText()); operation.setTarget(URI.create(opNode.get(Constants.ATTR_TARGET).asText())); entitySet.getOperations().add(operation); toRemove.add(field.getKey()); } } tree.remove(toRemove); return new ResWrap<EntityCollection>(contextURL, metadataETag, entitySet); }