List of usage examples for com.google.gson JsonSerializationContext serialize
public JsonElement serialize(Object src);
From source file:com.google.nigori.common.TypeAdapterByteString.java
License:Apache License
@Override public JsonElement serialize(ByteString src, Type typeOfSrc, JsonSerializationContext context) { return context.serialize(new String(Base64.encodeBase64(src.toByteArray()))); }
From source file:com.google.nigori.common.TypeAdapterProtobuf.java
License:Apache License
private JsonElement serializeWithEnumRewrite(JsonSerializationContext context, Object o) { if (o instanceof EnumValueDescriptor) { return context.serialize(((EnumValueDescriptor) o).getName()); } else {//from w w w . j a v a2 s . c o m return context.serialize(o); } }
From source file:com.google.wave.api.event.EventSerializer.java
License:Apache License
/** * Serializes the given {@link Event} into a {@link JsonObject}. * * @param event the {@link Event} to be serialized. * @param context the serialization context. * @return an instance of {@link JsonObject}, that is the JSON representation * of the given {@link Event}./*from w w w . ja v a2 s.c om*/ */ public static JsonObject serialize(Event event, JsonSerializationContext context) throws EventSerializationException { JsonObject result = new JsonObject(); // Serialize basic properties from Event. result.addProperty(TYPE, event.getType().name()); result.addProperty(MODIFIED_BY, event.getModifiedBy()); result.addProperty(TIMESTAMP, event.getTimestamp()); result.addProperty(TIMESTAMP, event.getTimestamp()); // Construct a properties object. JsonObject properties = new JsonObject(); try { // Serialize the blip id. Field blipIdField = AbstractEvent.class.getDeclaredField(BLIP_ID); blipIdField.setAccessible(true); properties.addProperty(BLIP_ID, (String) blipIdField.get(event)); // Serialize event specific properties. for (Field field : event.getClass().getDeclaredFields()) { field.setAccessible(true); properties.add(field.getName(), context.serialize(field.get(event))); } } catch (IllegalArgumentException e) { throw new EventSerializationException( "Unable to serialize event: " + BLIP_ID + " in " + event.getClass() + " is not accessible."); } catch (IllegalAccessException e) { throw new EventSerializationException( "Unable to serialize event: " + BLIP_ID + " in " + event.getClass() + " is not accessible."); } catch (NoSuchFieldException e) { throw new EventSerializationException( "Unable to serialize event: " + BLIP_ID + " in " + event.getClass() + " is not accessible."); } result.add(PROPERTIES, properties); return result; }
From source file:com.google.wave.api.impl.ElementGsonAdaptor.java
License:Apache License
@Override public JsonElement serialize(Element src, Type typeOfSrc, JsonSerializationContext context) { JsonObject jsonObject = new JsonObject(); jsonObject.addProperty(TYPE_TAG, src.getType().toString()); JsonObject properties = new JsonObject(); if (src.isAttachment()) { Attachment attachment = (Attachment) src; if (attachment.hasData()) { byte[] encodedData = Base64.encodeBase64(attachment.getData()); try { properties.add(Attachment.DATA, new JsonPrimitive(new String(encodedData, "UTF-8"))); } catch (UnsupportedEncodingException e) { // this shouldn't happen, so let it slide. }//ww w .ja va 2 s .c o m } } for (Entry<String, String> entry : src.getProperties().entrySet()) { // Note: Gson's JsonObject and MapTypeAdapter don't escape the key // automatically, so we have to manually escape it here by calling // JsonSerializationContext.serialize(). Also, unfortunately, calling // JsonPrimitive.toString() wraps the text inside double quotes, that we // need to strip out. String quotedKey = context.serialize(entry.getKey()).toString(); String key = quotedKey.substring(1, quotedKey.length() - 1); JsonElement value = context.serialize(entry.getValue()); properties.add(key, value); } jsonObject.add(PROPERTIES_TAG, properties); return jsonObject; }
From source file:com.google.wave.api.impl.EventMessageBundleGsonAdaptor.java
License:Apache License
@Override public JsonElement serialize(EventMessageBundle src, Type typeOfSrc, JsonSerializationContext context) { JsonObject result = new JsonObject(); JsonArray events = new JsonArray(); for (Event event : src.getEvents()) { try {/*from w w w .ja v a 2 s.com*/ events.add(EventSerializer.serialize(event, context)); } catch (EventSerializationException e) { throw new JsonParseException(e); } } result.add(EVENTS_TAG, events); result.add(WAVELET_TAG, context.serialize(src.getWaveletData())); result.add(BLIPS_TAG, context.serialize(src.getBlipData())); result.add(THREADS_TAG, context.serialize(src.getThreads())); result.addProperty(ROBOT_ADDRESS_TAG, src.getRobotAddress()); String proxyingFor = src.getProxyingFor(); if (proxyingFor != null && !proxyingFor.isEmpty()) { result.addProperty(PROXYING_FOR_TAG, proxyingFor); } String rpcServerUrl = src.getRpcServerUrl(); if (rpcServerUrl != null && !rpcServerUrl.isEmpty()) { result.addProperty(RPC_SERVER_URL_TAG, rpcServerUrl); } return result; }
From source file:com.google.wave.api.impl.JsonRpcResponseGsonAdaptor.java
License:Apache License
@Override public JsonElement serialize(JsonRpcResponse src, Type type, JsonSerializationContext context) { JsonObject result = new JsonObject(); result.addProperty(ResponseProperty.ID.key(), src.getId()); if (src.isError()) { JsonObject error = new JsonObject(); error.addProperty(ParamsProperty.MESSAGE.key(), src.getErrorMessage()); result.add(ResponseProperty.ERROR.key(), error); } else {//www.java 2 s . co m JsonObject data = new JsonObject(); for (Entry<ParamsProperty, Object> properties : src.getData().entrySet()) { data.add(properties.getKey().key(), context.serialize(properties.getValue())); } result.add(ResponseProperty.DATA.key(), data); } return result; }
From source file:com.google.wave.api.impl.OperationRequestGsonAdaptor.java
License:Apache License
@Override public JsonElement serialize(OperationRequest req, Type type, JsonSerializationContext ctx) { JsonObject object = new JsonObject(); object.addProperty(RequestProperty.METHOD.key(), operationNamespace + req.getMethod()); object.addProperty(RequestProperty.ID.key(), req.getId()); JsonObject parameters = new JsonObject(); for (Entry<ParamsProperty, Object> entry : req.getParams().entrySet()) { if (entry.getValue() != null) { parameters.add(entry.getKey().key(), ctx.serialize(entry.getValue())); }// ww w . jav a 2s . c o m } object.add(RequestProperty.PARAMS.key(), parameters); return object; }
From source file:com.heroiclabs.sdk.android.util.json.DatastoreItemPermissionsReadJsonAdapter.java
License:Apache License
@Override public JsonElement serialize(final @NonNull DatastoreItem.Permissions.Read src, final @NonNull Type typeOfSrc, final @NonNull JsonSerializationContext context) { return context.serialize(src.getKey()); }
From source file:com.heroiclabs.sdk.android.util.json.DatastoreItemPermissionsWriteJsonAdapter.java
License:Apache License
@Override public JsonElement serialize(final @NonNull DatastoreItem.Permissions.Write src, final @NonNull Type typeOfSrc, final @NonNull JsonSerializationContext context) { return context.serialize(src.getKey()); }
From source file:com.ibm.common.activitystreams.internal.EnumAdapter.java
License:Apache License
/** * Method serialize./*from ww w .ja v a 2 s . c o m*/ * @param src E * @param typeOfSrc Type * @param context JsonSerializationContext * @return JsonElement */ public JsonElement serialize(E src, Type typeOfSrc, JsonSerializationContext context) { return context.serialize(ser.convert(src)); }