Example usage for com.google.gson JsonSerializationContext serialize

List of usage examples for com.google.gson JsonSerializationContext serialize

Introduction

In this page you can find the example usage for com.google.gson JsonSerializationContext serialize.

Prototype

public JsonElement serialize(Object src, Type typeOfSrc);

Source Link

Document

Invokes default serialization on the specified object passing the specific type information.

Usage

From source file:com.sandata.lab.common.utils.data.serializer.CustomHashMapSerializer.java

License:Open Source License

/**
 * Transform Map key values to lower case.
 *
 * @param map//from  w  w w  .  ja va2 s  .c om
 * @param type
 * @param context
 * @return
 */
@Override
public JsonElement serialize(Map map, Type type, JsonSerializationContext context) {

    JsonObject result = new JsonObject();

    for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) {
        Map.Entry entry = (Map.Entry) iterator.next();
        Object obj = entry.getValue();

        JsonElement valueElement = context.serialize(obj, obj.getClass());
        result.add(entry.getKey().toString().toLowerCase(), valueElement);
    }

    return result;
}

From source file:com.srotya.tau.wraith.actions.ActionSerializer.java

License:Apache License

public JsonElement serialize(Action src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject result = new JsonObject();
    String type = src.getClass().getCanonicalName();
    if (Utils.CLASSNAME_FORWARD_MAP.containsKey(src.getClass().getCanonicalName())) {
        type = Utils.CLASSNAME_FORWARD_MAP.get(src.getClass().getCanonicalName());
    }/*from  w ww  .  java2  s.  c o  m*/
    result.add(TYPE, new JsonPrimitive(type));
    result.add(PROPS, context.serialize(src, src.getClass()));
    return result;
}

From source file:com.srotya.tau.wraith.conditions.ConditionSerializer.java

License:Apache License

public JsonElement serialize(Condition src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject result = new JsonObject();
    String type = src.getClass().getCanonicalName();
    if (Utils.CLASSNAME_FORWARD_MAP.containsKey(src.getClass().getCanonicalName())) {
        type = Utils.CLASSNAME_FORWARD_MAP.get(src.getClass().getCanonicalName());
    }/* w  w w . ja va2  s .  c om*/
    result.add(TYPE, new JsonPrimitive(type));
    result.add(PROPS, context.serialize(src, src.getClass()));
    return result;
}

From source file:com.vaadin.addon.charts.model.style.ThemeGradientColorSerializer.java

@Override
public JsonElement serialize(GradientColor src, Type typeOfSrc, JsonSerializationContext context) {

    // linearGradient: [0, 0, 250, 500],
    // stops: [/*from w w  w. j av a 2  s . c om*/
    // [0, 'rgb(48, 96, 48)'],
    // [1, 'rgb(0, 0, 0)']
    // ]

    JsonObject jsonObject = new JsonObject();

    LinearGradient linearGradient = src.getLinearGradient();

    if (linearGradient != null) {
        JsonArray value = new JsonArray();
        value.add(new JsonPrimitive(linearGradient.x1));
        value.add(new JsonPrimitive(linearGradient.y1));
        value.add(new JsonPrimitive(linearGradient.x2));
        value.add(new JsonPrimitive(linearGradient.y2));
        jsonObject.add("linearGradient", value);
    } else {
        RadialGradient radialGradient = src.getRadialGradient();
        JsonArray value = new JsonArray();
        value.add(new JsonPrimitive(radialGradient.cx));
        value.add(new JsonPrimitive(radialGradient.cy));
        value.add(new JsonPrimitive(radialGradient.r));
        jsonObject.add("radialGradient", value);
    }

    List<List<Object>> stops = src.getStops();
    jsonObject.add("stops", context.serialize(stops, List.class));

    return jsonObject;
}

From source file:com.yandex.money.api.typeadapters.model.showcase.uicontrol.AmountTypeAdapter.java

License:Open Source License

@Override
protected void serialize(Amount src, JsonObject to, JsonSerializationContext context) {
    to.addProperty(MEMBER_CURRENCY, src.currency.alphaCode);
    if (src.fee != NoFee.getInstance()) {
        to.add(MEMBER_FEE, context.serialize(src.fee, Fee.class));
    }/* w  ww  . ja v a2s  . com*/
    super.serialize(src, to, context);
}

