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:gson.util.MessageAdapter.java

@Override
public JsonElement serialize(Message t, Type type, JsonSerializationContext jsc) {

    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("name", t.getName());
    JsonElement messageBody = jsc.serialize(t.getMessageBody());
    jsonObject.add("message_body", messageBody);

    return jsonObject;
}

From source file:gson.util.PacjentAdapter.java

@Override
public JsonElement serialize(Pacjent t, Type type, JsonSerializationContext jsc) {
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("imie", t.getImie());
    jsonObject.addProperty("nazwisko", t.getNazwisko());
    jsonObject.addProperty("pesel", t.getPesel());
    final JsonElement wyniki = jsc.serialize(t.getWyniki());
    jsonObject.add("wyniki", wyniki);

    return jsonObject;
}

From source file:in.aesh.align.AlignedTurnSerializer.java

@Override
public JsonElement serialize(AlignedTurn turn, Type type, JsonSerializationContext ctx) {
    JsonObject o = new JsonObject();
    o.addProperty("speaker", turn.speaker);
    o.addProperty("start", turn.start);
    o.addProperty("end", turn.end);
    o.add("sentences", turn.sentences.stream().map(sentence -> new JsonPrimitive(sentence))
            .collect(new JsonArrayCollector()));
    o.add("speech",
            turn.speech.stream().map(segment -> ctx.serialize(segment)).collect(new JsonArrayCollector()));
    return o;/*from w  w  w  . ja  v  a 2  s  .  c  om*/
}

From source file:io.vertigo.vega.engines.webservice.json.FacetedQueryResultJsonSerializerV1.java

License:Apache License

/** {@inheritDoc} */
@Override/* w  ww .  j a va 2s. co  m*/
public JsonElement serialize(final FacetedQueryResult<?, ?> facetedQueryResult, final Type typeOfSrc,
        final JsonSerializationContext context) {
    final JsonObject jsonObject = new JsonObject();

    //1- add result list as data
    if (facetedQueryResult.getClusters().isEmpty()) {
        final JsonArray jsonList = (JsonArray) context.serialize(facetedQueryResult.getDtList());
        jsonObject.add("list", jsonList);
    } else {
        //if it's a cluster add data's cluster
        final JsonObject jsonCluster = new JsonObject();
        for (final Entry<FacetValue, ?> cluster : facetedQueryResult.getClusters().entrySet()) {
            final JsonArray jsonList = (JsonArray) context.serialize(cluster.getValue());
            jsonCluster.add(cluster.getKey().getLabel().getDisplay(), jsonList);
        }
        jsonObject.add("groups", jsonCluster);
    }

    //2- add facet list as facets
    final List<Facet> facets = facetedQueryResult.getFacets();
    final JsonObject jsonFacet = new JsonObject();
    for (final Facet facet : facets) {
        final JsonObject jsonFacetValues = new JsonObject();
        facet.getFacetValues().forEach((k, v) -> jsonFacetValues.addProperty(k.getLabel().getDisplay(), v));
        final String facetName = facet.getDefinition().getName();
        jsonFacet.add(facetName, jsonFacetValues);
    }
    jsonObject.add("facets", jsonFacet);

    //3 -add totalCount
    jsonObject.addProperty(DtList.TOTAL_COUNT_META, facetedQueryResult.getCount());
    return jsonObject;
}

From source file:io.vertigo.vega.engines.webservice.json.FacetedQueryResultJsonSerializerV2.java

License:Apache License

