Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.deser.std;
  
  
  
Deserializer that can build instances of com.fasterxml.jackson.databind.JsonNode from any JSON content, using appropriate com.fasterxml.jackson.databind.JsonNode type.
 
 public class JsonNodeDeserializer
     extends BaseNodeDeserializer<JsonNode>
 {
    
Singleton instance of generic deserializer for com.fasterxml.jackson.databind.JsonNode. Only used for types other than JSON Object and Array.
 
     private final static JsonNodeDeserializer instance = new JsonNodeDeserializer();
 
     protected JsonNodeDeserializer() { super(JsonNode.class); }

    
Factory method for accessing deserializer for specific node type
 
     public static JsonDeserializer<? extends JsonNodegetDeserializer(Class<?> nodeClass)
     {
         if (nodeClass == ObjectNode.class) {
             return ObjectDeserializer.getInstance();
         }
         if (nodeClass == ArrayNode.class) {
             return ArrayDeserializer.getInstance();
         }
         // For others, generic one works fine
         return ;
     }
     
     /*
     /**********************************************************
     /* Actual deserializer implementations
     /**********************************************************
      */

    
Implementation that will produce types of any JSON nodes; not just one deserializer is registered to handle (in case of more specialized handler). Overridden by typed sub-classes for more thorough checking
 
     @Override
         throws IOExceptionJsonProcessingException
     {
         switch (jp.getCurrentToken()) {
         case :
             return deserializeObject(jpctxtctxt.getNodeFactory());
         case :
             return deserializeArray(jpctxtctxt.getNodeFactory());
         default:
             return deserializeAny(jpctxtctxt.getNodeFactory());
         }
     }
 
     /*
     /**********************************************************
     /* Specific instances for more accurate types
     /**********************************************************
      */
 
     final static class ObjectDeserializer
         extends BaseNodeDeserializer<ObjectNode>
     {
         protected final static ObjectDeserializer _instance = new ObjectDeserializer();
 
         protected ObjectDeserializer() {
             super(ObjectNode.class);
         }
 
         public static ObjectDeserializer getInstance() { return ; }
         
         @Override
         public ObjectNode deserialize(JsonParser jpDeserializationContext ctxt)
             throws IOExceptionJsonProcessingException
         {
             if (jp.getCurrentToken() == .) {
                 jp.nextToken();
                 return deserializeObject(jpctxtctxt.getNodeFactory());
             }
             if (jp.getCurrentToken() == .) {
                 return deserializeObject(jpctxtctxt.getNodeFactory());
             }
             throw ctxt.mappingException(ObjectNode.class);
          }
     }
         
     final static class ArrayDeserializer
         extends BaseNodeDeserializer<ArrayNode>
    {
        protected final static ArrayDeserializer _instance = new ArrayDeserializer();
        protected ArrayDeserializer() {
            super(ArrayNode.class);
        }
        public static ArrayDeserializer getInstance() { return ; }
        
        @Override
        public ArrayNode deserialize(JsonParser jpDeserializationContext ctxt)
            throws IOExceptionJsonProcessingException
        {
            if (jp.isExpectedStartArrayToken()) {
                return deserializeArray(jpctxtctxt.getNodeFactory());
            }
            throw ctxt.mappingException(ArrayNode.class);
        }
    }
Base class for all actual com.fasterxml.jackson.databind.JsonNode deserializer implementations
abstract class BaseNodeDeserializer<N extends JsonNode>
    extends StdDeserializer<N>
    public BaseNodeDeserializer(Class<N> nodeClass)
    {
        super(nodeClass);
    }
    
    @Override
            TypeDeserializer typeDeserializer)
        throws IOExceptionJsonProcessingException
    {
        /* Output can be as JSON Object, Array or scalar: no way to know
         * a priori. So:
         */
        return typeDeserializer.deserializeTypedFromAny(jpctxt);
    }
    /*
    /**********************************************************
    /* Overridable methods
    /**********************************************************
     */
    
    protected void _reportProblem(JsonParser jpString msg)
        throws JsonMappingException
    {
        throw new JsonMappingException(msgjp.getTokenLocation());
    }
    
    
Method called when there is a duplicate value for a field. By default we don't care, and the last value is used. Can be overridden to provide alternate handling, such as throwing an exception, or choosing different strategy for combining values or choosing which one to keep.

