Java Jackson default type mapping -
Java Jackson default type mapping -
i'm using jackson 2.4 convert pojos to/from maps. wrote little test programme below.
import com.fasterxml.jackson.databind.objectmapper; import java.util.map; public class testobjectmapper { private object byteval; private object shortval; private object intval; private object longval; private object floatval; private object doubleval; public testobjectmapper() { byteval = (byte) 127; shortval = (short) 255; intval = 10; longval = 20l; floatval = 1.2f; doubleval = 1.4; system.out.println("constructor"); system.out.println("byteval.getclass() = " + byteval.getclass()); system.out.println("shortval.getclass() = " + shortval.getclass()); system.out.println("intval.getclass() = " + intval.getclass()); system.out.println("longval.getclass() = " + longval.getclass()); system.out.println("floatval.getclass() = " + floatval.getclass()); system.out.println("doubleval.getclass() = " + doubleval.getclass()); system.out.println(); } public object getbyteval() { homecoming byteval; } public object getshortval() { homecoming shortval; } public object getintval() { homecoming intval; } public object getlongval() { homecoming longval; } public object getfloatval() { homecoming floatval; } public object getdoubleval() { homecoming doubleval; } public static void main(string[] args) { objectmapper mapper = new objectmapper(); testobjectmapper t = new testobjectmapper(); map map = mapper.convertvalue(t, map.class); system.out.println("map = " + map); system.out.println(); (object key : map.keyset()) { system.out.format("map.get(\"%s\").getclass() = %s\n", key, map.get(key).getclass()); } string k = "byteval"; system.out.format("((integer) map.get(\"%s\")).bytevalue() = %d\n", k, ((integer) map.get(k)).bytevalue()); k = "floatval"; system.out.format("((double) map.get(\"%s\")).floatvalue() = %f\n", k, ((double) map.get(k)).floatvalue()); } }
which generates next output:
constructor byteval.getclass() = class java.lang.byte shortval.getclass() = class java.lang.short intval.getclass() = class java.lang.integer longval.getclass() = class java.lang.long floatval.getclass() = class java.lang.float doubleval.getclass() = class java.lang.double map = {byteval=127, shortval=255, intval=10, longval=20, floatval=1.2000000476837158, doubleval=1.4} map.get("byteval").getclass() = class java.lang.integer map.get("shortval").getclass() = class java.lang.short map.get("intval").getclass() = class java.lang.integer map.get("longval").getclass() = class java.lang.long map.get("floatval").getclass() = class java.lang.double map.get("doubleval").getclass() = class java.lang.double ((integer) map.get("byteval")).bytevalue() = 127 ((double) map.get("floatval")).floatvalue() = 1.200000
why mapping of types right in cases not in others? there way of controlling without doing changes classes?
why mapping of types right in cases not in others?
this expected behavior jackson
. if take business relationship of info types supported json
, below, makes finish sense jackson convert info types of values accordingly.
number
— signed decimal number may contain fractional part , may utilize exponential e notation. json not allow non-numbers nan, nor create distinction between integer , floating-point. (even though javascript uses double-precision floating-point format numeric values, other languages implementing json may encode numbers differently) string
— sequence of 0 or more unicode characters, though characters outside bmp must represented surrogate pair. strings delimited double-quotation marks , back upwards backslash escaping syntax. boolean
— either of values true or false array
— ordered list of 0 or more values, each of may of type. arrays utilize square bracket notation elements beingness comma-separated. object
— unordered associative array (name/value pairs). objects delimited curly brackets , utilize commas separate each pair, while within each pair colon ':' character separates key or name value. keys must strings , should distinct each other within object. null
— empty value, using word null is there way of controlling without doing changes classes?
it can controlled while jackson used deserialization not @ time of serialization. next stack overflow reply may of help you.
java jackson - prevent float int conversion when deserializing
update
in order convert 1 object another, objectmapper
first serializes source object jsonparser
object , deserializes jsonparser
object destination type object. hence, makes behavior of objectmapper
quite obvious.
/
** * actual conversion implementation: instead of using existing read * , write methods, much of code inlined. reason * must avoid wrapping/unwrapping both efficiency , * correctness. if wrapping/unwrapping desired, * caller must utilize explicit <code>writevalue</code> , * <code>readvalue</code> methods. */ protected object _convert(object fromvalue, javatype tovaluetype) throws illegalargumentexception { // sanity check null first: if (fromvalue == null) homecoming null; /* utilize tokenbuffer, jsongenerator: * (see [jackson-175]) */ tokenbuffer buf = new tokenbuffer(this); seek { // inlined 'writevalue' minor changes: // first: disable wrapping when writing serializationconfig config = getserializationconfig().without(serializationfeature.wrap_root_value); // no need check closing of tokenbuffer _serializerprovider(config).serializevalue(buf, fromvalue); // matching read, inlined 'readvalue' minor mods: final jsonparser jp = buf.asparser(); object result; // ok pass in existing feature flags; unwrapping handled mapper final deserializationconfig deserconfig = getdeserializationconfig(); jsontoken t = _initforreading(jp); if (t == jsontoken.value_null) { deserializationcontext ctxt = createdeserializationcontext(jp, deserconfig); result = _findrootdeserializer(ctxt, tovaluetype).getnullvalue(); } else if (t == jsontoken.end_array || t == jsontoken.end_object) { result = null; } else { // pointing event other null deserializationcontext ctxt = createdeserializationcontext(jp, deserconfig); jsondeserializer<object> deser = _findrootdeserializer(ctxt, tovaluetype); // note: no handling of unwarpping result = deser.deserialize(jp, ctxt); } jp.close(); homecoming result; } grab (ioexception e) { // should not occur, no real i/o... throw new illegalargumentexception(e.getmessage(), e); } }
source code http://grepcode.com/file/repo1.maven.org/maven2/com.fasterxml.jackson.core/jackson-databind/2.0.0-rc1/com/fasterxml/jackson/databind/objectmapper.java#objectmapper.convertvalue%28java.lang.object%2cjava.lang.class%29
java types jackson
Comments
Post a Comment