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.continuuity.loom.codec.json.current.LayoutConstraintCodec.java

License:Apache License

@Override
public JsonElement serialize(LayoutConstraint layoutConstraint, Type type, JsonSerializationContext context) {
    JsonObject jsonObj = new JsonObject();

    jsonObj.add("mustcoexist", context.serialize(layoutConstraint.getServicesThatMustCoexist()));
    jsonObj.add("cantcoexist", context.serialize(layoutConstraint.getServicesThatMustNotCoexist()));

    return jsonObj;
}

From source file:com.continuuity.loom.codec.json.current.LeaseDurationCodec.java

License:Apache License

@Override
public JsonElement serialize(LeaseDuration leaseDuration, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject jsonObj = new JsonObject();

    jsonObj.add("initial", context.serialize(leaseDuration.getInitial()));
    jsonObj.add("max", context.serialize(leaseDuration.getMax()));
    jsonObj.add("step", context.serialize(leaseDuration.getStep()));

    return jsonObj;
}

From source file:com.continuuity.loom.codec.json.current.PluginResourceMetaCodec.java

License:Apache License

@Override
public JsonElement serialize(ResourceMeta meta, Type type, JsonSerializationContext context) {
    JsonObject jsonObj = new JsonObject();

    jsonObj.add("name", context.serialize(meta.getName()));
    jsonObj.add("version", context.serialize(meta.getVersion()));
    jsonObj.add("status", context.serialize(meta.getStatus()));

    return jsonObj;
}

From source file:com.continuuity.loom.codec.json.current.ProviderCodec.java

License:Apache License

@Override
public JsonElement serialize(Provider provider, Type type, JsonSerializationContext context) {
    JsonObject jsonObj = new JsonObject();

    jsonObj.add("name", context.serialize(provider.getName()));
    jsonObj.add("description", context.serialize(provider.getDescription()));
    jsonObj.add("providertype", context.serialize(provider.getProviderType()));
    jsonObj.add("provisioner", context.serialize(provider.getProvisionerFields()));

    return jsonObj;
}

From source file:com.continuuity.loom.codec.json.current.ResourceCollectionCodec.java

License:Apache License

@Override
public JsonElement serialize(ResourceCollection src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject out = new JsonObject();
    out.add("automatortypes", new JsonObject());
    out.add("providertypes", new JsonObject());

    /*/*from www .  ja va  2  s  . c o  m*/
     * Transform it into something like:
     *
     * "resources": {
     *   "automatortypes": {
     *     "chef-solo": {
     *       "cookbooks": {
     *         "format": "archive|file",
     *         "active": [
     *           {
     *             "name":"reactor",
     *             "version":9
     *           }
     *         ]
     *       }
     *     },
     *     ...
     *   },
     * }
     */
    for (ImmutablePair<ResourceType, ResourceTypeFormat> key : src.getResources().keySet()) {
        ResourceType resourceType = key.getFirst();
        ResourceTypeFormat format = key.getSecond();
        String pluginTypeStr = pluginTypeToStr(resourceType.getPluginType());
        String pluginName = resourceType.getPluginName();
        String resourceTypeStr = resourceType.getTypeName();
        // ex: json object for automatortypes
        JsonObject pluginTypeObj = out.getAsJsonObject(pluginTypeStr);
        if (!pluginTypeObj.has(pluginName)) {
            pluginTypeObj.add(pluginName, new JsonObject());
        }
        // ex: json object for chef-solo
        JsonObject pluginObj = pluginTypeObj.getAsJsonObject(pluginName);
        if (!pluginObj.has(resourceTypeStr)) {
            pluginObj.add(resourceTypeStr, new JsonObject());
        }
        // ex: json object for cookbooks
        JsonObject resourceListObj = pluginObj.getAsJsonObject(resourceTypeStr);
        // write the format
        resourceListObj.add("format", context.serialize(format));
        // write the list of active resources
        JsonArray activeList = new JsonArray();
        for (ResourceMeta meta : src.getResources().get(key)) {
            JsonObject metaObj = new JsonObject();
            metaObj.addProperty("name", meta.getName());
            metaObj.addProperty("version", meta.getVersion());
            activeList.add(metaObj);
        }
        resourceListObj.add("active", activeList);
    }

    return out;
}

From source file:com.continuuity.loom.codec.json.current.ResourceTypeSpecificationCodec.java

License:Apache License

@Override
public JsonElement serialize(ResourceTypeSpecification spec, Type type, JsonSerializationContext context) {
    JsonObject jsonObj = new JsonObject();

    jsonObj.add("format", context.serialize(spec.getFormat()));

    return jsonObj;
}

From source file:com.continuuity.loom.codec.json.current.ServiceActionCodec.java

License:Apache License

@Override
public JsonElement serialize(ServiceAction action, Type type, JsonSerializationContext context) {
    JsonObject jsonObj = new JsonObject();

    jsonObj.add("type", context.serialize(action.getType()));
    jsonObj.add("fields", context.serialize(action.getFields()));

    return jsonObj;
}

From source file:com.continuuity.loom.codec.json.current.ServiceCodec.java

License:Apache License

@Override
public JsonElement serialize(Service service, Type type, JsonSerializationContext context) {
    JsonObject jsonObj = new JsonObject();

    jsonObj.add("name", context.serialize(service.getName()));
    jsonObj.add("description", context.serialize(service.getDescription()));
    jsonObj.add("dependencies", context.serialize(service.getDependencies()));
    JsonObject provisioner = new JsonObject();
    provisioner.add("actions", context.serialize(service.getProvisionerActions()));
    jsonObj.add("provisioner", provisioner);

    return jsonObj;
}

From source file:com.continuuity.loom.codec.json.current.ServiceConstraintCodec.java

License:Apache License

@Override
public JsonElement serialize(ServiceConstraint serviceConstraint, Type type, JsonSerializationContext context) {
    JsonObject jsonObj = new JsonObject();

    jsonObj.add("hardwaretypes", context.serialize(serviceConstraint.getRequiredHardwareTypes()));
    jsonObj.add("imagetypes", context.serialize(serviceConstraint.getRequiredImageTypes()));
    JsonObject quantities = new JsonObject();
    quantities.add("min", context.serialize(serviceConstraint.getMinCount()));
    quantities.add("max", context.serialize(serviceConstraint.getMaxCount()));
    quantities.add("stepSize", context.serialize(serviceConstraint.getStepSize()));
    quantities.add("ratio", context.serialize(ratioToString(serviceConstraint.getRatio())));
    jsonObj.add("quantities", quantities);

    return jsonObj;
}

From source file:com.continuuity.loom.codec.json.current.ServiceDependenciesCodec.java

License:Apache License

@Override
public JsonElement serialize(ServiceDependencies serviceDependencies, Type type,
        JsonSerializationContext context) {
    JsonObject jsonObj = new JsonObject();

    jsonObj.add("provides", context.serialize(serviceDependencies.getProvides()));
    jsonObj.add("conflicts", context.serialize(serviceDependencies.getConflicts()));
    jsonObj.add("install", context.serialize(serviceDependencies.getInstall()));
    jsonObj.add("runtime", context.serialize(serviceDependencies.getRuntime()));

    return jsonObj;
}