@Override public T next() { init(); try { final T retVal = objectCodec.readValue(jp, typeRef); jp.nextToken(); return retVal; } catch (IOException e) { throw Throwables.propagate(e); } }
/** * Retrieves the next deserialized object from the stream of JSON objects. * * @return the next deserialized object from the stream of JSON ovbjects */ @Override public T next() { if (!hasNext()) { throw new NoSuchElementException("No more objects to read!"); } try { final T retVal = objectCodec.readValue(jp, typeRef); jp.nextToken(); return retVal; } catch (IOException e) { throw new RuntimeException(e); } }
/** * Method to deserialize JSON content into a Java type, reference * to which is passed as argument. Type is passed using so-called * "super type token" * and specifically needs to be used if the root type is a * parameterized (generic) container type. * <b>Note</b>: method can only be called if the parser has * an object codec assigned; this is true for parsers constructed * by <code>MappingJsonFactory</code> (defined in 'jackson-databind' bundle) * but not for {@link JsonFactory} (unless its <code>setCodec</code> * method has been explicitly called). *<p> * This method may advance the event stream, for structured types * the current token will be the closing end marker (END_ARRAY, * END_OBJECT) of the bound structure. For non-structured Json types * (and for {@link JsonToken#VALUE_EMBEDDED_OBJECT}) * stream is not advanced. */ @SuppressWarnings("unchecked") public <T> T readValueAs(TypeReference<?> valueTypeRef) throws IOException { return (T) _codec().readValue(this, valueTypeRef); }
/** * Method to deserialize JSON content into a non-container * type (it can be an array type, however): typically a bean, array * or a wrapper type (like {@link java.lang.Boolean}). * <b>Note</b>: method can only be called if the parser has * an object codec assigned; this is true for parsers constructed * by <code>MappingJsonFactory</code> (from "jackson-databind" jar) * but not for {@link JsonFactory} (unless its <code>setCodec</code> * method has been explicitly called). *<p> * This method may advance the event stream, for structured types * the current token will be the closing end marker (END_ARRAY, * END_OBJECT) of the bound structure. For non-structured Json types * (and for {@link JsonToken#VALUE_EMBEDDED_OBJECT}) * stream is not advanced. *<p> * Note: this method should NOT be used if the result type is a * container ({@link java.util.Collection} or {@link java.util.Map}. * The reason is that due to type erasure, key and value types * can not be introspected when using this method. */ public <T> T readValueAs(Class<T> valueType) throws IOException { return _codec().readValue(this, valueType); }
private List readQueryResultArrayFromString(String str) throws Exception { List result = new ArrayList(); JsonParser jp = mapper.getFactory().createParser(str); if (jp.nextToken() != JsonToken.START_ARRAY) { throw new IAE("not an array [%s]", str); } ObjectCodec objectCodec = jp.getCodec(); while (jp.nextToken() != JsonToken.END_ARRAY) { result.add(objectCodec.readValue(jp, toolChest.getResultTypeReference())); } return result; }
@Test public void shouldThrowOnNullTree() throws Exception { exception.expect(JWTDecodeException.class); exception.expectMessage("Parsing the Payload's JSON resulted on a Null map"); JsonParser parser = mock(JsonParser.class); ObjectCodec codec = mock(ObjectCodec.class); DeserializationContext context = mock(DeserializationContext.class); when(codec.readValue(eq(parser), any(TypeReference.class))).thenReturn(null); when(parser.getCodec()).thenReturn(codec); deserializer.deserialize(parser, context); }
@Test public void shouldThrowOnNullTree() throws Exception { exception.expect(JWTDecodeException.class); exception.expectMessage("Parsing the Header's JSON resulted on a Null map"); JsonDeserializer deserializer = new HeaderDeserializer(objectReader); JsonParser parser = mock(JsonParser.class); ObjectCodec codec = mock(ObjectCodec.class); DeserializationContext context = mock(DeserializationContext.class); when(codec.readValue(eq(parser), any(TypeReference.class))).thenReturn(null); when(parser.getCodec()).thenReturn(codec); deserializer.deserialize(parser, context); }
@Override public BasicHeader deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { Map<String, JsonNode> tree = p.getCodec().readValue(p, new TypeReference<Map<String, JsonNode>>() { }); if (tree == null) { throw new JWTDecodeException("Parsing the Header's JSON resulted on a Null map"); } String algorithm = getString(tree, PublicClaims.ALGORITHM); String type = getString(tree, PublicClaims.TYPE); String contentType = getString(tree, PublicClaims.CONTENT_TYPE); String keyId = getString(tree, PublicClaims.KEY_ID); return new BasicHeader(algorithm, type, contentType, keyId, tree, objectReader); }
QueryInterruptedException cause = jp.getCodec().readValue(jp, QueryInterruptedException.class); throw new QueryInterruptedException(cause, host); } else if (nextToken != JsonToken.START_ARRAY) {
@Override public Payload deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { Map<String, JsonNode> tree = p.getCodec().readValue(p, new TypeReference<Map<String, JsonNode>>() { }); if (tree == null) { throw new JWTDecodeException("Parsing the Payload's JSON resulted on a Null map"); } String issuer = getString(tree, PublicClaims.ISSUER); String subject = getString(tree, PublicClaims.SUBJECT); List<String> audience = getStringOrArray(tree, PublicClaims.AUDIENCE); Date expiresAt = getDateFromSeconds(tree, PublicClaims.EXPIRES_AT); Date notBefore = getDateFromSeconds(tree, PublicClaims.NOT_BEFORE); Date issuedAt = getDateFromSeconds(tree, PublicClaims.ISSUED_AT); String jwtId = getString(tree, PublicClaims.JWT_ID); return new PayloadImpl(issuer, subject, audience, expiresAt, notBefore, issuedAt, jwtId, tree, objectReader); }
@Override public Resources<?> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { JavaType rootType = JacksonHelper.findRootType(this.contentType); UberDocument doc = p.getCodec().readValue(p, UberDocument.class); return extractResources(doc, rootType, this.contentType); }
@Override public Resource<?> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { UberDocument doc = p.getCodec().readValue(p, UberDocument.class); List<Link> links = doc.getUber().getLinks(); return doc.getUber().getData().stream().filter(uberData -> !StringUtils.isEmpty(uberData.getName())).findFirst() .map(uberData -> convertToResource(uberData, links)).orElseThrow( () -> new IllegalStateException("No data entry containing a 'value' was found in this document!")); }
@Override public ResourceSupport deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { UberDocument doc = p.getCodec().readValue(p, UberDocument.class); List<Link> links = doc.getUber().getLinks(); return doc.getUber().getData().stream() // .filter(uberData -> !StringUtils.isEmpty(uberData.getName())) // .findFirst() // .map(uberData -> convertToResourceSupport(uberData, links)) // .orElseGet(() -> { ResourceSupport resourceSupport = new ResourceSupport(); resourceSupport.add(links); return resourceSupport; }); }
@Override public PagedResources<?> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { JavaType rootType = JacksonHelper.findRootType(this.contentType); UberDocument doc = p.getCodec().readValue(p, UberDocument.class); Resources<?> resources = extractResources(doc, rootType, this.contentType); PageMetadata pageMetadata = extractPagingMetadata(doc); return new PagedResources<>(resources.getContent(), pageMetadata, resources.getLinks()); }
@Override public List<Link> deserialize(JsonParser jp, DeserializationContext deserializationContext) throws IOException { CollectionJsonDocument<?> document = jp.getCodec().readValue(jp, CollectionJsonDocument.class); return potentiallyAddSelfLink(document.getCollection().getLinks(), document.getCollection().getHref()); } }
@Override public Resource<?> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { JavaType rootType = JacksonHelper.findRootType(this.contentType); JavaType wrappedType = ctxt.getTypeFactory().constructParametricType(CollectionJsonDocument.class, rootType); CollectionJsonDocument<?> document = jp.getCodec().readValue(jp, wrappedType); List<? extends CollectionJsonItem<?>> items = Optional.ofNullable(document.getCollection().getItems()).orElse(new ArrayList<>()); List<Link> links = Optional.ofNullable(document.getCollection().getLinks()).orElse(new ArrayList<>()); if (items.size() == 0 && document.getCollection().getTemplate() != null) { Map<String, Object> properties = document.getCollection().getTemplate().getData().stream() .collect(Collectors.toMap(CollectionJsonData::getName, CollectionJsonData::getValue)); Object obj = PropertyUtils.createObjectFromProperties(rootType.getRawClass(), properties); return new Resource<>(obj, potentiallyAddSelfLink(links, document.getCollection().getHref())); } else { items.stream() .flatMap(item -> Optional.ofNullable(item.getLinks()) .map(Collection::stream) .orElse(Stream.empty())) .forEach(link -> { if (!links.contains(link)) links.add(link); }); return new Resource<>(items.get(0).toRawData(rootType), potentiallyAddSelfLink(links, items.get(0).getHref())); } }
JavaType wrappedType = ctxt.getTypeFactory().constructParametricType(CollectionJsonDocument.class, rootType); CollectionJsonDocument<?> document = jp.getCodec().readValue(jp, wrappedType);
@Override public PagedResources deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { JavaType rootType = JacksonHelper.findRootType(this.contentType); JavaType wrappedType = ctxt.getTypeFactory().constructParametricType(CollectionJsonDocument.class, rootType); CollectionJsonDocument<?> document = jp.getCodec().readValue(jp, wrappedType); List<Object> items = new ArrayList<>(); document.getCollection().getItems().forEach(item -> { Object data = item.toRawData(rootType); List<Link> links = item.getLinks() == null ? Collections.EMPTY_LIST : item.getLinks(); if (this.contentType.hasGenericTypes()) { if (this.contentType.containedType(0).hasRawClass(Resource.class)) { items.add(new Resource<>(data, potentiallyAddSelfLink(links, item.getHref()))); } else { items.add(data); } } }); PagedResources.PageMetadata pageMetadata = null; return new PagedResources(items, pageMetadata, potentiallyAddSelfLink(document.getCollection().getLinks(), document.getCollection().getHref())); }
@Override public Resources deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { JavaType rootType = JacksonHelper.findRootType(this.contentType); JavaType wrappedType = ctxt.getTypeFactory().constructParametricType(CollectionJsonDocument.class, rootType); CollectionJsonDocument<?> document = jp.getCodec().readValue(jp, wrappedType); List<Object> contentList = new ArrayList<>(); if (document.getCollection().getItems() != null) { for (CollectionJsonItem<?> item : document.getCollection().getItems()) { Object data = item.toRawData(rootType); if (this.contentType.hasGenericTypes()) { if (isResource(this.contentType)) { contentList.add(new Resource<>(data, potentiallyAddSelfLink(item.getLinks(), item.getHref()))); } else { contentList.add(data); } } } } return new Resources(contentList, potentiallyAddSelfLink(document.getCollection().getLinks(), document.getCollection().getHref())); }
@Override public PatchData deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { Map<String, Object> patch = p.getCodec().readValue(p, new TypeReference<Map<String, Object>>() {}); return new PatchData(patch); } }