List of usage examples for com.google.gson JsonSerializationContext serialize
public JsonElement serialize(Object src);
From source file:org.smartparam.manager.json.vendor.gson.DescribedEntitySerializer.java
License:Apache License
@Override public JsonElement serialize(DescribedEntity entity, Type typeOfSrc, JsonSerializationContext context) { JsonObject object = new JsonObject(); object.add("source", context.serialize(entity.source())); object.add("data", context.serialize(entity.data())); return object; }
From source file:org.smartparam.manager.json.vendor.gson.MapEntrySerializer.java
License:Apache License
@Override public JsonElement serialize(MapEntry src, Type typeOfSrc, JsonSerializationContext context) { return context.serialize(src.rawValues()); }
From source file:org.smartparam.manager.json.vendor.gson.RangeSerializer.java
License:Apache License
@Override public JsonElement serialize(Range<?> range, Type type, JsonSerializationContext context) { JsonObject object = new JsonObject(); object.addProperty("type", evaluateType(range)); object.add("from", context.serialize(range.from())); object.add("to", context.serialize(range.to())); return object; }
From source file:org.springframework.data.cloudant.core.UnmappedDataAdapter.java
License:Apache License
@Override public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) { JsonObject member = new JsonObject(); if (src instanceof CustomDocType) { member.addProperty("doc_type", ((CustomDocType) src).getDocType()); } else {/*from w ww . ja v a 2 s .c o m*/ member.addProperty("doc_type", src.getClass().getSimpleName()); } member.addProperty("_id", src.getId()); member.addProperty("_rev", src.getRevision()); ArrayList<Field> fields = new ArrayList(); fields = getNestedFieldList(src.getClass(), fields); for (Field field : fields) { try { field.setAccessible(true); Object fieldVal = field.get(src); member.add(field.getName(), context.serialize(fieldVal)); } catch (IllegalAccessException e) { e.printStackTrace(); continue; } } Map<String, Object> unmappedProperties = src.getUnmappedFields(); if (unmappedProperties != null) { for (Map.Entry<String, Object> entry : unmappedProperties.entrySet()) { // entry.setValue(entry.getValue().toString()); member.add(entry.getKey(), context.serialize(entry.getValue())); } } return member; }
From source file:org.sprintapi.hyperdata.gson.GsonHyperDataSerializer.java
License:Apache License
@Override public JsonElement serialize(HyperMap src, Type typeOfSrc, JsonSerializationContext context) { if (src == null) { return JsonNull.INSTANCE; }//from w w w. j a v a 2s . co m JsonObject obj = new JsonObject(); final Map<String, Object> metadata = src.getMetadata(); if (metadata != null) { for (String name : metadata.keySet()) { Object value = metadata.get(name); obj.add(Constants.META_CHAR.concat(name), context.serialize(value)); } } for (String name : src.names()) { Object value = src.get(name); obj.add(name, context.serialize(value)); } return obj; }
From source file:org.stjs.server.json.gson.JSArrayAdapter.java
License:Apache License
@Override public JsonElement serialize(Array<?> array, Type type, JsonSerializationContext ctx) { if (array == null) { return new JsonNull(); }// ww w . jav a2s. c o m JsonArray js = new JsonArray(); for (String i : array) { js.add(ctx.serialize(array.$get(i))); } return js; }
From source file:org.stjs.server.json.gson.JSMapAdapter.java
License:Apache License
@SuppressWarnings("unchecked") @Override//from www. j a v a2s . c o m public JsonElement serialize(Map<?, ?> map, Type typeOfSrc, JsonSerializationContext ctx) { if (map == null) { return new JsonNull(); } JsonObject js = new JsonObject(); for (Object k : map) { js.add(k.toString(), ctx.serialize(((Map) map).$get(k.toString()))); } return js; }
From source file:org.terasology.logic.behavior.core.BehaviorTreeBuilder.java
License:Apache License
@Override public JsonElement serialize(BehaviorNode src, Type typeOfSrc, JsonSerializationContext context) { JsonObject node = new JsonObject(); if (src instanceof DelegateNode) { DelegateNode delegateNode = (DelegateNode) src; return serialize(delegateNode.delegate, BehaviorNode.class, context); }// w w w . ja v a2s. com if (src instanceof CompositeNode) { String name = src.getName(); JsonArray array = new JsonArray(); for (int i = 0; i < src.getChildrenCount(); i++) { array.add(serialize(src.getChild(i), BehaviorNode.class, context)); } node.add(name, array); } else if (src instanceof ActionNode) { ActionNode actionNode = (ActionNode) src; JsonObject content; String name; if (actionNode.action != null) { content = (JsonObject) context.serialize(actionNode.action); name = actionNode.action.getName(); } else { content = new JsonObject(); name = actionNode.getName(); } if (src instanceof DecoratorNode) { DecoratorNode decoratorNode = (DecoratorNode) src; if (decoratorNode.getChildrenCount() > 0) { content.add("child", serialize(decoratorNode.getChild(0), BehaviorNode.class, context)); } } node.add(name, content); } else { return new JsonPrimitive(src.getName()); } return node; }
From source file:org.terasology.naming.gson.NameTypeAdapter.java
License:Apache License
@Override public JsonElement serialize(Name src, Type typeOfSrc, JsonSerializationContext context) { return context.serialize(src.toString()); }
From source file:org.terasology.naming.gson.VersionTypeAdapter.java
License:Apache License
@Override public JsonElement serialize(Version src, Type typeOfSrc, JsonSerializationContext context) { return context.serialize(src.toString()); }