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.continuuity.weave.internal.json.WeaveSpecificationCodec.java

License:Open Source License

@Override
public JsonElement serialize(WeaveSpecification src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject json = new JsonObject();
    json.addProperty("name", src.getName());
    json.add("runnables",
            context.serialize(src.getRunnables(), new TypeToken<Map<String, RuntimeSpecification>>() {
            }.getType()));//from   w ww. j  a v  a 2 s  .co  m
    json.add("orders", context.serialize(src.getOrders(), new TypeToken<List<WeaveSpecification.Order>>() {
    }.getType()));

    return json;
}

From source file:com.evilco.license.common.data.holder.LicenseHolderJsonAdapter.java

License:Apache License

/**
 * {@inheritDoc}/*from w w w .  j a v  a2  s .c  o  m*/
 */
@Override
public JsonElement serialize(ILicenseHolder src, Type typeOfSrc, JsonSerializationContext context) {
    // encode object
    JsonElement element = context.serialize(src, src.getClass());

    // append name
    element.getAsJsonObject().add("implementationClassName", context.serialize(src.getClass().getName()));

    // return modified element
    return element;
}

From source file:com.github.easyjsonapi.adapters.EasyJsonApiSerializer.java

License:Apache License

/**
 * Serializer object json for {@link EasyJsonApiTypeToken} sent to method
 * /*  w  ww . j a v a  2 s.  c o m*/
 * @param obj
 *            the object needs serializer
 * @param typeToken
 *            the type token to convert object
 * @param jsonContext
 *            the json context
 * @return one instance of json
 */
private JsonElement serializerObject(Object obj, EasyJsonApiTypeToken typeToken,
        JsonSerializationContext jsonContext) {

    Type type = Assert.notNull(this.tokenTypesToUse.get(EasyJsonApiTypeToken.TOKEN_DEFAULT))
            ? this.tokenTypesToUse.get(EasyJsonApiTypeToken.TOKEN_DEFAULT)
            : this.tokenTypesToUse.get(typeToken);

    if (Assert.isNull(type)) {
        throw new EasyJsonApiCastException(
                "Doesn't find token for " + obj.getClass().getName() + " resource object!");
    }

    return jsonContext.serialize(obj, type);
}

From source file:com.google.enterprise.adaptor.secmgr.json.ProxyTypeAdapter.java

License:Apache License

@Override
public JsonElement serialize(T instance, Type typeOfT, JsonSerializationContext context) {
    return context.serialize(invokeConstructor(constructor, instance), proxyClass);
}

From source file:com.google.gwtjsonrpc.server.MapDeserializer.java

License:Apache License

public JsonElement serialize(final Map<Object, Object> src, final Type typeOfSrc,
        final JsonSerializationContext context) {
    final Type kt = ((ParameterizedType) typeOfSrc).getActualTypeArguments()[0];
    final Type vt = ((ParameterizedType) typeOfSrc).getActualTypeArguments()[1];

    if (src == null) {
        return new JsonNull();
    }/*from   w w  w.  ja  v  a  2s  .  c o m*/

    if (kt == String.class) {
        final JsonObject r = new JsonObject();
        for (final Map.Entry<Object, Object> e : src.entrySet()) {
            r.add(e.getKey().toString(), context.serialize(e.getValue(), vt));
        }
        return r;
    } else {
        final JsonArray r = new JsonArray();
        for (final Map.Entry<Object, Object> e : src.entrySet()) {
            r.add(context.serialize(e.getKey(), kt));
            r.add(context.serialize(e.getValue(), vt));
        }
        return r;
    }
}

From source file:com.ibm.common.activitystreams.internal.ASObjectAdapter.java

License:Apache License

/**
 * Method serialize.//www.j  a  va2s  . com
 * @param obj ASObject
 * @param type Type
 * @param context JsonSerializationContext
        
 * @return JsonElement */
public final JsonElement serialize(ASObject obj, Type type, JsonSerializationContext context) {
    JsonObject el = new JsonObject();
    for (String key : obj) {
        Object val = obj.get(key);
        if (val != null) {
            el.add(key, context.serialize(val, val.getClass()));
        }
    }
    return el;

}

From source file:com.ibm.common.activitystreams.internal.LinkValueAdapter.java

License:Apache License

/**
 * Method serialize.//from  w w  w .  j a  va  2 s .  c o  m
 * @param value LinkValue
 * @param type Type
 * @param context JsonSerializationContext
        
 * @return JsonElement */
public JsonElement serialize(LinkValue value, Type type, JsonSerializationContext context) {
    switch (value.valueType()) {
    case SIMPLE:
        LinkValue.SimpleLinkValue simple = (SimpleLinkValue) value;
        return context.serialize(simple.url(), String.class);
    case ARRAY:
        return context.serialize(value, Iterable.class);
    case OBJECT:
        return context.serialize(value, ASObject.class);
    default:
        throw new IllegalArgumentException();
    }
}

From source file:com.ibm.common.activitystreams.internal.MultimapAdapter.java

License:Apache License

/**
 * Method serialize.//from  w  w w.  j  a  v  a 2 s. com
 * @param src Multimap
 * @param typeOfSrc Type
 * @param context JsonSerializationContext
        
 * @return JsonElement */
public JsonElement serialize(Multimap src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject obj = new JsonObject();
    for (Object key : src.keySet()) {
        Iterable<Object> vals = src.get(key);
        if (size(vals) == 1) {
            Object f = getFirst(vals, null);
            if (f != null)
                obj.add(key.toString(), context.serialize(f, f.getClass()));
        } else {
            obj.add(key.toString(), context.serialize(vals, Iterable.class));
        }
    }
    return obj;
}

From source file:com.ibm.common.activitystreams.internal.TypeValueAdapter.java

License:Apache License

/**
 * Method serialize.//from   w ww.j  a  v  a  2s  .co m
 * @param value TypeValue
 * @param type Type
 * @param context JsonSerializationContext
        
 * @return JsonElement */
public JsonElement serialize(TypeValue value, Type type, JsonSerializationContext context) {
    boolean simple = value.valueType() == ValueType.SIMPLE;
    return context.serialize(simple ? value.id() : value, simple ? String.class : ASObject.class);
}

From source file:com.ibm.common.activitystreams.legacy.MediaLinkAdapter.java

License:Apache License

public JsonElement serialize(MediaLink src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject el = new JsonObject();
    for (String key : src) {
        Object val = src.get(key);
        if (val != null)
            el.add(key, context.serialize(val, val.getClass()));
    }/*from  w  w  w. j ava2s.  c  om*/
    return el;
}