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:io.weba.processor.flink.event.gson.adapter.VisitorSerializer.java

License:Open Source License

@Override
public JsonObject serialize(Visitor visitor, java.lang.reflect.Type typeOfSrc,
        JsonSerializationContext context) {
    JsonObject visitorJsonObject = new JsonObject();
    visitorJsonObject.add("id", new JsonPrimitive(visitor.id.visitorIdentity.toString()));
    visitorJsonObject.add("first_visit_date", context.serialize(visitor.firstVisitDate));

    return visitorJsonObject;
}

From source file:jk_5.nailed.api.chat.serialization.BaseComponentSerializer.java

License:Open Source License

protected void serialize(@Nonnull JsonObject object, @Nonnull BaseComponent component,
        @Nonnull JsonSerializationContext context) {
    boolean first = false;
    if (ComponentSerializer.serializedComponents.get() == null) {
        first = true;/*from   w  ww  .j av a 2  s  . com*/
        ComponentSerializer.serializedComponents.set(new HashSet<BaseComponent>());
    }
    try {
        Checks.check(!ComponentSerializer.serializedComponents.get().contains(component), "Component loop");
        ComponentSerializer.serializedComponents.get().add(component);
        if (component.getColorRaw() != null) {
            object.addProperty("color", component.getColorRaw().getName());
        }
        if (component.isBoldRaw() != null) {
            object.addProperty("bold", component.isBoldRaw());
        }
        if (component.isItalicRaw() != null) {
            object.addProperty("italic", component.isItalicRaw());
        }
        if (component.isUnderlinedRaw() != null) {
            object.addProperty("underlined", component.isUnderlinedRaw());
        }
        if (component.isStrikethroughRaw() != null) {
            object.addProperty("strikethrough", component.isStrikethroughRaw());
        }
        if (component.isObfuscatedRaw() != null) {
            object.addProperty("obfuscated", component.isObfuscatedRaw());
        }

        if (component.getExtra() != null) {
            object.add("extra", context.serialize(component.getExtra()));
        }

        //Events
        if (component.getClickEvent() != null) {
            JsonObject clickEvent = new JsonObject();
            clickEvent.addProperty("action", component.getClickEvent().getAction().toString().toLowerCase());
            clickEvent.addProperty("value", component.getClickEvent().getValue());
            object.add("clickEvent", clickEvent);
        }
        if (component.getHoverEvent() != null) {
            JsonObject hoverEvent = new JsonObject();
            hoverEvent.addProperty("action", component.getHoverEvent().getAction().toString().toLowerCase());
            hoverEvent.add("value", context.serialize(component.getHoverEvent().getValue()));
            object.add("hoverEvent", hoverEvent);
        }
    } finally {
        ComponentSerializer.serializedComponents.get().remove(component);
        if (first) {
            ComponentSerializer.serializedComponents.set(null);
        }
    }
}

From source file:jk_5.nailed.api.chat.serialization.TranslatableComponentSerializer.java

License:Open Source License

@Override
public JsonElement serialize(TranslatableComponent src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject object = new JsonObject();
    serialize(object, src, context);//w  ww . j  av  a 2  s .com
    object.addProperty("translate", src.getTranslate());
    if (src.getWith() != null) {
        object.add("with", context.serialize(src.getWith()));
    }
    return object;
}

From source file:json.GraphSONNodeSerializer.java

License:Apache License

private void addEdges(JsonSerializationContext jsc, Map<String, List<Edge>> groupedEdges, String type) {
    if (groupedEdges.isEmpty()) {
        return;//from  www. jav  a 2s . c  o  m
    }
    JsonObject edgesObject = new JsonObject();
    groupedEdges.keySet().forEach(key -> {
        List<Edge> edgesSublist = groupedEdges.get(key);
        JsonArray edgesArray = new JsonArray();
        edgesSublist.forEach(edge -> {
            JsonObject edgeObject = new JsonObject();
            edgeObject.addProperty("id", edge.getId().toString());
            String edgeDirProperty = "inV";
            Integer nodeId = Integer.parseInt(edge.getTarget().getId().toString());
            if (type.equals("inE")) {
                edgeDirProperty = "outV";
                nodeId = Integer.parseInt(edge.getSource().getId().toString());
            }
            edgeObject.addProperty(edgeDirProperty, nodeId);

            Iterator<Column> edgeColumns = edge.getAttributeColumns().iterator();
            JsonObject edgePropertiesObject = new JsonObject();
            edgeColumns.forEachRemaining(property -> {
                Object propertyValue = edge.getAttribute(property);
                if (!isEdgeMetaAttribute(property.getTitle()) && propertyValue != null) {
                    edgePropertiesObject.add(property.getTitle(), jsc.serialize(propertyValue));
                }
            });

            if (!edgePropertiesObject.entrySet().isEmpty()) {
                edgeObject.add("properties", edgePropertiesObject);
            }
            edgesArray.add(edgeObject);
        });
        edgesObject.add(key, edgesArray);
    });
    nodeObject.add(type, edgesObject);
}

