Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.node;
  
  
Value node that contains a wrapped POJO, to be serialized as a JSON constructed through data mapping (usually done by calling com.fasterxml.jackson.databind.ObjectMapper).
 
 public final class POJONode
     extends ValueNode
 {
     protected final Object _value;
 
     public POJONode(Object v) {  = v; }
 
     /*
     /**********************************************************
     /* Base class overrides
     /**********************************************************
      */
 
     @Override public JsonToken asToken() { return .; }
 
     @Override
     public boolean isPojo() { return true; }

    
As it is possible that some implementations embed byte[] as POJONode (despite optimal being BinaryNode), let's add support for exposing binary data here too.
 
     @Override
     public byte[] binaryValue() throws IOException
     {
         if ( instanceof byte[]) {
             return (byte[]) ;
         }
         return super.binaryValue();
     }
     
     /* 
     /**********************************************************
     /* General type coercions
     /**********************************************************
      */
 
     @Override
     public String asText() {
         return ( == null) ? "null" : .toString();
     }
 
     @Override
     public boolean asBoolean(boolean defaultValue)
     {
         if ( != null &&  instanceof Boolean) {
             return ((Boolean).booleanValue();
         }
         return defaultValue;
     }
     
     @Override
     public int asInt(int defaultValue)
     {
         if ( instanceof Number) {
             return ((Number).intValue();
         }
         return defaultValue;
     }
 
     @Override
     public long asLong(long defaultValue)
     {
         if ( instanceof Number) {
             return ((Number).longValue();
         }
         return defaultValue;
     }
     
     @Override
     public double asDouble(double defaultValue)
     {
         if ( instanceof Number) {
             return ((Number).doubleValue();
         }
         return defaultValue;
     }
     
     /*
     /**********************************************************
     /* Public API, serialization
     /**********************************************************
      */
 
     @Override
    public final void serialize(JsonGenerator jgSerializerProvider provider)
        throws IOExceptionJsonProcessingException
    {
        if ( == null) {
            jg.writeNull();
        } else {
            jg.writeObject();
        }
    }
    /*
    /**********************************************************
    /* Extended API
    /**********************************************************
     */

    
Method that can be used to access the POJO this node wraps.
    public Object getPojo() { return ; }
    /*
    /**********************************************************
    /* Overridden standard methods
    /**********************************************************
     */
    @Override
    public boolean equals(Object o)
    {
        if (o == thisreturn true;
        if (o == nullreturn false;
        if (o.getClass() != getClass()) { // final class, can do this
            return false;
        }
        POJONode other = (POJONodeo;
        if ( == null) {
            return other._value == null;
        }
        return .equals(other._value);
    }
    @Override
    public int hashCode() { return .hashCode(); }
    @Override
    public String toString()
    {
        return String.valueOf();
    }
New to GrepCode? Check out our FAQ X