package com.fasterxml.jackson.databind.node;
import java.io.IOException;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.JsonSerializable;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.util.RawValue;
public class POJONode
extends ValueNode
{
protected final Object _value;
public POJONode(Object v) { _value = v; }
@Override
public JsonNodeType getNodeType() {
return JsonNodeType.POJO;
}
@Override public JsonToken asToken() { return JsonToken.VALUE_EMBEDDED_OBJECT; }
@Override
public byte[] binaryValue() throws IOException
{
if (_value instanceof byte[]) {
return (byte[]) _value;
}
return super.binaryValue();
}
@Override
public String asText() { return (_value == null) ? "null" : _value.toString(); }
@Override public String asText(String defaultValue) {
return (_value == null) ? defaultValue : _value.toString();
}
@Override
public boolean asBoolean(boolean defaultValue)
{
if (_value != null && _value instanceof Boolean) {
return ((Boolean) _value).booleanValue();
}
return defaultValue;
}
@Override
public int asInt(int defaultValue)
{
if (_value instanceof Number) {
return ((Number) _value).intValue();
}
return defaultValue;
}
@Override
public long asLong(long defaultValue)
{
if (_value instanceof Number) {
return ((Number) _value).longValue();
}
return defaultValue;
}
@Override
public double asDouble(double defaultValue)
{
if (_value instanceof Number) {
return ((Number) _value).doubleValue();
}
return defaultValue;
}
@Override
public final void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException
{
if (_value == null) {
serializers.defaultSerializeNull(gen);
} else if (_value instanceof JsonSerializable) {
((JsonSerializable) _value).serialize(gen, serializers);
} else {
gen.writeObject(_value);
}
}
public Object getPojo() { return _value; }
@Override
public boolean equals(Object o)
{
if (o == this) return true;
if (o == null) return false;
if (o instanceof POJONode) {
return _pojoEquals((POJONode) o);
}
return false;
}
protected boolean _pojoEquals(POJONode other)
{
if (_value == null) {
return other._value == null;
}
return _value.equals(other._value);
}
@Override
public int hashCode() { return _value.hashCode(); }
@Override
public String toString()
{
if (_value instanceof byte[]) {
return String.format("(binary value of %d bytes)", ((byte[]) _value).length);
}
if (_value instanceof RawValue) {
return String.format("(raw value '%s')", ((RawValue) _value).toString());
}
return String.valueOf(_value);
}
}