Example usage for com.fasterxml.jackson.core TreeNode getClass

List of usage examples for com.fasterxml.jackson.core TreeNode getClass

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core TreeNode getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:io.coala.json.DynaBean.java

/**
 * @param referenceType/*from   w  ww .  j  a v a 2  s  . com*/
 * @param <S>
 * @param <T>
 * @return
 */
static final <S, T> JsonDeserializer<T> createJsonDeserializer(final ObjectMapper om, final Class<T> resultType,
        final Properties... imports) {
    return new JsonDeserializer<T>() {

        @Override
        public T deserializeWithType(final JsonParser jp, final DeserializationContext ctxt,
                final TypeDeserializer typeDeserializer) throws IOException, JsonProcessingException {
            return deserialize(jp, ctxt);
        }

        @Override
        public T deserialize(final JsonParser jp, final DeserializationContext ctxt)
                throws IOException, JsonProcessingException {
            if (jp.getCurrentToken() == JsonToken.VALUE_NULL)
                return null;

            //            if( Wrapper.class.isAssignableFrom( resultType ) )
            //            {
            //               // FIXME
            //               LOG.trace( "deser wrapper intf of {}", jp.getText() );
            //               return (T) Wrapper.Util.valueOf( jp.getText(),
            //                     resultType.asSubclass( Wrapper.class ) );
            //            } 
            if (Config.class.isAssignableFrom(resultType)) {
                final Map<String, Object> entries = jp.readValueAs(new TypeReference<Map<String, Object>>() {
                });

                final Iterator<Entry<String, Object>> it = entries.entrySet().iterator();
                for (Entry<String, Object> next = null; it.hasNext(); next = it.next())
                    if (next != null && next.getValue() == null) {
                        LOG.trace("Ignoring null value: {}", next);
                        it.remove();
                    }
                return resultType.cast(ConfigFactory.create(resultType.asSubclass(Config.class), entries));
            }
            // else if (Config.class.isAssignableFrom(resultType))
            // throw new JsonGenerationException(
            // "Config does not extend "+Mutable.class.getName()+" required for deserialization: "
            // + Arrays.asList(resultType
            // .getInterfaces()));

            // can't parse directly to interface type
            final DynaBean bean = new DynaBean();
            final TreeNode tree = jp.readValueAsTree();

            // override attributes as defined in interface getters
            final Set<String> attributes = new HashSet<>();
            for (Method method : resultType.getMethods()) {
                if (method.getReturnType().equals(Void.TYPE) || method.getParameterTypes().length != 0)
                    continue;

                final String attribute = method.getName();
                if (attribute.equals("toString") || attribute.equals("hashCode"))
                    continue;

                attributes.add(attribute);
                final TreeNode value = tree.get(attribute);// bean.any().get(attributeName);
                if (value == null)
                    continue;

                bean.set(method.getName(),
                        om.treeToValue(value, JsonUtil.checkRegistered(om, method.getReturnType(), imports)));
            }
            if (tree.isObject()) {
                // keep superfluous properties as TreeNodes, just in case
                final Iterator<String> fieldNames = tree.fieldNames();
                while (fieldNames.hasNext()) {
                    final String fieldName = fieldNames.next();
                    if (!attributes.contains(fieldName))
                        bean.set(fieldName, tree.get(fieldName));
                }
            } else if (tree.isValueNode()) {
                for (Class<?> type : resultType.getInterfaces())
                    for (Method method : type.getDeclaredMethods()) {
                        //                     LOG.trace( "Scanning {}", method );
                        if (method.isAnnotationPresent(JsonProperty.class)) {
                            final String property = method.getAnnotation(JsonProperty.class).value();
                            //                        LOG.trace( "Setting {}: {}", property,
                            //                              ((ValueNode) tree).textValue() );
                            bean.set(property, ((ValueNode) tree).textValue());
                        }
                    }
            } else
                throw ExceptionFactory.createUnchecked("Expected {} but parsed: {}", resultType,
                        tree.getClass());

            return DynaBean.proxyOf(om, resultType, bean, imports);
        }
    };
}