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);

Source Link

Document

Invokes default serialization on the specified object.

Usage

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

License:Apache License

/**
 * Method serialize./*from  w  w w.j  a va  2 s  .  c o m*/
 * @param nlv NLV
 * @param type Type
 * @param context JsonSerializationContext
        
 * @return JsonElement */
public JsonElement serialize(NLV nlv, Type type, JsonSerializationContext context) {
    JsonElement el = null;
    switch (nlv.valueType()) {
    case SIMPLE:
        el = context.serialize(((NLV.SimpleNLV) nlv).value());
        break;
    case OBJECT:
        NLV.MapNLV map = (MapNLV) nlv;
        JsonObject obj = new JsonObject();
        for (String lang : map)
            obj.addProperty(lang.toString(), map.value(lang));
        el = obj;
        break;
    default:
    }
    return el;
}

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

License:Apache License

/**
 * Method serialize.// w  w w. j av a2  s  .  co  m
 * @param t T
 * @param type Type
 * @param context JsonSerializationContext
        
        
 * @return JsonElement * @see com.google.gson.JsonSerializer#serialize(T, Type, JsonSerializationContext) */
public JsonElement serialize(T t, Type type, JsonSerializationContext context) {
    return context.serialize(serialize(t));
}

From source file:com.ibm.util.merge.json.DirectiveSerializer.java

License:Apache License

@Override
public JsonElement serialize(AbstractDirective src, Type directive, JsonSerializationContext context) {
    switch (src.getType()) {
    case Directives.TYPE_REPLACE_VALUE:
        return context.serialize((ReplaceValue) src);
    case Directives.TYPE_REQUIRE:
        return context.serialize((Require) src);
    case Directives.TYPE_TAG_INSERT:
        return context.serialize((InsertSubsTag) src);
    case Directives.TYPE_SQL_INSERT:
        return context.serialize((InsertSubsSql) src);
    case Directives.TYPE_SQL_REPLACE_COL:
        return context.serialize((ReplaceColSql) src);
    case Directives.TYPE_SQL_REPLACE_ROW:
        return context.serialize((ReplaceRowSql) src);
    case Directives.TYPE_CSV_INSERT:
        return context.serialize((InsertSubsCsv) src);
    case Directives.TYPE_CSV_REPLACE_COL:
        return context.serialize((ReplaceColCsv) src);
    case Directives.TYPE_CSV_REPLACE_ROW:
        return context.serialize((ReplaceRowCsv) src);
    case Directives.TYPE_HTML_INSERT:
        return context.serialize((InsertSubsHtml) src);
    case Directives.TYPE_HTML_REPLACE_COL:
        return context.serialize((ReplaceColHtml) src);
    case Directives.TYPE_HTML_REPLACE_ROW:
        return context.serialize((ReplaceRowHtml) src);
    case Directives.TYPE_HTML_REPLACE_MARKUP:
        return context.serialize((ReplaceMarkupHtml) src);
    default://www  .  ja  v  a  2  s  .c  om
        return null;
    }
}

From source file:com.ibm.util.merge.json.ProviderSerializer.java

License:Apache License

@Override
public JsonElement serialize(AbstractProvider src, Type provider, JsonSerializationContext context) {
    switch (src.getType()) {
    case Providers.TYPE_CSV:
        return context.serialize((ProviderCsv) src);
    case Providers.TYPE_HTML:
        return context.serialize((ProviderHtml) src);
    case Providers.TYPE_SQL:
        return context.serialize((ProviderSql) src);
    case Providers.TYPE_TAG:
        return context.serialize((ProviderTag) src);
    default://from  w w  w.  j  av a 2  s  .co m
        return null;
    }
}

From source file:com.javacreed.examples.gson.part2.BookSerialiser.java

License:Apache License

@Override
public JsonElement serialize(final Book book, final Type typeOfSrc, final JsonSerializationContext context) {
    final JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("title", book.getTitle());
    jsonObject.addProperty("isbn", book.getIsbn());

    final JsonElement jsonAuthros = context.serialize(book.getAuthors());
    jsonObject.add("authors", jsonAuthros);

    return jsonObject;
}

