json - Java - Does Google's GSON use constructors? -


i wondering when working on our project. gson api google use constructors json's want deserialize? example:

i've got json string want convert employee object. employee object has constructor applies checks parameters (for example, it's id > 0). we're using code below deserialize json's. constructor called when deserializing json employee?

link gson: https://github.com/google/gson

edit: after experimenting break points figured out constructor not called. know way called anyway?

/**  * gson class create , de-serialize json objects.  */ gson gson = new gson();  /**  * convert json object.  * @param json json convert.  * @param cls class convert to.  * @return converted json object.  */ public object jsontoobject(string json, class<?> cls) {     return gson.fromjson(json, cls); } 

libraries gson, jackson or java persistence api (jpa) use no-argument (default) construtor instantiate object , set fields via reflection. in newer versions of gson, not have declare default constructor anymore, see here.

if have call specific constructor in gson, can implement custom jsondeserializer, mentioned here.

in other libraries jackson, can define deserialization on methods, instead of fields (like gson does), allows substitute missing specialized constructor call.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -