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:org.terasology.utilities.gson.MultimapHandler.java

License:Apache License

@Override
public JsonElement serialize(Multimap<String, V> src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject result = new JsonObject();
    List<String> keys = Lists.newArrayList(src.keys());
    Collections.sort(keys);/*w  w  w .j  a v  a2  s. com*/
    for (String key : keys) {
        Collection<V> values = src.get(key);
        if (values.size() > 1) {
            JsonArray array = new JsonArray();
            for (V value : values) {
                array.add(context.serialize(value));
            }
            result.add(key, array);
        } else if (values.size() == 1) {
            result.add(key, context.serialize(values.iterator().next()));
        } else {
            result.add(key, context.serialize(""));
        }
    }
    return result;
}

From source file:org.terasology.utilities.gson.ResolutionHandler.java

License:Apache License

@Override
public JsonElement serialize(Resolution src, Type typeOfSrc, JsonSerializationContext context) {
    if (src instanceof LwjglResolution) {
        return context.serialize(((LwjglResolution) src).getDisplayMode());
    } else {/*w  w  w.  jav a2 s . co  m*/
        return null;
    }
}

From source file:org.terasology.utilities.gson.SetMultimapTypeAdapter.java

License:Apache License

@Override
public JsonElement serialize(SetMultimap<String, V> src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject result = new JsonObject();
    List<String> keys = Lists.newArrayList(src.keys());
    Collections.sort(keys);//from ww w  .j a  v  a2  s .c om
    for (String key : keys) {
        Collection<V> values = src.get(key);
        if (values.size() > 1) {
            JsonArray array = new JsonArray();
            for (V value : values) {
                array.add(context.serialize(value));
            }
            result.add(key, array);
        } else if (values.size() == 1) {
            result.add(key, context.serialize(values.iterator().next()));
        } else {
            result.add(key, context.serialize(""));
        }
    }
    return result;
}

From source file:org.tuleap.mylyn.task.core.internal.serializer.AbstractTuleapSerializer.java

License:Open Source License

/**
 * {@inheritDoc}//from   ww  w  .  ja  v  a2s  . c om
 *
 * @see com.google.gson.JsonSerializer#serialize(java.lang.Object, java.lang.reflect.Type,
 *      com.google.gson.JsonSerializationContext)
 */
@Override
public JsonElement serialize(T element, Type type, JsonSerializationContext context) {
    JsonObject json = super.serialize(element, type, context).getAsJsonObject();
    JsonArray values = new JsonArray();
    json.add(ITuleapConstants.VALUES, values);
    for (AbstractTuleapField field : element.getFields()) {
        if (mustSerialize(field, element.isNew())) {
            AbstractFieldValue fieldValue = element.getFieldValue(field.getIdentifier());
            if (fieldValue == null) {
                if (field instanceof TuleapFileUpload) {
                    fieldValue = new AttachmentFieldValue(field.getIdentifier(), null);
                } else if (field instanceof TuleapArtifactLink) {
                    fieldValue = new ArtifactLinkFieldValue(field.getIdentifier(), null);
                } else if (field instanceof TuleapOpenList || field instanceof TuleapSelectBox
                        || field instanceof TuleapMultiSelectBox) {
                    fieldValue = new BoundFieldValue(field.getIdentifier(), null);
                } else {
                    fieldValue = new LiteralFieldValue(field.getIdentifier(), null);
                }
            }
            values.add(context.serialize(fieldValue));
        }
    }
    return json;
}

From source file:org.universAAL.middleware.brokers.message.gson.BrokerMessageSerializer.java

License:Apache License

public JsonElement serialize(BrokerMessage src, Type typeOfSrc, JsonSerializationContext context) {

    JsonObject retValue = new JsonObject();
    String className = src.getClass().getCanonicalName();
    retValue.addProperty(CLASSNAME, className);
    JsonElement elem = context.serialize(src);
    retValue.add(INSTANCE, elem);/*  ww  w  .ja v  a  2  s  .  c o  m*/
    return retValue;
}

From source file:org.wso2.carbon.uuf.renderablecreator.hbs.internal.serialize.HbsContextSerializer.java

License:Open Source License

