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.ibm.common.activitystreams.util.AbstractDictionaryObjectAdapter.java

License:Apache License

public JsonElement serialize(X x, Type type, JsonSerializationContext context) {
    JsonObject obj = new JsonObject();
    for (String key : x)
        obj.add(key, context.serialize(x.getSingle(key), klass));
    return obj;//from w  w w. j av  a 2s . c o m
}

From source file:com.ibm.common.geojson.as2.GeoAdapter.java

License:Apache License

@Override
public JsonElement serialize(GeoObject geo, Type type, JsonSerializationContext context) {

    JsonObject obj = new JsonObject();
    obj.add("type", context.serialize(geo.type(), GeoObject.Type.class));
    switch (geo.type()) {
    case POINT:/*from  w w  w  .  ja  v  a 2s  .  c o m*/
    case MULTIPOINT:
    case LINESTRING:
    case MULTILINESTRING:
    case MULTIPOLYGON:
    case POLYGON:
        CoordinateGeometry c = (CoordinateGeometry) geo;
        obj.add("coordinates", context.serialize(c.coordinates(), Iterable.class));
        break;
    case GEOMETRYCOLLECTION:
        GeometryCollection gc = (GeometryCollection) geo;
        obj.add("geometries", context.serialize(gc.geometries(), Iterable.class));
        break;
    case FEATURE:
        Feature feature = (Feature) geo;
        if (feature.id() != null)
            obj.addProperty("id", feature.id());
        Geometry<?, ?> geometry = feature.geometry();
        Map<String, Object> properties = feature.properties();
        if (geometry != null)
            obj.add("geometry", context.serialize(feature.geometry()));
        if (properties != null)
            obj.add("properties", context.serialize(properties));
        break;
    case FEATURECOLLECTION:
        FeatureCollection fc = (FeatureCollection) geo;
        obj.add("features", context.serialize(fc.features(), Iterable.class));
        break;
    default:
        break;

    }
    if (geo.boundingBox() != null) {
        BoundingBox bb = geo.boundingBox();
        obj.add("bbox", context.serialize(bb, Iterable.class));
    }
    if (geo.crs() != null) {
        CRS crs = geo.crs();
        JsonObject crsobj = new JsonObject();
        crsobj.addProperty("name", crs.type());
        if (crs.size() > 0)
            crsobj.add("properties", context.serialize(crs.properties()));
        obj.add("crs", crsobj);
    }
    return obj;
}

From source file:com.indragie.cmput301as1.ExpenseItemJSONSerializer.java

License:Open Source License

@Override
public JsonElement serialize(ExpenseItem src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject obj = new JsonObject();
    obj.addProperty("name", src.getName());
    obj.addProperty("description", src.getDescription());
    obj.addProperty("category", src.getCategory());
    obj.addProperty("incomplete", src.isIncomplete());
    obj.add("amount", context.serialize(src.getAmount(), Money.class));
    obj.add("date", context.serialize(src.getDate(), Date.class));

    Uri receiptUri = src.getReceiptUri();
    if (receiptUri != null) {
        InputStream inputStream;//w w w . j a  va  2s .com
        try {
            inputStream = new FileInputStream(receiptUri.getPath());
            byte[] buffer = new byte[inputStream.available()];
            inputStream.read(buffer);
            inputStream.close();

            String base64Str = Base64.encodeToString(buffer, Base64.DEFAULT);
            obj.addProperty("receipt_base64", base64Str);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return obj;
}

From source file:com.kotcrab.vis.editor.serializer.json.AssetComponentSerializer.java

License:Apache License

@Override
public JsonElement serialize(AssetReference asset, Type typeOfSrc, JsonSerializationContext context) {
    JsonElement jsonAsset = context.serialize(asset.asset, asset.asset.getClass());
    GsonUtils.appendClassProperty(jsonAsset, asset.asset, context);

    JsonObject jsonObject = new JsonObject();
    jsonObject.add("asset", jsonAsset);
    return jsonObject;
}

From source file:com.kotcrab.vis.editor.serializer.json.IntMapJsonSerializer.java

License:Apache License

@Override
public JsonElement serialize(IntMap<T> intMap, Type typeOfSrc, JsonSerializationContext context) {
    JsonArray jsonArray = new JsonArray();
    for (IntMap.Entry<T> entry : intMap.entries()) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.add(String.valueOf(entry.key), context.serialize(entry.value, entry.value.getClass()));
        GsonUtils.appendClassProperty(jsonObject, entry.value, context);

        jsonArray.add(jsonObject);/* ww w .j  a v a 2  s.  c o  m*/
    }

    return jsonArray;
}

From source file:com.kotcrab.vis.editor.serializer.json.ObjectMapJsonSerializer.java

License:Apache License

@Override
public JsonElement serialize(ObjectMap<K, V> objMap, Type typeOfSrc, JsonSerializationContext context) {
    JsonArray jsonArray = new JsonArray();
    for (ObjectMap.Entry<K, V> entry : objMap.entries()) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.add("key", context.serialize(entry.key, entry.key.getClass()));
        jsonObject.add("value", context.serialize(entry.value, entry.value.getClass()));
        GsonUtils.appendClassProperty(jsonObject, entry.key, context, PROPERTY_CLASS_KEY);
        GsonUtils.appendClassProperty(jsonObject, entry.value, context, PROPERTY_CLASS_VALUE);

        jsonArray.add(jsonObject);/*www  . j  a v  a 2  s.c o m*/
    }

    return jsonArray;
}

From source file:com.l.notel.notel.org.redpin.android.json.BaseFingerprintTypeAdapter.java

License:Open Source License

/**
 * @see JsonSerializer#serialize(Object, Type, JsonSerializationContext)
 *//*from w w  w.  j  a  va 2s  . co  m*/

public JsonElement serialize(org.redpin.base.core.Fingerprint src, Type typeOfSrc,
        JsonSerializationContext context) {
    return context.serialize(src, Fingerprint.class);
}

From source file:com.l.notel.notel.org.redpin.android.json.BaseLocationTypeAdapter.java

License:Open Source License

/**
 * @see JsonSerializer#serialize(Object, Type, JsonSerializationContext)
 *//*  www. jav a  2 s. c o  m*/
public JsonElement serialize(org.redpin.base.core.Location src, Type typeOfSrc,
        JsonSerializationContext context) {

    return context.serialize(src, Location.class);
}

From source file:com.l.notel.notel.org.redpin.android.json.BaseMapTypeAdapter.java

License:Open Source License

/**
 * @see JsonSerializer#serialize(Object, Type, JsonSerializationContext)
 *//*from w  w  w . j a v a 2  s. co m*/
public JsonElement serialize(org.redpin.base.core.Map src, Type typeOfSrc, JsonSerializationContext context) {
    return context.serialize(src, Map.class);
}

From source file:com.l.notel.notel.org.redpin.android.json.BaseMeasurementTypeAdapter.java

License:Open Source License

/**
 * @see JsonSerializer#serialize(Object, Type, JsonSerializationContext)
 *///from  ww  w  .j a  va  2  s .  c o m
public JsonElement serialize(org.redpin.base.core.Measurement src, Type typeOfSrc,
        JsonSerializationContext context) {

    return context.serialize(src, Measurement.class);
}