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.otago.lecturerweb.utill.XMLGregorianCalendarGsonSerializer.java

@Override
public JsonElement serialize(XMLGregorianCalendar src, Type typeOfSrc, JsonSerializationContext context) {
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    Date time = src.toGregorianCalendar().getTime();
    return context.serialize(sdf.format(time));
}

From source file:com.softwaremagico.tm.json.InterfaceAdapter.java

License:Open Source License

@Override
public JsonElement serialize(T jsonElement, Type type, JsonSerializationContext jsonSerializationContext) {
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty(JSON_CLASSNAME, jsonElement.getClass().getName());
    jsonObject.add(JSON_DATA, jsonSerializationContext.serialize(jsonElement));
    return jsonObject;
}

From source file:com.solidfire.jsvcgen.serialization.OptionalAdapter.java

License:Open Source License

/**
 * Serializes an Optional object.//from  w ww .  j a v a  2  s  .  c om
 *
 * @param optional the value to serialize.
 * @param type     the type of the source object.
 * @param context  Context used for serialization.
 * @return A tree of JsonElements corresponding to the serialized form of optional.
 */
public JsonElement serialize(Optional<?> optional, Type type, JsonSerializationContext context) {
    if (!optional.isPresent()) {
        // Nothing to serialize
        return null;
    }
    // Defer serialization to handler for type contained in Optional
    return context.serialize(optional.get());
}

From source file:com.splicemachine.derby.ddl.InterfaceSerializer.java

License:Apache License

@Override
public JsonElement serialize(T object, Type interfaceType, JsonSerializationContext context) {
    final JsonObject wrapper = new JsonObject();
    wrapper.addProperty("type", object.getClass().getName());
    wrapper.add("data", context.serialize(object));
    return wrapper;
}

From source file:com.thoughtworks.go.plugin.access.configrepo.codec.ArtifactTypeAdapter.java

License:Apache License

@Override
public JsonElement serialize(CRArtifact crArtifact, Type type, JsonSerializationContext context) {
    return context.serialize(crArtifact).getAsJsonObject();
}

From source file:com.thoughtworks.go.plugin.configrepo.codec.MaterialTypeAdapter.java

License:Apache License

@Override
public JsonElement serialize(CRMaterial material, Type type, JsonSerializationContext context) {
    return context.serialize(material);
}

From source file:com.thoughtworks.go.plugin.configrepo.codec.TaskTypeAdapter.java

License:Apache License

@Override
public JsonElement serialize(CRTask crTask, Type type, JsonSerializationContext context) {
    JsonObject retValue = context.serialize(crTask).getAsJsonObject();
    CRTask onCancel = crTask.getOnCancel();
    if (onCancel != null) {
        retValue.remove("onCancel");
        retValue.add("onCancel", context.serialize(onCancel));
    }//ww  w  .  j  ava 2 s .c om

    return retValue;
}

From source file:com.vaadin.addon.charts.model.gsonhelpers.AxisListSerializer.java

@Override
public JsonElement serialize(AxisList obj, Type type, JsonSerializationContext context) {

    if (obj.getNumberOfAxes() == 1) {
        return context.serialize(obj.getAxis(0));
    } else if (obj instanceof AxisList) {
        return context.serialize(obj.getAxes());
    } else {//w w w .ja v  a2 s .  co m
        return null;
    }
}

From source file:com.vaadin.addon.charts.model.gsonhelpers.ContainerDataSeriesSerializer.java

@Override
public JsonElement serialize(ContainerDataSeries src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject series = new JsonObject();
    JsonArray data = new JsonArray();

    if (context != null) {
        AbstractPlotOptions plotOptions = src.getPlotOptions();
        if (plotOptions != null) {
            if (!(plotOptions instanceof PlotOptionsSeries)) {
                series.addProperty("type", plotOptions.getChartType().toString());
            }/*from   w w  w .ja  v a 2  s. c  o m*/
            JsonObject po = context.serialize(plotOptions).getAsJsonObject();
            Set<Entry<String, JsonElement>> entrySet = po.entrySet();
            for (Entry<String, JsonElement> entry : entrySet) {
                series.add(entry.getKey(), entry.getValue());
            }
        }
        series.add("name", context.serialize(src.getName()));
        series.add("stack", context.serialize(src.getStack()));
    }
    Number xAxis = src.getxAxis();
    if (xAxis != null) {
        series.add("xAxis", new JsonPrimitive(xAxis));
    }
    Number yAxis = src.getyAxis();
    if (yAxis != null) {
        series.add("yAxis", new JsonPrimitive(yAxis));
    }
    series.add("data", data);

    Map<String, Object> pidMap = src.getAttributeToPropertyIdMap();

    Mode mode = null;
    for (Object o : pidMap.keySet()) {
        if (!(o.equals(ContainerDataSeries.SERIES_DEFAULT_ATTRIBUTE1)
                || o.equals(ContainerDataSeries.SERIES_DEFAULT_ATTRIBUTE2))) {
            mode = Mode.OBJECT;
            break;
        }
    }
    Object xProperty = pidMap.get(ContainerDataSeries.SERIES_DEFAULT_ATTRIBUTE1);
    if (xProperty == null) {
        xProperty = ContainerDataSeries.SERIES_DEFAULT_ATTRIBUTE1;
    }
    Object yProperty = pidMap.get(ContainerDataSeries.SERIES_DEFAULT_ATTRIBUTE2);
    if (yProperty == null) {
        yProperty = ContainerDataSeries.SERIES_DEFAULT_ATTRIBUTE2;
    }
    Container container = src.getVaadinContainer();
    if (mode != Mode.OBJECT) {
        if (container.getContainerPropertyIds().contains(xProperty)) {
            mode = Mode.XY;
        } else {
            mode = Mode.ONLY_Y;
        }
    }

    for (Object iid : container.getItemIds()) {
        Item item = container.getItem(iid);
        switch (mode) {
        case ONLY_Y:
            addAnonymousTypedValue(data, item.getItemProperty(yProperty));
            break;
        case XY:
            JsonArray entryArray = new JsonArray();
            data.add(entryArray);
            addAnonymousTypedValue(entryArray, item.getItemProperty(xProperty));
            addAnonymousTypedValue(entryArray, item.getItemProperty(yProperty));
            break;

        default:
            // render as json object
            JsonObject entryObject = new JsonObject();
            Property x = item.getItemProperty(xProperty);
            if (x != null) {
                addNamedAndTypedValue(entryObject, ContainerDataSeries.SERIES_DEFAULT_ATTRIBUTE1, x);
            }
            Property y = item.getItemProperty(yProperty);
            if (y != null) {
                addNamedAndTypedValue(entryObject, ContainerDataSeries.SERIES_DEFAULT_ATTRIBUTE2, y);
            }

            Iterator<String> iter = pidMap.keySet().iterator();
            while (iter.hasNext()) {
                String name = iter.next();
                Object id = pidMap.get(name);
                if (!id.equals(xProperty) && !id.equals(yProperty)) {
                    addNamedAndTypedValue(entryObject, name, item.getItemProperty(id));
                }
            }
            data.add(entryObject);

            break;
        }
    }
    return series;
}

From source file:com.vaadin.addon.charts.model.gsonhelpers.PaneListSerializer.java

@Override
public JsonElement serialize(PaneList obj, Type type, JsonSerializationContext context) {

    if (obj.getNumberOfPanes() == 1) {
        return context.serialize(obj.getPane(0));
    } else if (obj instanceof PaneList) {
        return context.serialize(obj.getPanes());
    } else {//from  w ww  .j  a  v a2s.com
        return null;
    }
}