From source file:com.javacreed.examples.gson.part3.BookSerialiser.java

License:Apache License

@Override
public JsonElement serialize(final Book book, final Type typeOfSrc, final JsonSerializationContext context) {
    final JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("title", book.getTitle());
    jsonObject.addProperty("isbn", book.getIsbn());

    final JsonElement jsonAuthros = context.serialize(book.getAuthorsIds());
    jsonObject.add("authors", jsonAuthros);

    return jsonObject;
}

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

License:Apache License

@Override
public JsonElement serialize(Array<T> array, Type typeOfSrc, JsonSerializationContext context) {
    JsonArray jsonArray = new JsonArray();
    for (T element : array) {
        JsonElement jsonElement = context.serialize(element);

        if (jsonElement.isJsonObject()) {
            GsonUtils.appendClassProperty(jsonElement.getAsJsonObject(), element, context);
        } else if (jsonElement.isJsonPrimitive()) {
            JsonObject jsonNestedPrimitive = new JsonObject();
            jsonNestedPrimitive.add(PRIMITIVE_CONTENT, jsonElement);
            GsonUtils.appendClassProperty(jsonNestedPrimitive, element, context);
            jsonElement = jsonNestedPrimitive;
        } else if (jsonElement instanceof JsonArray) {
            throw new UnsupportedOperationException("Nested Arrays are not supported by ArrayJsonSerializer");
        }/*from   w  w w.j av  a2  s.c  o m*/

        jsonArray.add(jsonElement);
    }

    return jsonArray;
}

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

License:Apache License

public static void appendClassProperty(JsonElement json, Object object, JsonSerializationContext context,
        String customMemberName) {
    json.getAsJsonObject().add(customMemberName, context.serialize(object.getClass()));
}

From source file:com.kurento.kmf.jsonrpcconnector.JsonUtils.java

License:Open Source License

@Override
public JsonElement serialize(Props props, Type typeOfSrc, JsonSerializationContext context) {

    JsonObject jsonObject = new JsonObject();
    for (Prop prop : props) {
        jsonObject.add(prop.getName(), context.serialize(prop.getValue()));
    }/*  w w w .j ava2  s .  com*/

    return jsonObject;
}

From source file:com.myjeeva.digitalocean.serializer.DropletSerializer.java

License:Open Source License

@Override
public JsonElement serialize(Droplet droplet, Type paramType, JsonSerializationContext context) {

    final JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("name", droplet.getName());
    jsonObject.addProperty("region", droplet.getRegion().getSlug());
    jsonObject.addProperty("size", droplet.getSize());

    if (null == droplet.getImage().getId()) {
        jsonObject.addProperty("image", droplet.getImage().getSlug());
    } else {//from   www  .j  a  va2  s . c o m
        jsonObject.addProperty("image", droplet.getImage().getId());
    }

    if (null != droplet.getEnableBackup()) {
        jsonObject.addProperty("backups", droplet.getEnableBackup());
    }

    if (null != droplet.getEnableIpv6()) {
        jsonObject.addProperty("ipv6", droplet.getEnableIpv6());
    }

    if (null != droplet.getEnablePrivateNetworking()) {
        jsonObject.addProperty("private_networking", droplet.getEnablePrivateNetworking());
    }

    if (null != droplet.getKeys() && droplet.getKeys().size() > 0) {
        JsonArray sshKeys = new JsonArray();
        for (Key k : droplet.getKeys()) {
            if (null != k.getId()) {
                sshKeys.add(context.serialize(k.getId()));
            }
            if (!StringUtils.isEmpty(k.getFingerprint())) {
                sshKeys.add(context.serialize(k.getFingerprint()));
            }
        }
        jsonObject.add("ssh_keys", sshKeys);
    }

    // #19 - https://github.com/jeevatkm/digitalocean-api-java/issues/19
    if (null != droplet.getUserData()) {
        jsonObject.addProperty("user_data", droplet.getUserData());
    }

    return jsonObject;
}