/** {@inheritDoc} */
@Override/*from w  w w  .  ja  v a  2s .c  o  m*/
public JsonElement serialize(final FacetedQueryResult<?, ?> facetedQueryResult, final Type typeOfSrc,
        final JsonSerializationContext context) {
    final JsonObject jsonObject = new JsonObject();

    //1- add result list as data
    if (facetedQueryResult.getClusters().isEmpty()) {
        final JsonArray jsonList = (JsonArray) context.serialize(facetedQueryResult.getDtList());
        jsonObject.add("list", jsonList);
    } else {
        //if it's a cluster add data's cluster
        final JsonArray jsonCluster = new JsonArray();
        for (final Entry<FacetValue, ?> cluster : facetedQueryResult.getClusters().entrySet()) {
            final JsonArray jsonList = (JsonArray) context.serialize(cluster.getValue());
            final JsonObject jsonClusterElement = new JsonObject();
            jsonClusterElement.add(cluster.getKey().getLabel().getDisplay(), jsonList);
            jsonCluster.add(jsonClusterElement);
        }
        jsonObject.add("groups", jsonCluster);
    }

    //2- add facet list as facets
    final List<Facet> facets = facetedQueryResult.getFacets();
    final JsonArray jsonFacet = new JsonArray();
    for (final Facet facet : facets) {
        final JsonArray jsonFacetValues = new JsonArray();
        for (final Entry<FacetValue, Long> entry : facet.getFacetValues().entrySet()) {
            final JsonObject jsonFacetValuesElement = new JsonObject();
            jsonFacetValuesElement.addProperty(entry.getKey().getLabel().getDisplay(), entry.getValue());
            jsonFacetValues.add(jsonFacetValuesElement);
        }
        final String facetName = facet.getDefinition().getName();
        final JsonObject jsonFacetElement = new JsonObject();
        jsonFacetElement.add(facetName, jsonFacetValues);
        jsonFacet.add(jsonFacetElement);
    }
    jsonObject.add("facets", jsonFacet);

    //3 -add totalCount
    jsonObject.addProperty(DtList.TOTAL_COUNT_META, facetedQueryResult.getCount());
    return jsonObject;
}

From source file:io.vertigo.vega.engines.webservice.json.FacetedQueryResultJsonSerializerV3.java

License:Apache License

/** {@inheritDoc} */
@Override//from  www.  j  ava  2 s .c o m
public JsonElement serialize(final FacetedQueryResult<?, ?> facetedQueryResult, final Type typeOfSrc,
        final JsonSerializationContext context) {
    final JsonObject jsonObject = new JsonObject();

    //1- add result list as data
    if (facetedQueryResult.getClusters().isEmpty()) {
        final JsonArray jsonList = (JsonArray) context.serialize(facetedQueryResult.getDtList());
        jsonObject.add("list", jsonList);
    } else {
        //if it's a cluster add data's cluster
        final JsonArray jsonCluster = new JsonArray();
        for (final Entry<FacetValue, ?> cluster : facetedQueryResult.getClusters().entrySet()) {
            final DtList<?> dtList = (DtList<?>) cluster.getValue();
            if (!dtList.isEmpty()) {
                final JsonArray jsonList = (JsonArray) context.serialize(dtList);
                final JsonObject jsonClusterElement = new JsonObject();
                jsonClusterElement.addProperty("code", cluster.getKey().getCode());
                jsonClusterElement.addProperty("label", cluster.getKey().getLabel().getDisplay());
                jsonClusterElement.add("list", jsonList);
                jsonCluster.add(jsonClusterElement);
            }
        }
        jsonObject.add("groups", jsonCluster);
    }

    //2- add facet list as facets
    final List<Facet> facets = facetedQueryResult.getFacets();
    final JsonArray jsonFacet = new JsonArray();
    for (final Facet facet : facets) {
        final JsonArray jsonFacetValues = new JsonArray();
        for (final Entry<FacetValue, Long> entry : facet.getFacetValues().entrySet()) {
            if (entry.getValue() > 0) {
                final JsonObject jsonFacetValuesElement = new JsonObject();
                jsonFacetValuesElement.addProperty("code", entry.getKey().getCode());
                jsonFacetValuesElement.addProperty("count", entry.getValue());
                jsonFacetValuesElement.addProperty("label", entry.getKey().getLabel().getDisplay());
                jsonFacetValues.add(jsonFacetValuesElement);
            }
        }
        final String facetName = facet.getDefinition().getName();
        final JsonObject jsonFacetElement = new JsonObject();
        jsonFacetElement.add(facetName, jsonFacetValues);
        jsonFacet.add(jsonFacetElement);
    }
    jsonObject.add("facets", jsonFacet);

    //3 -add totalCount
    jsonObject.addProperty(DtList.TOTAL_COUNT_META, facetedQueryResult.getCount());
    return jsonObject;
}

From source file:io.vertigo.vega.engines.webservice.json.FacetedQueryResultJsonSerializerV4.java

License:Apache License

