Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is possible to use setters when Gson deserializes a JSON?

Is there any way the set methods of a given class, are used when using Gson's fromJson method?

I would like to do this because for every String global variable of the target class a trim is made.

Is there any GSON API annotation for this?

I am aware that GSON provides the ability to write custom serializers/deserializers but I would like to know if there is another way to achieve this.

like image 607
RedEagle Avatar asked May 14 '14 16:05

RedEagle


People also ask

Does GSON use getters and setters?

Java Gson fromJson The example uses fromJson method to read JSON into a Java object. Notice that getters and setters are not necessary.

Does GSON ignore transient fields?

By default, GSON excludes transient and static fields from the serialization/deserialization process.

Is GSON better than JSON?

GSON can use the Object definition to directly create an object of the desired type. While JSONObject needs to be parsed manually.

Does GSON offer parse () function?

The GSON JsonParser class can parse a JSON string or stream into a tree structure of Java objects. GSON also has two other parsers. The Gson JSON parser which can parse JSON into Java objects, and the JsonReader which can parse a JSON string or stream into tokens (a pull parser).


2 Answers

No, there is not. Gson works mainly by reflection on instance fields. So if you do not plan to move to Jackson that has this feature I think you cannot have a general way to call your setters. So there's no annotation for that.

BUT

to achieve your specific need you could:

  1. write your own custom TypeAdapter or
  2. create a constructor that has the string you intend to trim and create a custom InstanceCreator or
  3. parse your JSON as JsonObject, do some processing of the strings and then use that object as source for parsing into your class.

I can provide you with more hints as long as you post some code or give information about your data/JSON.

like image 167
giampaolo Avatar answered Nov 15 '22 17:11

giampaolo


I implemented a JsonDeserializer<String> and registered it on GsonBuilder. So, to all String fields received, Gson will use my StringGsonTypeAdapter to deserialize the value.

Below is my code:

import static net.hugonardo.java.commons.text.StringUtils.normalizeSpace;
import static net.hugonardo.java.commons.text.StringUtils.trimToNull;

final class StringGsonTypeAdapter implements JsonDeserializer<String> {

    private static final StringGsonTypeAdapter INSTANCE = new StringGsonTypeAdapter();

    static StringGsonTypeAdapter instance() {
        return INSTANCE;
    }

    @Override
    public String deserialize(JsonElement jsonElement, Type type, 
        JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
        return normalizeSpace(trimToNull(jsonElement.getAsString()));
    }
}

...and my GsonBuilder:

Gson gson = new GsonBuilder()
    .registerTypeAdapter(String.class, StringGsonTypeAdapter.instance())
    .create())
like image 21
hugonardo Avatar answered Nov 15 '22 19:11

hugonardo