List of usage examples for com.fasterxml.jackson.databind.introspect BeanPropertyDefinition getMutator
public abstract AnnotatedMember getMutator();
From source file:org.usrz.libs.riak.JsonTest.java
@Test public void jsonTest() throws Exception { RiakIntrospector introspector = new RiakIntrospector(); System.err.println(introspector.getConverter(Set.class, Set.class)); final Bean bean = new Bean(); System.err.println("========================= SER"); final BeanDescription serializationDescription = introspector.getEntityReader(bean); final List<BeanPropertyDefinition> serializationProperties = serializationDescription.findProperties(); for (BeanPropertyDefinition property : serializationProperties) { System.err.println(property); property.couldDeserialize();/*from ww w .ja va2s . c om*/ System.err.println(" ----> " + property.getAccessor().getClass()); System.err.println(" ----> " + property.getAccessor().getRawType()); } System.err.println("========================= DESER"); final BeanDescription deserializationDescription = introspector.getEntityWriter(bean); final List<BeanPropertyDefinition> deserializationProperties = deserializationDescription.findProperties(); for (BeanPropertyDefinition property : deserializationProperties) { System.err.println(property); System.err.println(" ----> " + property.getMutator().getClass()); System.err.println(" ----> " + property.getMutator().getRawType()); } // // final ObjectMapper mapper = new ObjectMapper(); // mapper.enable(SerializationFeature.INDENT_OUTPUT); // // final String json = mapper.writeValueAsString(new Bean()); // System.err.println(json); // mapper.readValue(json, Bean.class); }
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. c o 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; }