Parameters:
fieldName Name of the field for which duplicate value was found
objectNode Object node that contains values
oldValue Value that existed for the object node before newValue was added
newValue Newly added value just added to the object node
    protected void _handleDuplicateField(String fieldNameObjectNode objectNode,
                                         JsonNode oldValueJsonNode newValue)
        throws JsonProcessingException
    {
        // By default, we don't do anything
        ;
    }
    
    /*
    /**********************************************************
    /* Helper methods
    /**********************************************************
     */
    
    protected final ObjectNode deserializeObject(JsonParser jpDeserializationContext ctxt,
            final JsonNodeFactory nodeFactory)            
        throws IOExceptionJsonProcessingException
    {
        ObjectNode node = nodeFactory.objectNode();
        JsonToken t = jp.getCurrentToken();
        if (t == .) {
            t = jp.nextToken();
        }
        for (; t == .t = jp.nextToken()) {
            String fieldName = jp.getCurrentName();
            JsonNode value;
            switch (jp.nextToken()) {
            case :
                value = deserializeObject(jpctxtnodeFactory);
                break;
            case :
                value = deserializeArray(jpctxtnodeFactory);
                break;
            case :
                value = nodeFactory.textNode(jp.getText());
                break;
            default:
                value = deserializeAny(jpctxtnodeFactory);
            }
            JsonNode old = node.replace(fieldNamevalue);
            if (old != null) {
                _handleDuplicateField(fieldNamenodeoldvalue);
            }
        }
        return node;
    }
    
    protected final ArrayNode deserializeArray(JsonParser jpDeserializationContext ctxt,
            final JsonNodeFactory nodeFactory)            
        throws IOExceptionJsonProcessingException
    {
        ArrayNode node = nodeFactory.arrayNode();
        while (true) {
            JsonToken t = jp.nextToken();
            if (t == null) {
                throw ctxt.mappingException("Unexpected end-of-input when binding data into ArrayNode");
            }
            switch (t) {
            case :
                node.add(deserializeObject(jpctxtnodeFactory));
                break;
            case :
                node.add(deserializeArray(jpctxtnodeFactory));
                break;
            case :
                return node;
            case :
                node.add(nodeFactory.textNode(jp.getText()));
                break;
            default:
                node.add(deserializeAny(jpctxtnodeFactory));
                break;
            }
        }
    }
    protected final JsonNode deserializeAny(JsonParser jpDeserializationContext ctxt,
            final JsonNodeFactory nodeFactory)            
        throws IOExceptionJsonProcessingException
    {
        switch (jp.getCurrentToken()) {
        case :
            return deserializeObject(jpctxtnodeFactory);
        case :
            return deserializeArray(jpctxtnodeFactory);
        case :
            return deserializeObject(jpctxtnodeFactory);
        case :
            // [JACKSON-796]
            {
                Object ob = jp.getEmbeddedObject();
                if (ob == null) { // should this occur?
                    return nodeFactory.nullNode();
                }
                Class<?> type = ob.getClass();
                if (type == byte[].class) { // most common special case
                    return nodeFactory.binaryNode((byte[]) ob);
                }
                // any other special handling needed?
                return nodeFactory.POJONode(ob);
            }
        case :
            return nodeFactory.textNode(jp.getText());
        case :
            {
                JsonParser.NumberType nt = jp.getNumberType();
                if (nt == ..
                    || ctxt.isEnabled(.)) {
                    return nodeFactory.numberNode(jp.getBigIntegerValue());
                }
                if (nt == ..) {
                    return nodeFactory.numberNode(jp.getIntValue());
                }
                return nodeFactory.numberNode(jp.getLongValue());
            }
        case :
            {
                JsonParser.NumberType nt = jp.getNumberType();
                if (nt == ..
                    || ctxt.isEnabled(.)) {
                    return nodeFactory.numberNode(jp.getDecimalValue());
                }
                return nodeFactory.numberNode(jp.getDoubleValue());
            }
        case :
            return nodeFactory.booleanNode(true);
        case :
            return nodeFactory.booleanNode(false);
        case :
            return nodeFactory.nullNode();
            
            // These states can not be mapped; input stream is
            // off by an event or two
        //case END_OBJECT:
        //case END_ARRAY:
        default:
            throw ctxt.mappingException(getValueClass());
        }
    }
New to GrepCode? Check out our FAQ X