/** {@inheritDoc} */
@Override/*from  ww w.j av  a2  s .  c  om*/
public JsonElement serialize(final FacetedQueryResult<?, ?> facetedQueryResult, final Type typeOfSrc,
        final JsonSerializationContext context) {
    final JsonObject jsonObject = new JsonObject();

    //1- add result list as data, with highlight
    if (!facetedQueryResult.getClusterFacetDefinition().isPresent()) {
        final DtList<?> dtList = facetedQueryResult.getDtList();
        final JsonArray jsonList = (JsonArray) context.serialize(dtList);
        jsonObject.add("list", jsonList);
        jsonObject.addProperty("listType", dtList.getDefinition().getClassSimpleName());
        jsonObject.add("highlight", serializeHighLight(dtList, (FacetedQueryResult) facetedQueryResult));
    } else {
        //if it's a cluster add data's cluster
        final JsonArray jsonCluster = new JsonArray();
        for (final Entry<FacetValue, ?> cluster : facetedQueryResult.getClusters().entrySet()) {
            final DtList<?> dtList = (DtList<?>) cluster.getValue();
            if (!dtList.isEmpty()) {
                final JsonArray jsonList = (JsonArray) context.serialize(dtList);
                final JsonObject jsonClusterElement = new JsonObject();
                jsonClusterElement.addProperty("code", cluster.getKey().getCode());
                jsonClusterElement.addProperty("label", cluster.getKey().getLabel().getDisplay());
                jsonClusterElement.add("list", jsonList);
                jsonClusterElement.addProperty("listType", dtList.getDefinition().getClassSimpleName());
                jsonClusterElement.addProperty("totalCount",
                        getFacetCount(cluster.getKey(), facetedQueryResult));
                jsonClusterElement.add("highlight",
                        serializeHighLight(dtList, (FacetedQueryResult) facetedQueryResult));
                jsonCluster.add(jsonClusterElement);
            }
        }
        jsonObject.add("groups", jsonCluster);
    }

    //2- add facet list as facets
    final List<Facet> facets = facetedQueryResult.getFacets();
    final JsonArray jsonFacet = new JsonArray();
    for (final Facet facet : facets) {
        final JsonArray jsonFacetValues = new JsonArray();
        for (final Entry<FacetValue, Long> entry : facet.getFacetValues().entrySet()) {
            if (entry.getValue() > 0) {
                final JsonObject jsonFacetValuesElement = new JsonObject();
                jsonFacetValuesElement.addProperty("code", entry.getKey().getCode());
                jsonFacetValuesElement.addProperty("count", entry.getValue());
                jsonFacetValuesElement.addProperty("label", entry.getKey().getLabel().getDisplay());
                jsonFacetValues.add(jsonFacetValuesElement);
            }
        }
        final JsonObject jsonFacetElement = new JsonObject();
        jsonFacetElement.addProperty("code", facet.getDefinition().getName());
        jsonFacetElement.addProperty("label", facet.getDefinition().getLabel().getDisplay());
        jsonFacetElement.add("values", jsonFacetValues);
        jsonFacet.add(jsonFacetElement);
    }
    jsonObject.add("facets", jsonFacet);

    //3 -add totalCount
    jsonObject.addProperty(DtList.TOTAL_COUNT_META, facetedQueryResult.getCount());
    return jsonObject;
}

From source file:io.vertigo.vega.rest.engine.GoogleJsonEngine.java

License:Apache License

