下面列出了怎么用com.google.gson.JsonSerializationContext的API类实例代码及写法,或者点击链接到github查看源代码。
@SuppressWarnings("rawtypes")
@Override
public JsonElement serialize(SimplePrincipalCollection src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject response = new JsonObject();
JsonArray principals = new JsonArray();
Set<String> realms = src.getRealmNames();
if (realms != null) {
for (String realm : realms) {
JsonObject jsonRealm = new JsonObject();
JsonArray realmPrincipals = new JsonArray();
Collection principalCollection = src.fromRealm(realm);
if (principalCollection != null && !principalCollection.isEmpty()) {
for (Object value : principalCollection) {
realmPrincipals.add(context.serialize(value));
}
}
jsonRealm.add(realm, realmPrincipals);
principals.add(jsonRealm);
}
}
response.add(ATTR_PRINCIPAL_MAP, principals);
return response;
}
@Override
public JsonElement serialize(CounterGroup cg, Type t, JsonSerializationContext ctx) {
JsonObject obj = new JsonObject();
if (!cg.getName().equals(cg.getDisplayName()))
obj.addProperty("displayName", cg.getDisplayName());
JsonObject dns = new JsonObject();
boolean anyNamesDiffer = false;
for (Counter c : cg) {
obj.addProperty(c.getName(), c.getValue());
if (!c.getName().equals(c.getDisplayName()))
anyNamesDiffer = true;
dns.addProperty(c.getName(), c.getDisplayName());
}
if (anyNamesDiffer)
obj.add("displayNames", dns);
return obj;
}
@Override
public JsonObject serialize(final S state, final Type type, final JsonSerializationContext context)
{
final JsonObject map = new JsonObject();
map.add(COMPOSITE_KEY, context.serialize(state.compositeProperty().get()));
map.add(CONVERTER_KEY, context.serialize(state.converter()));
map.addProperty(COMPOSITE_TYPE_KEY, state.compositeProperty().get().getClass().getName());
map.addProperty(CONVERTER_TYPE_KEY, state.converter().getClass().getName());
map.add(INTERPOLATION_KEY, context.serialize(state.interpolationProperty().get()));
map.addProperty(IS_VISIBLE_KEY, state.isVisibleProperty().get());
map.addProperty(SOURCE_TYPE_KEY, state.getDataSource().getClass().getName());
map.add(SOURCE_KEY, context.serialize(state.getDataSource()));
map.addProperty(NAME_KEY, state.nameProperty().get());
map.add(DEPENDS_ON_KEY, context.serialize(getDependencies(state)));
return map;
}
private static JsonObject convertPc(ReflexActionSendProtocol pr, JsonSerializationContext context) {
JsonObject json = new JsonObject();
json.addProperty("t", "PC");
switch (pr.getType()) {
case ZWAVE:
json.addProperty("p", "ZW");
break;
case ZIGBEE:
json.addProperty("p", "ZB");
break;
default:
throw new RuntimeException("unknown send protocol type: " + pr.getType());
}
json.addProperty("m", Base64.encodeBase64String(pr.getMessage()));
return json;
}
@Override
public JsonElement serialize(ReflexDriverDefinition driver, Type typeOfSrc, JsonSerializationContext context) {
JsonObject drv = new JsonObject();
drv.addProperty("fmt", LATEST.num);
drv.addProperty("n", driver.getName());
drv.addProperty("v", driver.getVersion().getRepresentation());
drv.addProperty("h", driver.getHash());
drv.addProperty("o", driver.getOfflineTimeout());
drv.addProperty("m", driver.getMode().name());
drv.add("c", context.serialize(ImmutableList.copyOf(driver.getCapabilities()), LIST_CAPS));
drv.add("r", context.serialize(driver.getReflexes(), LIST_REFLEXES));
if (driver.getDfa() != null) {
drv.add("d", context.serialize(driver.getDfa(), REFLEX_DFA));
}
return drv;
}
@Override
public JsonElement serialize(ScreenScalesConfig screenScalesConfig, Type type, JsonSerializationContext jsonSerializationContext) {
LOG.debug("Serializing {}", screenScalesConfig);
final JsonObject obj = new JsonObject();
Optional
.ofNullable(screenScalesConfig.screenScalesProperty().get())
.map(scales -> scales.getScalesCopy())
.ifPresent(scales -> obj.add(SCALES_KEY, jsonSerializationContext.serialize(scales)));
return obj;
}
/**
* Returns serialized json element containing the allocated group ranges
*
* @param context Serializer context
* @param ranges allocated group range
*/
private JsonElement serializeAllocatedGroupRanges(@NonNull final JsonSerializationContext context,
@NonNull final List<AllocatedGroupRange> ranges) {
final Type allocatedGroupRanges = new TypeToken<List<AllocatedGroupRange>>() {
}.getType();
return context.serialize(ranges, allocatedGroupRanges);
}
private static JsonObject convertAt(ReflexMatchAttribute at, JsonSerializationContext context) {
JsonObject json = new JsonObject();
json.addProperty("t", "AT");
json.addProperty("a", at.getAttr());
json.add("v", context.serialize(at.getValue()));
return json;
}
public JsonElement serialize(ConstructId src, Type typeOfSrc, JsonSerializationContext context) {
final JsonObject c = new JsonObject();
c.addProperty("lang", src.getLanguage().toString());
c.addProperty("type", JavaId.typeToString(((JavaId)src).getType()));
c.addProperty("qname", src.getQualifiedName());
final Set<String> annotations = ((JavaId)src).getAnnotations();
if(!annotations.isEmpty()) {
final JsonArray anno = new JsonArray();
for(String a: annotations) {
anno.add(new JsonPrimitive(a));
}
c.add("a", anno);
}
return c;
}
private static JsonObject convertAl(ReflexActionAlertmeLifesign al, JsonSerializationContext context) {
JsonObject json = new JsonObject();
json.addProperty("t", "AL");
json.addProperty("a", al.getType().name());
if (al.getMinimum() != null) {
json.addProperty("m", al.getMinimum());
}
if (al.getNominal() != null) {
json.addProperty("n", al.getNominal());
}
return json;
}
@Override
public JsonElement serialize(final CrosshairConfig src, final Type typeOfSrc, final JsonSerializationContext
context)
{
final JsonObject map = new JsonObject();
map.addProperty(ON_FOCUS_COLOR_KEY, Colors.toHTML(src.getOnFocusColor()));
map.addProperty(OFF_FOCUS_COLOR_KEY, Colors.toHTML(src.getOutOfFocusColor()));
map.addProperty(VISIBLE_KEY, src.getShowCrosshairs());
return map;
}
@Override
public JsonElement serialize(TimeZone src, Type typeOfSrc, JsonSerializationContext context) {
try {
return src == null ? null : new JsonPrimitive(src.getID());
} catch (Exception e) {
logger.log(SentryLevel.ERROR, "Error when serializing TimeZone", e);
}
return null;
}
@Override
public JsonElement serialize(SentryId src, Type typeOfSrc, JsonSerializationContext context) {
try {
return src == null ? null : new JsonPrimitive(src.toString());
} catch (Exception e) {
logger.log(SentryLevel.ERROR, "Error when serializing SentryId", e);
}
return null;
}
@Override
public JsonElement serialize(List<TradeInfo> tradeInfoList, Type type, JsonSerializationContext jsonSerializationContext) {
if (Utils.isListEmpty(tradeInfoList)) {
return null;
}
TradeInfo tradeInfo = tradeInfoList.get(0);
if (tradeInfo instanceof PosTradeInfo) {
return new JsonPrimitive(StringUtils.join(tradeInfoList, ""));
}
return jsonSerializationContext.serialize(tradeInfoList);
}
@Override
public JsonElement serialize(BookmarkConfig config, Type typeOfSrc, JsonSerializationContext context) {
final JsonObject map = new JsonObject();
final List<BookmarkConfig.Bookmark> bookmarks = new ArrayList<>(config.getUnmodifiableBookmarks());
if (bookmarks.size() > 0)
map.add(BOOKMARKS_KEY, context.serialize(bookmarks.toArray()));
map.add(TRANSITION_TIME_KEY, context.serialize(config.getTransitionTime()));
return map;
}
@Override
public JsonElement serialize(Collection<?> src, Type typeOfSrc,
JsonSerializationContext context) {
if (src == null || src.isEmpty()) {
return null;
}
JsonArray array = new JsonArray();
for (Object child : src) {
JsonElement element = context.serialize(child);
array.add(element);
}
return array;
}
@Override
public JsonElement serialize(FieldInsnNode src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject jsonObject = new JsonObject();
jsonObject.add("opcode", context.serialize(src.getOpcode(), Integer.class));
jsonObject.add("owner", context.serialize(src.owner, String.class));
jsonObject.add("name", context.serialize(src.name, String.class));
jsonObject.add("desc", context.serialize(src.desc, String.class));
return jsonObject;
}
@Override
public JsonElement serialize(GetParameterInfoCommand cmd, Type typeOfSrc,
JsonSerializationContext context) {
final JsonObject object = new JsonObject();
object.add("command", context.serialize(cmd.getCommand()));
if (cmd.getTxnid() != null) object.add("txnid", context.serialize(cmd.getTxnid()));
return object;
}
@Override
public JsonElement serialize(GetDeviceInfoCommand cmd, Type typeOfSrc,
JsonSerializationContext context) {
final JsonObject object = new JsonObject();
object.add("command", context.serialize(cmd.getCommand()));
if (cmd.getTxnid() != null) object.add("txnid", context.serialize(cmd.getTxnid()));
return object;
}
public JsonElement serialize(T object, Type typeOfSrc, JsonSerializationContext context) {
JsonElement el = context.serialize(object, object.getClass());
JsonObject result = el.getAsJsonObject();
String discr = reverseSubclasses.get(object.getClass());
if (discr == null) {
throw new JsonParseException("Invalid subclass " + object.getClass());
}
result.addProperty(discriminator, discr);
return result;
}
public JsonElement serialize(SetDeviceInfoCommand cmd, Type typeOfSrc, JsonSerializationContext context) {
final JsonObject object = new JsonObject();
object.add("command", context.serialize(cmd.getCommand()));
if (cmd.getTxnid() != null) object.add("txnid", context.serialize(cmd.getTxnid()));
if (cmd.getValues() != null) object.add("values", context.serialize(cmd.getValues()));
return object;
}
@Override
public JsonElement serialize(GetReportConfigurationCommand cmd,
Type typeOfSrc, JsonSerializationContext context) {
final JsonObject object = new JsonObject();
object.add("command", context.serialize(cmd.getCommand()));
if (cmd.getTxnid() != null) object.add("txnid", context.serialize(cmd.getTxnid()));
return object;
}
@Override
public JsonElement serialize(RebootCommand cmd, Type typeOfSrc,
JsonSerializationContext context) {
final JsonObject object = new JsonObject();
object.add("command", context.serialize(cmd.getCommand()));
if (cmd.getTxnid() != null) object.add("txnid", context.serialize(cmd.getTxnid()));
return object;
}
@Override
public JsonElement serialize(Double src, Type type,
JsonSerializationContext context) {
if (src % 1 == 0) {
return new JsonPrimitive(src.intValue());
}
else {
return new JsonPrimitive(src);
}
}
@Override
public JsonElement serialize(WxMpMassVideo message, Type typeOfSrc, JsonSerializationContext context) {
JsonObject messageJson = new JsonObject();
messageJson.addProperty("media_id", message.getMediaId());
messageJson.addProperty("description", message.getDescription());
messageJson.addProperty("title", message.getTitle());
return messageJson;
}
@Override
public JsonElement serialize(operation_type src, Type typeOfSrc, JsonSerializationContext context) {
JsonArray jsonArray = new JsonArray();
jsonArray.add(src.nOperationType);
Type type = operations_map.getOperationObjectById(src.nOperationType);
assert(type != null);
jsonArray.add(context.serialize(src.operationContent, type));
return jsonArray;
}
@Override
public JsonElement serialize(Multimap<String,String> src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject mm = new JsonObject();
for (Entry<String,Collection<String>> e : src.asMap().entrySet()) {
JsonArray values = new JsonArray();
Collection<String> filtered = Collections2.filter(e.getValue(), Predicates.notNull());
for (String value : filtered)
values.add(new JsonPrimitive(value));
mm.add(e.getKey(), values);
}
return mm;
}
@Override
public JsonElement serialize(WxMpTemplateMessage message, Type typeOfSrc, JsonSerializationContext context) {
JsonObject messageJson = new JsonObject();
messageJson.addProperty("touser", message.getToUser());
messageJson.addProperty("template_id", message.getTemplateId());
if (message.getUrl() != null) {
messageJson.addProperty("url", message.getUrl());
}
if (message.getMiniProgram() != null) {
JsonObject miniProgramJson = new JsonObject();
miniProgramJson.addProperty("appid", message.getMiniProgram().getAppid());
miniProgramJson.addProperty("pagepath", message.getMiniProgram().getPagePath());
messageJson.add("miniprogram", miniProgramJson);
}
JsonObject data = new JsonObject();
messageJson.add("data", data);
for (WxMpTemplateData datum : message.getData()) {
JsonObject dataJson = new JsonObject();
dataJson.addProperty("value", datum.getValue());
if (datum.getColor() != null) {
dataJson.addProperty("color", datum.getColor());
}
data.add(datum.getName(), dataJson);
}
return messageJson;
}
@Override
public JsonElement serialize(VarInsnNode src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject object = new JsonObject();
object.add("opcode", context.serialize(src.getOpcode()));
object.add("var", context.serialize(src.var));
return object;
}
@Override
public JsonElement serialize(FrameNode src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject object = new JsonObject();
object.add("opcode", context.serialize(src.getOpcode()));
object.add("type", context.serialize(src.type));
object.add("local", context.serialize(src.local));
object.add("stack", context.serialize(src.stack));
return object;
}