List of usage examples for com.fasterxml.jackson.databind DeserializationContext constructType
public final JavaType constructType(Class<?> paramClass)
From source file:org.ebayopensource.common.util.ParametersDeserializer.java
/** * We need to implement this method to properly find things to delegate * to: it can not be done earlier since delegated deserializers almost * certainly require access to this instance (at least "List" and "Map" ones) *//*w ww .j a va 2 s . c o m*/ @Override public void resolve(DeserializationContext ctxt) throws JsonMappingException { JavaType obType = ctxt.constructType(Object.class); JavaType stringType = ctxt.constructType(String.class); TypeFactory tf = ctxt.getTypeFactory(); _mapDeserializer = _findCustomDeser(ctxt, tf.constructMapType(Map.class, stringType, obType)); }
From source file:javaslang.jackson.datatype.deserialize.SerializableDeserializer.java
@Override public T deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { JsonDeserializer<Object> deserializer = ctxt.findRootValueDeserializer(ctxt.constructType(byte[].class)); byte[] bytes = (byte[]) deserializer.deserialize(p, ctxt); return deserialize(bytes); }
From source file:javaslang.jackson.datatype.deserialize.EitherDeserializer.java
@Override public void resolve(DeserializationContext ctxt) throws JsonMappingException { super.resolve(ctxt); stringDeserializer = ctxt.findContextualValueDeserializer(ctxt.constructType(String.class), null); }
From source file:com.basistech.rosette.dm.jackson.MorphoAnalysisListDeserializer.java
private MorphoAnalysisListDeserializer(DeserializationContext ctxt) throws JsonMappingException { JavaType type = ctxt.constructType(MorphoAnalysis.class); maDeserializer = ctxt.findRootValueDeserializer(type); type = ctxt.constructType(HanMorphoAnalysis.class); hanMaDeserializer = ctxt.findRootValueDeserializer(type); type = ctxt.constructType(ArabicMorphoAnalysis.class); arMaDeserializer = ctxt.findRootValueDeserializer(type); ctxt.setAttribute(MorphoAnalysisListDeserializer.class, maDeserializer); type = ctxt.constructType(KoreanMorphoAnalysis.class); korMaDeserializer = ctxt.findRootValueDeserializer(type); ctxt.setAttribute(KoreanMorphoAnalysis.class, korMaDeserializer); cached = true;/* w w w.j av a 2 s .co m*/ }
From source file:com.addthis.codec.jackson.WriteonlyPropertyIgnorer.java
@Override public boolean handleUnknownProperty(DeserializationContext ctxt, JsonParser jp, JsonDeserializer<?> deserializer, Object beanOrClass, String propertyName) throws IOException, JsonProcessingException { ObjectCodec objectCodec = jp.getCodec(); if (objectCodec instanceof ObjectMapper) { ObjectMapper objectMapper = (ObjectMapper) objectCodec; BeanDescription beanDescription = objectMapper.getSerializationConfig() .introspect(ctxt.constructType(beanOrClass.getClass())); for (BeanPropertyDefinition propertyDefinition : beanDescription.findProperties()) { if (propertyName.equals(propertyDefinition.getName())) { jp.skipChildren();/*w w w .java2 s. com*/ return true; } } } return false; }
From source file:net.nullschool.grains.jackson.datatype.GrainDeserializer.java
@Override public void resolve(DeserializationContext ctxt) throws JsonMappingException { for (GrainProperty gp : factory.getBasisProperties().values()) { JacksonGrainProperty prop = new JacksonGrainProperty(gp, ctxt.getTypeFactory(), getValueClass()); JsonDeserializer<?> deserializer = ctxt.findContextualValueDeserializer(prop.getType(), prop); readers.put(prop.getName(), new PropertyReader(deserializer)); }/*from w w w.j a v a2s . co m*/ extValueDeserializer = ctxt.findContextualValueDeserializer(ctxt.constructType(Object.class), null); extArrayDeserializer = ctxt.findContextualValueDeserializer(ctxt.constructType(ConstCollection.class), null); extObjectDeserializer = ctxt.findContextualValueDeserializer(ctxt.constructType(ConstMap.class), null); }
From source file:de.fraunhofer.iosb.ilt.sta.deserialize.custom.CustomEntityDeserializer.java
@Override public T deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException, JsonProcessingException { Entity result;//w w w .j a v a 2 s . co m try { result = clazz.newInstance(); } catch (InstantiationException | IllegalAccessException ex) { throw new IOException("Error deserializing JSON!"); } // need to make subclass of this class for every Entity subclass with custom field to get expected class!!! BeanDescription beanDescription = ctxt.getConfig().introspect(ctxt.constructType(clazz)); ObjectMapper mapper = (ObjectMapper) parser.getCodec(); JsonNode obj = (JsonNode) mapper.readTree(parser); List<BeanPropertyDefinition> properties = beanDescription.findProperties(); Iterator<Map.Entry<String, JsonNode>> i = obj.fields(); // First check if we know all properties that are present. if (ctxt.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) { for (; i.hasNext();) { Map.Entry<String, JsonNode> next = i.next(); String fieldName = next.getKey(); JsonNode field = next.getValue(); Optional<BeanPropertyDefinition> findFirst = properties.stream() .filter(p -> p.getName().equals(fieldName)).findFirst(); if (!findFirst.isPresent()) { throw new UnrecognizedPropertyException(parser, "Unknown field: " + fieldName, parser.getCurrentLocation(), clazz, fieldName, null); } } } for (BeanPropertyDefinition classProperty : properties) { if (obj.has(classProperty.getName())) { // property is present in class and json Annotation annotation = classProperty.getAccessor().getAnnotation(CustomSerialization.class); if (annotation != null) { // property has custom annotation // check if encoding property is also present in json (and also in class itself for sanity reasons) CustomSerialization customAnnotation = (CustomSerialization) annotation; Optional<BeanPropertyDefinition> encodingClassProperty = properties.stream() .filter(p -> p.getName().equals(customAnnotation.encoding())).findFirst(); if (!encodingClassProperty.isPresent()) { throw new IOException("Error deserializing JSON as class '" + clazz.toString() + "' \n" + "Reason: field '" + customAnnotation.encoding() + "' specified by annotation as encoding field is not defined in class!"); } String customEncoding = null; if (obj.has(customAnnotation.encoding())) { customEncoding = obj.get(customAnnotation.encoding()).asText(); } Object customDeserializedValue = CustomDeserializationManager.getInstance() .getDeserializer(customEncoding) .deserialize(mapper.writeValueAsString(obj.get(classProperty.getName()))); classProperty.getMutator().setValue(result, customDeserializedValue); } else { // TODO type identificatin is not safe beacuase may ne be backed by field. Rather do multiple checks Object value = mapper.readValue(mapper.writeValueAsString(obj.get(classProperty.getName())), classProperty.getField().getType()); classProperty.getMutator().setValue(result, value); } } } return (T) result; }
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; }