private Gson createGson() {
    return new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").setPrettyPrinting()
            //TODO  registerTypeAdapter(String.class, new EmptyStringAsNull<>())// add "" <=> null
            //.serializeNulls()//On veut voir les null
            .registerTypeAdapter(UiObject.class, new UiObjectDeserializer<>())
            .registerTypeAdapter(UiListDelta.class, new UiListDeltaDeserializer<>())
            .registerTypeAdapter(UiList.class, new UiListDeserializer<>())
            //.registerTypeAdapter(UiObjectExtended.class, new UiObjectExtendedDeserializer<>())
            /*.registerTypeAdapter(DtObjectExtended.class, new JsonSerializer<DtObjectExtended<?>>() {
               @Override// ww w  .  j a va 2  s .  c om
               public JsonElement serialize(final DtObjectExtended<?> src, final Type typeOfSrc, final JsonSerializationContext context) {
                  final JsonObject jsonObject = new JsonObject();
                  final JsonObject jsonInnerObject = (JsonObject) context.serialize(src.getInnerObject());
                  for (final Entry<String, JsonElement> entry : jsonInnerObject.entrySet()) {
                     jsonObject.add(entry.getKey(), entry.getValue());
                  }
                  for (final Entry<String, Serializable> entry : src.entrySet()) {
                     jsonObject.add(entry.getKey(), context.serialize(entry.getValue()));
                  }
                  return jsonObject;
               }
            })*/
            .registerTypeAdapter(FacetedQueryResult.class, new JsonSerializer<FacetedQueryResult>() {
                @Override
                public JsonElement serialize(final FacetedQueryResult facetedQueryResult, final Type typeOfSrc,
                        final JsonSerializationContext context) {
                    final JsonObject jsonObject = new JsonObject();
                    //1- add result list as data
                    final JsonArray jsonData = (JsonArray) context.serialize(facetedQueryResult.getDtList());
                    jsonObject.add("data", jsonData);

                    //2- add facet list as facets
                    final List<Facet> facets = facetedQueryResult.getFacets();
                    final JsonArray facetList = new JsonArray();
                    for (final Facet facet : facets) {
                        final JsonObject jsonFacet = new JsonObject();

                        final Map<String, Long> maps = new HashMap<>();
                        for (final Entry<FacetValue, Long> entry : facet.getFacetValues().entrySet()) {
                            maps.put(entry.getKey().getLabel().getDisplay(), entry.getValue());
                        }

                        final JsonObject jsonFacetValues = (JsonObject) context.serialize(maps);
                        final String facetName = facet.getDefinition().getLabel().getDisplay();
                        jsonFacet.add(facetName, jsonFacetValues);
                        facetList.add(jsonFacet);
                    }
                    jsonObject.add("facets", context.serialize(facetList));
                    return jsonObject;
                }
            }).registerTypeAdapter(ComponentInfo.class, new JsonSerializer<ComponentInfo>() {
                @Override
                public JsonElement serialize(final ComponentInfo componentInfo, final Type typeOfSrc,
                        final JsonSerializationContext context) {
                    final JsonObject jsonObject = new JsonObject();
                    jsonObject.add(componentInfo.getTitle(), context.serialize(componentInfo.getValue()));
                    return jsonObject;
                }
            }).registerTypeAdapter(List.class, new JsonSerializer<List>() {

                @Override
                public JsonElement serialize(final List src, final Type typeOfSrc,
                        final JsonSerializationContext context) {
                    if (src.isEmpty()) {
                        return null;
                    }
                    return context.serialize(src);
                }
            }).registerTypeAdapter(Map.class, new JsonSerializer<Map>() {

                @Override
                public JsonElement serialize(final Map src, final Type typeOfSrc,
                        final JsonSerializationContext context) {
                    if (src.isEmpty()) {
                        return null;
                    }
                    return context.serialize(src);
                }
            }).registerTypeAdapter(DefinitionReference.class, new JsonSerializer<DefinitionReference>() {

                @Override
                public JsonElement serialize(final DefinitionReference src, final Type typeOfSrc,
                        final JsonSerializationContext context) {
                    return context.serialize(src.get().getName());
                }
            }).registerTypeAdapter(Option.class, new JsonSerializer<Option>() {

                @Override
                public JsonElement serialize(final Option src, final Type typeOfSrc,
                        final JsonSerializationContext context) {
                    if (src.isDefined()) {
                        return context.serialize(src.get());
                    }
                    return null; //rien
                }
            }).registerTypeAdapter(Class.class, new JsonSerializer<Class>() {

                @Override
                public JsonElement serialize(final Class src, final Type typeOfSrc,
                        final JsonSerializationContext context) {
                    return new JsonPrimitive(src.getName());
                }
            }).addSerializationExclusionStrategy(new ExclusionStrategy() {
                @Override
                public boolean shouldSkipField(final FieldAttributes arg0) {
                    if (arg0.getAnnotation(JsonExclude.class) != null) {
                        return true;
                    }
                    return false;
                }

                @Override
                public boolean shouldSkipClass(final Class<?> arg0) {
                    return false;
                }
            }).create();
}

From source file:io.weba.processor.flink.event.gson.adapter.MapSerializer.java

License:Open Source License

@Override
public JsonObject serialize(Map<String, Object> map, java.lang.reflect.Type typeOfSrc,
        JsonSerializationContext context) {
    JsonObject jsonObject = new JsonObject();

    for (Map.Entry<String, Object> entry : map.entrySet()) {
        jsonObject.add(CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, entry.getKey()),
                context.serialize(entry.getValue()));
    }// w  w w .j a  v a  2  s.c  om

    return jsonObject;
}

From source file:io.weba.processor.flink.event.gson.adapter.SessionSerializer.java

License:Open Source License

@Override
public JsonObject serialize(Session session, java.lang.reflect.Type typeOfSrc,
        JsonSerializationContext context) {
    JsonObject sessionJsonObject = new JsonObject();
    sessionJsonObject.add("id", new JsonPrimitive(session.id.id.toString()));
    sessionJsonObject.add("start_date", context.serialize(session.startDate));
    sessionJsonObject.add("end_date", context.serialize(session.endDate));

    return sessionJsonObject;
}