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.lanternpowered.server.script.function.condition.json.OrConditionJsonSerializer.java

License:MIT License

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

From source file:org.lanternpowered.server.text.gson.JsonTextLiteralSerializer.java

License:MIT License

static JsonElement serializeLiteralText(Text text, String content, JsonSerializationContext context,
        boolean removeComplexity) {
    final boolean noActionsAndStyle = areActionsAndStyleEmpty(text);
    final ImmutableList<Text> children = text.getChildren();

    if (noActionsAndStyle) {
        if (children.isEmpty()) {
            return new JsonPrimitive(content);
            // Try to make the serialized text object less complex,
            // like text objects nested in a lot of other
            // text objects, this seems to happen a lot
        } else if (removeComplexity && content.isEmpty()) {
            if (children.size() == 1) {
                return context.serialize(children.get(0));
            } else {
                return context.serialize(children);
            }//www.  jav a2 s. c  o  m
        }
    }

    final JsonObject json = new JsonObject();
    json.addProperty(TEXT, content);
    serialize(json, text, context);
    return json;
}

From source file:org.lanternpowered.server.util.option.SimpleOptionValueMapJsonSerializer.java

License:MIT License

@Override
public JsonElement serialize(SimpleOptionValueMap src, Type typeOfSrc, JsonSerializationContext context) {
    final JsonObject json = new JsonObject();
    for (Map.Entry<String, Object> entry : src.values.entrySet()) {
        json.add(entry.getKey(), context.serialize(entry.getValue()));
    }//w w  w. java 2 s .  c  om
    return json;
}

From source file:org.lanternpowered.server.util.option.UnmodifiableOptionValueMapJsonSerializer.java

License:MIT License

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

From source file:org.lanternpowered.server.world.weather.WeatherBuilderJsonSerializer.java

License:MIT License

@Override
public JsonElement serialize(WeatherBuilder src, Type typeOfSrc, JsonSerializationContext context) {
    final JsonObject json = new JsonObject();
    if (!src.aliases.isEmpty()) {
        json.add("aliases", context.serialize(src.aliases));
    }/*w  ww .jav a2s.c  o m*/
    json.add("options", context.serialize(src.options));
    json.add("action", context.serialize(src.action));
    json.addProperty("weight", src.weight);
    if (src.duration != WeatherBuilder.DEFAULT_DURATION) {
        json.add("duration", context.serialize(src.duration));
    }
    return json;
}

From source file:org.mitre.oauth2.view.TokenIntrospection.java

License:Apache License

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    Gson gson = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() {

        @Override/* w  w  w . j  a  va 2  s  .  c  o  m*/
        public boolean shouldSkipField(FieldAttributes f) {
            /*
            if (f.getDeclaringClass().isAssignableFrom(OAuth2AccessTokenEntity.class)) {
               // we don't want to serialize the whole object, just the scope and timeout
               if (f.getName().equals("scope")) {
                  return false;
               } else if (f.getName().equals("expiration")) {
                  return false;
               } else {
                  // skip everything else on this class
                  return true;
               }
            } else {
               // serialize other classes without filter (lists and sets and things)
               return false;
            }
            */
            return false;
        }

        @Override
        public boolean shouldSkipClass(Class<?> clazz) {
            // skip the JPA binding wrapper
            if (clazz.equals(BeanPropertyBindingResult.class)) {
                return true;
            } else {
                return false;
            }
        }

    }).registerTypeAdapter(OAuth2AccessTokenEntity.class, new JsonSerializer<OAuth2AccessTokenEntity>() {
        public JsonElement serialize(OAuth2AccessTokenEntity src, Type typeOfSrc,
                JsonSerializationContext context) {
            JsonObject token = new JsonObject();

            token.addProperty("valid", true);

            JsonArray scopes = new JsonArray();
            for (String scope : src.getScope()) {
                scopes.add(new JsonPrimitive(scope));
            }
            token.add("scope", scopes);

            token.add("expires", context.serialize(src.getExpiration()));

            return token;
        }

    }).setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create();

    response.setContentType("application/json");

    Writer out = response.getWriter();

    Object obj = model.get("entity");
    if (obj == null) {
        obj = model;
    }

    gson.toJson(obj, out);

}

From source file:org.mitre.oauth2.view.TokenIntrospectionView.java