From source file:json.GraphSONNodeSerializer.java

License:Apache License

private void addPropertyValue(JsonSerializationContext jsc, JsonArray propertyValuesArray,
        Object propertyValue) {//from ww w. j ava2  s  .  c  o m
    JsonObject propertyValueObject = new JsonObject();
    propertyValueObject.addProperty("id", idCounter.toString());
    idCounter++;
    propertyValueObject.add("value", jsc.serialize(propertyValue));
    propertyValuesArray.add(propertyValueObject);
}

From source file:main.java.miro.validator.export.json.ManifestSerializer.java

License:Open Source License

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

    JsonObject manifest_json = new JsonObject();
    JsonArray mft_files_json = new JsonArray();
    JsonObject file_json;/*from   ww w  .j a  v a2s  .c  o  m*/

    manifest_json.add("filename", new JsonPrimitive(src.getFilename()));
    ManifestCms manifest = src.getManifest();
    Map<String, byte[]> files = manifest.getFiles();

    for (String filename : manifest.getFileNames()) {
        file_json = new JsonObject();
        file_json.add("filename", new JsonPrimitive(filename));
        file_json.add("hash", context.serialize(files.get(filename)));
        mft_files_json.add(file_json);
    }
    manifest_json.add("files", mft_files_json);
    manifest_json.add("validation_result",
            context.serialize(src.getValidationResults(), ValidationResults.class));
    manifest_json.add("validity_period", new JsonPrimitive(src.getValidityPeriod().toString()));
    return manifest_json;
}

From source file:main.java.miro.validator.stats.export.RPKIRepositoryStatsSerializer.java

License:Open Source License

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

    JsonObject statsObj = new JsonObject();
    statsObj.add("name", new JsonPrimitive(src.getName()));
    DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
    String dtStr = fmt.print(src.getTimestamp());
    statsObj.add("timestamp", new JsonPrimitive(dtStr));
    statsObj.add("trustAnchor", new JsonPrimitive(src.getTrustAnchor()));
    statsObj.add("result", context.serialize(src.getResult()));
    return statsObj;
}

From source file:net.conquiris.gson.GsonIndexReport.java

License:Apache License

@Override
public JsonElement serialize(IndexReport src, Type typeOfSrc, JsonSerializationContext context) {
    final JsonObject object = new JsonObject();
    object.addProperty(LEVEL, src.getLevel().toString());
    object.addProperty(STARTED, src.isIndexStarted());
    object.addProperty(ACTIVE, src.isIndexActive());
    object.addProperty(STATUS, src.getIndexStatus().toString());
    if (src.getLevel() != IndexReportLevel.BASIC) {
        object.add(DELAYS, context.serialize(src.getDelays().get()));
        object.add(INFO, context.serialize(src.getInfo().get()));
    }// w  w w  . j av a 2 s  .co m
    return object;
}

From source file:net.datenstrudel.bulbs.core.application.facade.json.TypeHierarchyAdapterDtoActuatorCmd.java

@Override
public JsonElement serialize(DtoAbstractActuatorCmd src, Type typeOfSrc, JsonSerializationContext context) {
    return context.serialize(src);
}

From source file:net.kleditzsch.shcCore.Json.AutomationDeviceResponseSerializer.java

License:Open Source License

@Override
public JsonElement serialize(AutomationDeviceResponse automationDeviceResponse, Type type,
        JsonSerializationContext jsonSerializationContext) {

    JsonObject jo = new JsonObject();
    jo.addProperty("success", automationDeviceResponse.isSuccess());
    jo.addProperty("message", automationDeviceResponse.getMessage());
    jo.addProperty("errorCode", automationDeviceResponse.getErrorCode());

    JsonArray ja = new JsonArray();
    Map<String, AutomationDevice> automationDeviceMap = automationDeviceResponse.getAutomationDevices();
    for (String hash : automationDeviceMap.keySet()) {

        AutomationDevice automationDevice = automationDeviceMap.get(hash);
        ja.add(jsonSerializationContext.serialize(automationDevice));
    }//ww w .j a  va 2 s.  c  o m

    jo.add("devices", ja);

    JsonArray ja1 = new JsonArray();
    Map<String, SwitchServer> switchServers = automationDeviceResponse.getSwitchServers();
    for (String hash : switchServers.keySet()) {

        SwitchServer switchServer = switchServers.get(hash);
        ja1.add(jsonSerializationContext.serialize(switchServer));
    }

    jo.add("switchServer", ja1);
    return jo;
}