/**
 * {@inheritDoc}//from w ww  .  j ava  2 s  .  c om
 */
@Override
public JsonElement serialize(Context hbsContext, Type type, JsonSerializationContext serializationContext) {
    JsonObject serialized = serializationContext.serialize(hbsContext.model()).getAsJsonObject();

    Context extendedContext;
    try {
        Field extendedContextField = ((Class) type).getDeclaredField("extendedContext");
        extendedContextField.setAccessible(true);
        extendedContext = (Context) extendedContextField.get(hbsContext);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        return serialized;
    }
    if (extendedContext == null) {
        return serialized;
    }

    serializationContext.serialize(extendedContext.model()).getAsJsonObject().entrySet()
            .forEach(entry -> serialized.add(entry.getKey(), entry.getValue()));
    return serialized;
}

From source file:org.wso2.carbon.uuf.renderablecreator.hbs.internal.serialize.ScriptObjectMirrorSerializer.java

License:Open Source License

/**
 * {@inheritDoc}/*from  ww  w .j av  a2  s.com*/
 */
@Override
public JsonElement serialize(ScriptObjectMirror jsObj, Type type, JsonSerializationContext context) {
    if ((jsObj == null) || ScriptObjectMirror.isUndefined(jsObj) || jsObj.isFunction()) {
        return JsonNull.INSTANCE;
    }

    if (jsObj.isArray()) {
        JsonArray jsonArray = new JsonArray();
        for (Object item : jsObj.values()) {
            jsonArray.add(serializeFurther(context.serialize(item), context));
        }
        return jsonArray;
    }
    if (jsObj.isEmpty()) {
        return new JsonObject();
    }

    JsonObject jsonObject = new JsonObject();
    for (String key : jsObj.getOwnKeys(false)) {
        jsonObject.add(key, serializeFurther(jsObj.getMember(key), context));
    }
    return jsonObject;
}

From source file:org.wso2.carbon.uuf.renderablecreator.hbs.internal.serialize.ScriptObjectMirrorSerializer.java

License:Open Source License

private JsonElement serializeFurther(Object src, JsonSerializationContext context) {
    return ScriptObjectMirror.isUndefined(src) ? JsonNull.INSTANCE : context.serialize(src);
}

From source file:org.xacml4j.v30.marshal.json.CategoryAdapter.java

License:Open Source License

@Override
public JsonElement serialize(Category src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject o = new JsonObject();
    if (src.getId() != null) {
        o.addProperty(JsonProperties.ID_PROPERTY, src.getId());
    }/*  ww  w  . ja va2s .c o m*/
    Entity e = src.getEntity();
    String categoryId = SHORT_NAMES.inverse().get(src.getCategoryId());
    o.addProperty(JsonProperties.CATEGORY_ID_PROPERTY,
            (categoryId == null) ? src.getCategoryId().getId() : categoryId);
    o.addProperty(JsonProperties.CONTENT_PROPERTY, DOMUtil.nodeToString(e.getContent()));
    o.add(JsonProperties.ATTRIBUTE_PROPERTY, context.serialize(e.getAttributes()));
    return o;
}

From source file:org.xacml4j.v30.marshal.json.RequestContextAdapter.java

License:Open Source License

@Override
public JsonElement serialize(RequestContext src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject o = new JsonObject();
    o.addProperty(RETURN_POLICY_ID_LIST_PROPERTY, src.isReturnPolicyIdList());
    o.addProperty(COMBINED_DECISION_PROPERTY, src.isCombinedDecision());
    // TODO: add support for predefined Attributes objects: Subject, Action, Resource, Environment
    o.add(JsonProperties.CATEGORY_ARRAY_NAME, context.serialize(src.getAttributes()));
    // SPEC: There must be at least one RequestReference object inside the MultiRequests object
    Collection<RequestReference> requestReferences = src.getRequestReferences();
    if (requestReferences != null && !requestReferences.isEmpty()) {
        JsonObject multiRequests = new JsonObject();
        multiRequests.add(REQUEST_REFERENCE_PROPERTY, context.serialize(requestReferences));
        o.add(MULTI_REQUESTS_PROPERTY, multiRequests);
    }/*from   w w  w  .  j a v  a2  s .  co m*/
    return o;
}