List of usage examples for com.fasterxml.jackson.databind.type TypeFactory constructType
public JavaType constructType(Type paramType)
From source file:com.github.ljtfreitas.restify.http.client.message.converter.json.JacksonMessageConverter.java
@Override public boolean canRead(Type type) { TypeFactory typeFactory = objectMapper.getTypeFactory(); return objectMapper.canDeserialize(typeFactory.constructType(type)); }
From source file:com.github.cjm.service.BaseService.java
public ResourceCollection load(String resource, Class clazz, int page) { StringBuilder uri = new StringBuilder(MOVIEDB_URL); uri.append(resource);/* ww w.j av a2s.c om*/ uri.append("?api_key=").append(apiKey); if (page > 1) { uri.append("&page=").append(page); } RestTemplate restTemplate = new RestTemplate(); ResourceCollection<T> resourceCollection = new ResourceCollection<>(); String data = null; try { data = restTemplate.getForObject(uri.toString(), String.class); } catch (HttpClientErrorException e) { log.warn(e.getMessage()); } if (data != null) { ObjectMapper mapper = new ObjectMapper(); try { TypeFactory t = TypeFactory.defaultInstance(); resourceCollection = mapper.readValue(data, t.constructType(clazz)); } catch (IOException ex) { log.error("ObjectMapper exception: ", ex); } } return resourceCollection; }
From source file:com.github.ljtfreitas.restify.http.client.message.converter.json.JacksonMessageConverter.java
@Override public T read(HttpResponseMessage httpResponseMessage, Type expectedType) throws RestifyHttpMessageReadException { try {//from ww w .j av a2 s . co m TypeFactory typeFactory = objectMapper.getTypeFactory(); return objectMapper.readValue(httpResponseMessage.body(), typeFactory.constructType(expectedType)); } catch (IOException e) { throw new RestifyHttpMessageReadException(e); } }
From source file:ch.rasc.wampspring.method.MethodParameterConverter.java
public Object convert(MethodParameter parameter, Object argument) { if (argument == null) { if (parameter.getParameterType().getName().equals("java.util.Optional")) { return OptionalUnwrapper.empty(); }/* w w w.ja va 2 s. c om*/ return null; } Class<?> sourceClass = argument.getClass(); Class<?> targetClass = parameter.getParameterType(); TypeDescriptor td = new TypeDescriptor(parameter); if (targetClass.isAssignableFrom(sourceClass)) { return convertListElements(td, argument); } if (this.conversionService.canConvert(sourceClass, targetClass)) { try { return convertListElements(td, this.conversionService.convert(argument, targetClass)); } catch (Exception e) { TypeFactory typeFactory = this.objectMapper.getTypeFactory(); if (td.isCollection()) { JavaType type = CollectionType.construct(td.getType(), typeFactory.constructType(td.getElementTypeDescriptor().getType())); return this.objectMapper.convertValue(argument, type); } else if (td.isArray()) { JavaType type = typeFactory.constructArrayType(td.getElementTypeDescriptor().getType()); return this.objectMapper.convertValue(argument, type); } throw e; } } return this.objectMapper.convertValue(argument, targetClass); }
From source file:org.emfjson.jackson.databind.deser.ResourceDeserializer.java
@Override public Resource deserialize(JsonParser jp, DeserializationContext ctxt, Resource intoValue) throws IOException { final Resource resource = getResource(ctxt, intoValue); if (resource == null) { throw new IllegalArgumentException("Invalid resource"); }// w w w . j ava 2 s . co m final ReferenceEntries entries = new ReferenceEntries(); final EcoreTypeFactory ecoreType = new EcoreTypeFactory(); final ResourceSet resourceSet = resource.getResourceSet(); ctxt.setAttribute(RESOURCE, resource); ctxt.setAttribute(REFERENCE_ENTRIES, entries); ctxt.setAttribute(CACHE, new Cache()); ctxt.setAttribute(TYPE_FACTORY, ecoreType); ctxt.setAttribute(RESOURCE_SET, resourceSet); if (!jp.hasCurrentToken()) { jp.nextToken(); } final TypeFactory factory = TypeFactory.defaultInstance(); final JsonDeserializer<Object> deserializer = ctxt .findRootValueDeserializer(factory.constructType(EObject.class)); if (jp.getCurrentToken() == JsonToken.START_ARRAY) { while (jp.nextToken() != JsonToken.END_ARRAY) { EObject value = (EObject) deserializer.deserialize(jp, ctxt); if (value != null) { resource.getContents().add(value); } } } else if (jp.getCurrentToken() == JsonToken.START_OBJECT) { EObject value = (EObject) deserializer.deserialize(jp, ctxt); if (value != null) { resource.getContents().add(value); } } entries.resolve(resourceSet, uriHandler); return resource; }
From source file:org.camunda.spin.impl.json.jackson.format.ListJacksonJsonTypeDetector.java
protected JavaType constructType(Object object) { TypeFactory typeFactory = TypeFactory.defaultInstance(); if (object instanceof List && !((List<?>) object).isEmpty()) { List<?> list = (List<?>) object; Object firstElement = list.get(0); return typeFactory.constructCollectionType(list.getClass(), constructType(firstElement)); } else {/*ww w. j a v a 2 s .c o m*/ return typeFactory.constructType(object.getClass()); } }
From source file:com.arpnetworking.configuration.jackson.BaseJacksonConfiguration.java
/** * {@inheritDoc}//w ww .j a v a 2 s .c om */ @Override public <T> Optional<T> getPropertyAs(final String name, final Type type) throws IllegalArgumentException { final Optional<String> property = getProperty(name); if (!property.isPresent()) { return Optional.empty(); } try { final TypeFactory typeFactory = _objectMapper.getTypeFactory(); @SuppressWarnings("unchecked") final Optional<T> value = Optional .ofNullable((T) _objectMapper.readValue(property.get(), typeFactory.constructType(type))); return value; } catch (final IOException e) { throw new IllegalArgumentException( String.format("Unable to construct object from configuration; name=%s, type=%s, property=%s", name, type, property), e); } }
From source file:io.swagger.inflector.utils.ReflectionUtils.java
public JavaType[] getOperationParameterClasses(Operation operation, Map<String, Model> definitions) { TypeFactory tf = Json.mapper().getTypeFactory(); JavaType[] jt = new JavaType[operation.getParameters().size() + 1]; int i = 0;//from w w w .j ava2 s. c o m jt[i] = tf.constructType(RequestContext.class); i += 1; for (Parameter parameter : operation.getParameters()) { JavaType argumentClass = getTypeFromParameter(parameter, definitions); jt[i] = argumentClass; i += 1; } return jt; }
From source file:com.arpnetworking.configuration.jackson.BaseJacksonConfiguration.java
/** * {@inheritDoc}// ww w. j av a2 s . com */ @Override public <T> Optional<T> getAs(final Type type) throws IllegalArgumentException { final Optional<JsonNode> property = getJsonSource().getValue(); if (!property.isPresent()) { return Optional.empty(); } try { final TypeFactory typeFactory = _objectMapper.getTypeFactory(); @SuppressWarnings("unchecked") final Optional<T> value = Optional.ofNullable((T) _objectMapper .readValue(_objectMapper.treeAsTokens(property.get()), typeFactory.constructType(type))); return value; } catch (final IOException e) { throw new IllegalArgumentException(String.format( "Unable to construct object from configuration; type=%s, property=%s", type, property), e); } }
From source file:io.swagger.inflector.utils.ReflectionUtils.java
public JavaType getTypeFromProperty(String type, String format, Property property, Map<String, Model> definitions) { TypeFactory tf = Json.mapper().getTypeFactory(); if (("byte".equals(type)) || property instanceof ByteArrayProperty) { return tf.constructType(Byte[].class); }/*from ww w. j a v a 2 s. c om*/ if (("boolean".equals(type)) || property instanceof BooleanProperty) { return tf.constructType(Boolean.class); } if (("string".equals(type) && "date".equals(format)) || property instanceof DateProperty) { return tf.constructType(LocalDate.class); } if (("string".equals(type) && "date-time".equals(format)) || property instanceof DateTimeProperty) { return tf.constructType(DateTime.class); } if (("string".equals(type) && format == null) || property instanceof StringProperty) { return tf.constructType(String.class); } if (("number".equals(type) && format == null) || property instanceof DecimalProperty) { return tf.constructType(BigDecimal.class); } if (("number".equals(type) && "double".equals(format)) || property instanceof DoubleProperty) { return tf.constructType(Double.class); } if (("string".equals(type) && "email".equals(format)) || property instanceof EmailProperty) { return tf.constructType(String.class); } if (("number".equals(type) && "float".equals(format)) || property instanceof FloatProperty) { return tf.constructType(Float.class); } if (("string".equals(type) && "uuid".equals(format)) || property instanceof UUIDProperty) { return tf.constructType(UUID.class); } if (("file".equals(type)) || property instanceof FileProperty) { throw new RuntimeException("not implemented!"); } if (("integer".equals(type) && "int32".equals(format)) || property instanceof IntegerProperty) { return tf.constructType(Integer.class); } if (("integer".equals(type) && "int64".equals(format)) || property instanceof LongProperty) { return tf.constructType(Long.class); } if (property instanceof StringProperty) { return tf.constructType(String.class); } if (property instanceof ArrayProperty) { ArrayProperty ap = (ArrayProperty) property; Property inner = ap.getItems(); JavaType innerType = getTypeFromProperty(null, null, inner, definitions); return tf.constructArrayType(innerType); } if (property instanceof MapProperty) { MapProperty mp = (MapProperty) property; Property inner = mp.getAdditionalProperties(); JavaType innerType = getTypeFromProperty(null, null, inner, definitions); return tf.constructMapLikeType(Map.class, getTypeFromProperty("string", null, null, definitions), innerType); } if (property instanceof RefProperty) { RefProperty ref = (RefProperty) property; if (definitions != null) { Model model = definitions.get(ref.getSimpleRef()); if (model != null) { JavaType mt = getTypeFromModel(ref.getSimpleRef(), model, definitions); if (mt != null) { return mt; } } } } return null; }