License:Apache License

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) {/*from  w ww . j a va2 s  .  c  o m*/

    Gson gson = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() {

        @Override
        public boolean shouldSkipField(FieldAttributes f) {
            /*
            if (f.getDeclaringClass().isAssignableFrom(OAuth2AccessTokenEntity.class)) {
               // we don't want to serialize the whole object, just the scope and timeout
               if (f.getName().equals("scope")) {
                  return false;
               } else if (f.getName().equals("expiration")) {
                  return false;
               } else {
                  // skip everything else on this class
                  return true;
               }
            } else {
               // serialize other classes without filter (lists and sets and things)
               return false;
            }
             */
            return false;
        }

        @Override
        public boolean shouldSkipClass(Class<?> clazz) {
            // skip the JPA binding wrapper
            if (clazz.equals(BeanPropertyBindingResult.class)) {
                return true;
            } else {
                return false;
            }
        }

    }).registerTypeAdapter(OAuth2AccessTokenEntity.class, new JsonSerializer<OAuth2AccessTokenEntity>() {
        @Override
        public JsonElement serialize(OAuth2AccessTokenEntity src, Type typeOfSrc,
                JsonSerializationContext context) {
            JsonObject token = new JsonObject();

            token.addProperty("active", true);

            token.addProperty("scope", Joiner.on(" ").join(src.getScope()));

            token.add("exp", context.serialize(src.getExpiration()));

            //token.addProperty("audience", src.getAuthenticationHolder().getAuthentication().getAuthorizationRequest().getClientId());

            token.addProperty("sub", src.getAuthenticationHolder().getAuthentication().getName());

            token.addProperty("client_id",
                    src.getAuthenticationHolder().getAuthentication().getOAuth2Request().getClientId());

            token.addProperty("token_type", src.getTokenType());

            return token;
        }

    }).registerTypeAdapter(OAuth2RefreshTokenEntity.class, new JsonSerializer<OAuth2RefreshTokenEntity>() {
        @Override
        public JsonElement serialize(OAuth2RefreshTokenEntity src, Type typeOfSrc,
                JsonSerializationContext context) {
            JsonObject token = new JsonObject();

            token.addProperty("active", true);

            token.addProperty("scope", Joiner.on(" ")
                    .join(src.getAuthenticationHolder().getAuthentication().getOAuth2Request().getScope()));

            token.add("exp", context.serialize(src.getExpiration()));

            //token.addProperty("audience", src.getAuthenticationHolder().getAuthentication().getAuthorizationRequest().getClientId());

            token.addProperty("sub", src.getAuthenticationHolder().getAuthentication().getName());

            token.addProperty("client_id",
                    src.getAuthenticationHolder().getAuthentication().getOAuth2Request().getClientId());

            return token;
        }

    }).setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create();

    response.setContentType("application/json");

    Writer out;

    try {

        out = response.getWriter();
        Object obj = model.get("entity");
        if (obj == null) {
            obj = model;
        }

        gson.toJson(obj, out);

    } catch (IOException e) {

        logger.error("IOException occurred in TokenIntrospectionView.java: ", e);

    }

}

From source file:org.mobicents.servlet.restcomm.http.converter.CallDetailRecordListConverter.java

License:Open Source License

@Override
public JsonObject serialize(CallDetailRecordList cdrList, Type type, JsonSerializationContext context) {

    JsonObject result = new JsonObject();

    JsonArray array = new JsonArray();
    for (CallDetailRecord cdr : cdrList.getCallDetailRecords()) {
        array.add(context.serialize(cdr));
    }/*from   w ww . ja va2s .co m*/

    result.addProperty("page", page);
    result.addProperty("num_pages", getTotalPages());
    result.addProperty("page_size", pageSize);
    result.addProperty("total", total);
    result.addProperty("start", getFirstIndex());
    result.addProperty("end", getLastIndex(cdrList));
    result.addProperty("uri", pathUri);
    result.addProperty("first_page_uri", getFirstPageUri());
    result.addProperty("previous_page_uri", getPreviousPageUri());
    result.addProperty("next_page_uri", getNextPageUri(cdrList));
    result.addProperty("last_page_uri", getLastPageUri());
    result.add("calls", array);

    return result;
}

From source file:org.mobicents.servlet.restcomm.http.converter.ConferenceDetailRecordListConverter.java

License:Open Source License

@Override
public JsonObject serialize(ConferenceDetailRecordList cdrList, Type type, JsonSerializationContext context) {

    JsonObject result = new JsonObject();

    JsonArray array = new JsonArray();
    for (ConferenceDetailRecord cdr : cdrList.getConferenceDetailRecords()) {
        array.add(context.serialize(cdr));
    }// ww w . j a  va 2  s. c o  m

    result.addProperty("page", page);
    result.addProperty("num_pages", getTotalPages());
    result.addProperty("page_size", pageSize);
    result.addProperty("total", total);
    result.addProperty("start", getFirstIndex());
    result.addProperty("end", getLastIndex(cdrList));
    result.addProperty("uri", pathUri);
    result.addProperty("first_page_uri", getFirstPageUri());
    result.addProperty("previous_page_uri", getPreviousPageUri());
    result.addProperty("next_page_uri", getNextPageUri(cdrList));
    result.addProperty("last_page_uri", getLastPageUri());
    result.add("conferences", array);

    return result;
}

From source file:org.mobicents.servlet.restcomm.http.converter.MonitoringServiceConverter.java

License:Open Source License

@Override
public JsonElement serialize(MonitoringServiceResponse monitoringServiceResponse, Type typeOfSrc,
        JsonSerializationContext context) {
    Map<String, Integer> countersMap = monitoringServiceResponse.getCountersMap();
    JsonObject result = new JsonObject();
    JsonObject metrics = new JsonObject();
    JsonArray callsArray = new JsonArray();

    //First add InstanceId and Version details
    result.addProperty("InstanceId", monitoringServiceResponse.getInstanceId().getId().toString());
    result.addProperty("Version", org.mobicents.servlet.restcomm.Version.getVersion());
    result.addProperty("Revision", org.mobicents.servlet.restcomm.Version.getRevision());

    Iterator<String> counterIterator = countersMap.keySet().iterator();
    while (counterIterator.hasNext()) {
        String counter = counterIterator.next();
        metrics.addProperty(counter, countersMap.get(counter));
    }//  w  w w.  j  a v  a 2 s.  c  o  m
    result.add("Metrics", metrics);

    if (monitoringServiceResponse.getCallDetailsList().size() > 0)
        for (CallInfo callInfo : monitoringServiceResponse.getCallDetailsList()) {
            callsArray.add(context.serialize(callInfo));
        }
    result.add("LiveCallDetails", callsArray);
    return result;
}