Example usage for com.google.gson GsonBuilder registerTypeAdapter

List of usage examples for com.google.gson GsonBuilder registerTypeAdapter

Introduction

In this page you can find the example usage for com.google.gson GsonBuilder registerTypeAdapter.

Prototype

@SuppressWarnings({ "unchecked", "rawtypes" })
public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) 

Source Link

Document

Configures Gson for custom serialization or deserialization.

Usage

From source file:ch.berta.fabio.popularmovies.data.rest.MovieDbClient.java

License:Apache License

/**
 * Returns a custom date deserializer that handles empty strings and returns today's date instead.
 *
 * @return the Gson object to use//from   w  w  w  . j  a va 2s. c  o  m
 */
private static Gson getGsonObject() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        final DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US);

        @Override
        public Date deserialize(final JsonElement json, final Type typeOfT,
                final JsonDeserializationContext context) throws JsonParseException {
            try {
                return dateFormat.parse(json.getAsString());
            } catch (ParseException e) {
                return new Date();
            }
        }
    });

    return gsonBuilder.create();
}

From source file:ch.vorburger.worlds.persistence.gson.GraphAdapterBuilder.java

License:Apache License

public void registerOn(GsonBuilder gsonBuilder) {
    Factory factory = new Factory(instanceCreators);
    gsonBuilder.registerTypeAdapterFactory(factory);
    for (Map.Entry<Type, InstanceCreator<?>> entry : instanceCreators.entrySet()) {
        gsonBuilder.registerTypeAdapter(entry.getKey(), factory);
    }//from  w  ww  . j  av a 2  s  .  com
}

From source file:citysdk.tourism.client.parser.JsonParser.java

License:Open Source License

private Deserializable parseJson(Class<? extends Deserializable> clazz) throws UnknownErrorException {
    if (json == null)
        return null;

    Logger logger = LogManager.getLogManager().getLogger(Logger.GLOBAL_LOGGER_NAME);
    logger.fine("Deserializing for " + clazz);
    logger.finest("JSON is: " + json);
    Deserializable deserialize;/*from   www . ja va  2s .c o m*/
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(clazz, new POIDeserializer());

    Gson gson = builder.create();
    try {
        deserialize = gson.fromJson(json, clazz);
    } catch (Exception e) {
        throw new UnknownErrorException("There was an error handling the request: " + e.getMessage(), e);
    }

    logger.fine("Done deserialization");
    return deserialize;
}

From source file:cl.niclabs.cb.common.MethodParser.java

License:Open Source License

public static Gson makeGson(MethodFactory factory) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Method.class, new MethodParser(factory));
    return gsonBuilder.create();
}

From source file:cl.niclabs.tscrypto.common.utils.Util.java

License:Open Source License

public static Gson GsonFactory(boolean pretty) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    if (pretty)//from w w  w . jav  a2 s .c  o m
        gsonBuilder.setPrettyPrinting();
    gsonBuilder.registerTypeAdapter(BigInteger.class, new BigIntegerBase64TypeAdapter());
    gsonBuilder.registerTypeAdapter(TSMessage.class, new TSMessageParser());
    return gsonBuilder.create();
}

From source file:classes.analysis.Analysis.java

License:Open Source License

/**
 * This static function returns a new object using the data contained in the
 * given JSON object (as String)./*  w ww . j a v a2  s .c o  m*/
 *
 * @param jsonString the JSON object
 * @return the new Object.
 */
public static Analysis fromJSON(JsonElement jsonString) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(NonProcessedData.class, getNonProcessedDataDeserializerInstance());
    gsonBuilder.registerTypeAdapter(ProcessedData.class, getProcessedDataDeserializerInstance());
    Gson gson = gsonBuilder.create();

    Analysis analysis = gson.fromJson(jsonString, Analysis.class);

    //FINALLY, WE HAVE TO ORDER THE NON PROCESSED DATA BY STEP NUMBER
    Arrays.sort(analysis.getNonProcessedData());

    return analysis;
}

From source file:classes.analysis.non_processed_data.RAWdata.java

License:Open Source License

/**
 * This static function returns a new object using the data contained in the
 * given JSON object (as String).//ww w  . ja va  2s.  co  m
 *
 * @param jsonString the JSON object
 * @return the new Object.
 */
public static RAWdata fromJSON(String jsonString) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(ExtractionMethod.class, getExtractionMethodDeserializerInstance());
    Gson gson = gsonBuilder.create();
    RAWdata step = gson.fromJson(jsonString, RAWdata.class);

    if (step.extractionMethod != null) {
        step.extractionMethod.setRawdataID(step.getStepID());
    }

    return step;
}

From source file:classes.analysis.non_processed_data.raw_data.ExtractionMethods.MassSpectrometry.java

License:Open Source License

/**
 * This static function returns a new object using the data contained in the
 * given JSON object (as String).//ww w .j a v  a 2 s  . c o m
 *
 * @param jsonString the JSON object
 * @return the new Object.
 */
public static MassSpectrometry fromJSON(String jsonString) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(SeparationMethod.class, getSeparationMethodDeserializerInstance());
    Gson gson = gsonBuilder.create();
    MassSpectrometry massSpectrometry = gson.fromJson(jsonString, MassSpectrometry.class);

    if (massSpectrometry.getSeparationMethod() != null) {
        massSpectrometry
                .setSeparationMethodType(massSpectrometry.getSeparationMethod().getSeparationMethodType());
    }

    return massSpectrometry;
}

From source file:classes.analysis.non_processed_data.raw_data.ExtractionMethods.NuclearMagneticResonance.java

License:Open Source License

/**
 * This static function returns a new object using the data contained in the
 * given JSON object (as String).// www.j av a  2s. c o  m
 *
 * @param jsonString the JSON object
 * @return the new Object.
 */
public static NuclearMagneticResonance fromJSON(String jsonString) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(SeparationMethod.class, getSeparationMethodDeserializerInstance());
    Gson gson = gsonBuilder.create();
    NuclearMagneticResonance nuclearMagneticResonance = gson.fromJson(jsonString,
            NuclearMagneticResonance.class);

    if (nuclearMagneticResonance.getSeparationMethod() != null) {
        nuclearMagneticResonance.setSeparationMethodType(
                nuclearMagneticResonance.getSeparationMethod().getSeparationMethodType());
    }

    return nuclearMagneticResonance;
}

From source file:cmput301.f13t01.elasticsearch.ESClient.java

License:GNU General Public License

/**
 * Creates the gson object that knows how to serialize Media.
 *//*from   ww w.j  a va 2  s.c o  m*/
public ESClient() {

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Media.class, new InterfaceAdapter<Media<String>>());
    gsonBuilder.registerTypeAdapter(Media.class, new InterfaceAdapter<Media<SpannableString>>());
    gsonBuilder.registerTypeAdapter(Media.class, new InterfaceAdapter<Media>());
    gson = gsonBuilder.create();

}