List of usage examples for com.google.gson JsonSerializationContext serialize
public JsonElement serialize(Object src);
From source file:org.cmg.jresp.json.SCELJsonUtil.java
License:Open Source License
/** * Serialize an object into a {@link JsonElement}. The object is rendered as * a {@link JsonObject} containing two attributes: * <ul>//from ww w . j ava2 s. co m * <li><code>type</code>, containing a string with the fully qualified name * of the serialized object; * <li><code>value</code>, containing the {@link JsonElement} associated to * the serialized object. * </ul> * * When the object will be deserialized, the first attribute will be used to * identify the object class, while the second one will be used to retrieve * object status. * * * @param o * object to serialize * @param context * Context for serialization * @return a json representation of o */ public static JsonElement jsonFromObject(Object o, JsonSerializationContext context) { JsonObject json = new JsonObject(); json.add(SCELJsonUtil.TYPE_ID, new JsonPrimitive(o.getClass().getName())); json.add(SCELJsonUtil.VALUE_ID, context.serialize(o)); return json; }
From source file:org.commonjava.couch.io.json.CouchDocumentActionAdapter.java
License:Apache License
@Override public JsonElement serialize(final CouchDocumentAction src, final Type typeOfSrc, final JsonSerializationContext context) { return context.serialize(src.getDocument()); }
From source file:org.commonjava.web.json.ser.ListingAdapter.java
License:Open Source License
@Override public JsonElement serialize(final Listing<?> src, final Type typeOfSrc, final JsonSerializationContext context) { final JsonObject result = new JsonObject(); final JsonArray array = new JsonArray(); result.add("items", array); final List<?> items = src.getItems(); for (final Object item : items) { final JsonElement element = context.serialize(item); array.add(element);/*from ww w. j a v a2s . co m*/ } return result; }
From source file:org.diorite.config.serialization.JsonSerializerImpl.java
License:Open Source License
@Override public JsonElement serialize(T t, Type type, JsonSerializationContext jsonSerializationContext) { try {//from w w w. ja v a 2 s .c om SimpleSerializationData data = (SimpleSerializationData) SerializationData .create(SerializationType.JSON, this.serialization, this.serializer.getType()); this.serializer.serialize(t, data); return jsonSerializationContext.serialize(data.rawValue()); } catch (SerializationException e) { throw e; } catch (Exception e) { throw new SerializationException(this.serializer.getType(), e.getMessage(), e); } }
From source file:org.eclipse.che.api.local.ProjectConfigAdapter.java
License:Open Source License
private JsonElement serializeModule(ProjectConfig moduleConfig, JsonSerializationContext context) { JsonObject object = new JsonObject(); object.addProperty("name", moduleConfig.getName()); object.addProperty("path", moduleConfig.getPath()); object.addProperty("type", moduleConfig.getType()); object.add("attributes", context.serialize(moduleConfig.getAttributes())); object.add("source", context.serialize(moduleConfig.getSource())); return object; }
From source file:org.eclipse.che.dto.server.NullOrEmptyCollectionAdapter.java
License:Open Source License
@Override public JsonElement serialize(List<?> src, Type typeOfSrc, JsonSerializationContext context) { if (src == null || src.isEmpty()) { return null; }//from w w w .ja va 2 s .com JsonArray array = new JsonArray(); for (Object child : src) { JsonElement element = context.serialize(child); array.add(element); } return array; }
From source file:org.eclipse.che.dto.server.NullOrEmptyMapAdapter.java
License:Open Source License
@Override public JsonElement serialize(Map<?, ?> src, Type typeOfSrc, JsonSerializationContext context) { if (src == null || src.isEmpty()) { return null; }/*from w w w.jav a 2 s. co m*/ JsonObject object = new JsonObject(); for (Map.Entry<?, ?> entry : src.entrySet()) { object.add(entry.getKey().toString(), context.serialize(entry.getValue())); } return object; }
From source file:org.eclipse.leshan.core.model.json.ObjectModelSerializer.java
License:Open Source License
@Override public JsonElement serialize(ObjectModel object, Type typeOfSrc, JsonSerializationContext context) { JsonObject element = new JsonObject(); // sort resources value List<ResourceModel> resourceSpecs = new ArrayList<ResourceModel>(object.resources.values()); Collections.sort(resourceSpecs, new Comparator<ResourceModel>() { @Override/*from w w w . j a v a 2 s . com*/ public int compare(ResourceModel r1, ResourceModel r2) { return r1.id - r2.id; } }); // serialize fields element.addProperty("name", object.name); element.addProperty("id", object.id); element.addProperty("instancetype", object.multiple ? "multiple" : "single"); element.addProperty("mandatory", object.mandatory); element.addProperty("description", object.description); element.add("resourcedefs", context.serialize(resourceSpecs)); return element; }
From source file:org.eclipse.leshan.core.objectspec.json.ObjectSpecSerializer.java
License:Open Source License
@Override public JsonElement serialize(ObjectSpec object, Type typeOfSrc, JsonSerializationContext context) { JsonObject element = new JsonObject(); // sort resources value List<ResourceSpec> resourceSpecs = new ArrayList<ResourceSpec>(object.resources.values()); Collections.sort(resourceSpecs, new Comparator<ResourceSpec>() { @Override/*w ww . j av a 2s .c o m*/ public int compare(ResourceSpec r1, ResourceSpec r2) { return r1.id - r2.id; } }); // serialize fields element.addProperty("name", object.name); element.addProperty("id", object.id); element.addProperty("instancetype", object.multiple ? "multiple" : "single"); element.addProperty("mandatory", object.mandatory); element.addProperty("description", object.description); element.add("resourcedefs", context.serialize(resourceSpecs)); return element; }
From source file:org.eclipse.leshan.server.demo.servlet.json.ClientSerializer.java
License:Open Source License
@Override public JsonElement serialize(Client src, Type typeOfSrc, JsonSerializationContext context) { JsonObject element = new JsonObject(); element.addProperty("endpoint", src.getEndpoint()); element.addProperty("registrationId", src.getRegistrationId()); element.add("registrationDate", context.serialize(src.getRegistrationDate())); element.add("lastUpdate", context.serialize(src.getLastUpdate())); element.addProperty("address", src.getAddress().getHostAddress() + ":" + src.getPort()); element.addProperty("smsNumber", src.getSmsNumber()); element.addProperty("lwM2mVersion", src.getLwM2mVersion()); element.addProperty("lifetime", src.getLifeTimeInSec()); element.addProperty("bindingMode", src.getBindingMode().toString()); element.add("rootPath", context.serialize(src.getRootPath())); element.add("objectLinks", context.serialize(src.getSortedObjectLinks())); element.add("secure", context.serialize(src.getRegistrationEndpointAddress().getPort() == securePort)); element.add("additionalRegistrationAttributes", context.serialize(src.getAdditionalRegistrationAttributes())); return element; }