• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.node;
2 
3 import java.io.IOException;
4 
5 import com.fasterxml.jackson.core.*;
6 import com.fasterxml.jackson.databind.JsonSerializable;
7 import com.fasterxml.jackson.databind.SerializerProvider;
8 
9 /**
10  * Value node that contains a wrapped POJO, to be serialized as
11  * a JSON constructed through data mapping (usually done by
12  * calling {@link com.fasterxml.jackson.databind.ObjectMapper}).
13  */
14 public class POJONode
15     extends ValueNode
16 {
17     private static final long serialVersionUID = 2L;
18 
19     protected final Object _value;
20 
POJONode(Object v)21     public POJONode(Object v) { _value = v; }
22 
23     /*
24     /**********************************************************
25     /* Base class overrides
26     /**********************************************************
27      */
28 
29     @Override
getNodeType()30     public JsonNodeType getNodeType() {
31         return JsonNodeType.POJO;
32     }
33 
asToken()34     @Override public JsonToken asToken() { return JsonToken.VALUE_EMBEDDED_OBJECT; }
35 
36     /**
37      * As it is possible that some implementations embed byte[] as POJONode
38      * (despite optimal being {@link BinaryNode}), let's add support for exposing
39      * binary data here too.
40      */
41     @Override
binaryValue()42     public byte[] binaryValue() throws IOException
43     {
44         if (_value instanceof byte[]) {
45             return (byte[]) _value;
46         }
47         return super.binaryValue();
48     }
49 
50     /*
51     /**********************************************************
52     /* General type coercions
53     /**********************************************************
54      */
55 
56     @Override
asText()57     public String asText() { return (_value == null) ? "null" : _value.toString(); }
58 
asText(String defaultValue)59     @Override public String asText(String defaultValue) {
60         return (_value == null) ? defaultValue : _value.toString();
61     }
62 
63     @Override
asBoolean(boolean defaultValue)64     public boolean asBoolean(boolean defaultValue)
65     {
66         if (_value != null && _value instanceof Boolean) {
67             return ((Boolean) _value).booleanValue();
68         }
69         return defaultValue;
70     }
71 
72     @Override
asInt(int defaultValue)73     public int asInt(int defaultValue)
74     {
75         if (_value instanceof Number) {
76             return ((Number) _value).intValue();
77         }
78         return defaultValue;
79     }
80 
81     @Override
asLong(long defaultValue)82     public long asLong(long defaultValue)
83     {
84         if (_value instanceof Number) {
85             return ((Number) _value).longValue();
86         }
87         return defaultValue;
88     }
89 
90     @Override
asDouble(double defaultValue)91     public double asDouble(double defaultValue)
92     {
93         if (_value instanceof Number) {
94             return ((Number) _value).doubleValue();
95         }
96         return defaultValue;
97     }
98 
99     /*
100     /**********************************************************
101     /* Public API, serialization
102     /**********************************************************
103      */
104 
105     @Override
serialize(JsonGenerator gen, SerializerProvider ctxt)106     public final void serialize(JsonGenerator gen, SerializerProvider ctxt) throws IOException
107     {
108         if (_value == null) {
109             ctxt.defaultSerializeNull(gen);
110         } else if (_value instanceof JsonSerializable) {
111             ((JsonSerializable) _value).serialize(gen, ctxt);
112         } else {
113             // 25-May-2018, tatu: [databind#1991] do not call via generator but through context;
114             //    this to preserve contextual information
115             ctxt.defaultSerializeValue(_value, gen);
116         }
117     }
118 
119     /*
120     /**********************************************************
121     /* Extended API
122     /**********************************************************
123      */
124 
125     /**
126      * Method that can be used to access the POJO this node wraps.
127      */
getPojo()128     public Object getPojo() { return _value; }
129 
130     /*
131     /**********************************************************
132     /* Overridden standard methods
133     /**********************************************************
134      */
135 
136     @Override
equals(Object o)137     public boolean equals(Object o)
138     {
139         if (o == this) return true;
140         if (o == null) return false;
141         if (o instanceof POJONode) {
142             return _pojoEquals((POJONode) o);
143         }
144         return false;
145     }
146 
147     /**
148      * @since 2.3
149      */
_pojoEquals(POJONode other)150     protected boolean _pojoEquals(POJONode other)
151     {
152         if (_value == null) {
153             return other._value == null;
154         }
155         return _value.equals(other._value);
156     }
157 
158     @Override
hashCode()159     public int hashCode() { return _value.hashCode(); }
160 }
161