From source file:edu.isi.wings.portal.classes.JsonHandler.java

License:Apache License

public JsonElement serialize(Binding binding, Type typeOfSrc, JsonSerializationContext context) {
    if (binding.isSet()) {
        JsonArray arr = new JsonArray();
        for (WingsSet s : binding)
            arr.add(context.serialize((Binding) s, typeOfSrc));
        return arr;
    } else {//from   w w w  .jav  a  2  s .c om
        JsonObject obj = new JsonObject();
        if (binding.isURIBinding()) {
            obj.add("id", new JsonPrimitive(binding.getID()));
            obj.add("type", new JsonPrimitive("uri"));
        } else if (binding.getValue() != null) {
            ValueBinding vb = (ValueBinding) binding;
            String datatype = vb.getDatatype();
            if (datatype == null)
                datatype = KBUtils.XSD + "string";
            obj.add("value", new JsonPrimitive(vb.getValueAsString()));
            obj.add("datatype", new JsonPrimitive(datatype));
            obj.add("type", new JsonPrimitive("literal"));
        }
        return obj;
    }
}

From source file:edu.tu_berlin.ise.opendata.blume.DailyMeasurementsSerializer.java

@Override
public JsonElement serialize(DailyMeasurements dailyMeasurements, Type typeOfSrc,
        JsonSerializationContext context) {

    JsonArray array = new JsonArray();

    for (Map.Entry<Station, Measurement[]> mapEntry : dailyMeasurements.stationMeasurements.entrySet()) {
        Station station = mapEntry.getKey();

        JsonObject rootObject = new JsonObject();
        rootObject.addProperty("source_id", "BLUME"); //TODO get from global config somehow (ctor?)
        rootObject.addProperty("device", station.id);
        rootObject.addProperty("timestamp", dailyMeasurements.date.toString());
        rootObject.add("location", context.serialize(station.coord, Coordinates.class));
        rootObject.addProperty("license", "TODO"); //TODO check license terms for BLUME

        JsonObject sensorsObject = new JsonObject();

        for (Measurement measurement : mapEntry.getValue()) {
            JsonObject sensorObject = new JsonObject();
            sensorObject.addProperty("observation_type", measurement.measurementType);
            sensorObject.addProperty("observation_value", measurement.value);
            sensorObject.addProperty("unit", measurement.unit);
            sensorsObject.add(measurement.measurand, sensorObject);
        }/*from w  w  w .  j a  v a2s . c o m*/

        rootObject.add("sensors", sensorsObject);
        array.add(rootObject);
    }
    return array;
}

From source file:gov.nasa.jpf.jdart.summaries.json.SubClassHandler.java

License:Open Source License

@Override
public JsonElement serialize(T t, Type type, JsonSerializationContext jsc) {
    JsonElement je = jsc.serialize(t, t.getClass());
    je.getAsJsonObject().addProperty(SUB_CLASS, t.getClass().getName());
    return je;// www  .j  ava  2 s .  c o  m
}

From source file:io.datakernel.serializer.GsonSubclassesAdapter.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/*from  w  ww . java  2 s . co m*/
public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) {
    Class<? extends T> aClass = (Class<? extends T>) src.getClass();
    String classTag = classTags.inverse().get(aClass);
    if (classTag != null) {
        return new JsonPrimitive(classTag);
    }
    String subclassName = subclassNames.inverse().get(aClass);
    if (subclassName != null) {
        JsonObject result = new JsonObject();
        result.addProperty(subclassField, subclassName);
        JsonObject element = (JsonObject) context.serialize(src, src.getClass());
        for (Map.Entry<String, JsonElement> entry : element.entrySet()) {
            result.add(entry.getKey(), entry.getValue());
        }
        return result;
    }
    return new JsonPrimitive(src.getClass().getName());
}

From source file:io.ucoin.ucoinj.core.client.model.bma.gson.IdentityTypeAdapter.java

License:Open Source License

@Override
public JsonElement serialize(BlockchainBlock.Identity identity, Type type, JsonSerializationContext context) {
    String result = new StringBuilder().append(identity.getPubkey()).append(":").append(identity.getSignature())
            .append(":").append(identity.getTimestamp()).append(":").append(identity.getUid()).toString();

    return context.serialize(result.toString(), String.class);
}