List of usage examples for com.google.gson JsonObject addProperty
public void addProperty(String property, Character value)
From source file:ca.paullalonde.gocd.sns_plugin.executors.GetViewRequestExecutor.java
License:Apache License
@Override public GoPluginApiResponse execute() throws Exception { JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("template", Util.readResource("/plugin-settings.template.html")); DefaultGoPluginApiResponse defaultGoPluginApiResponse = new DefaultGoPluginApiResponse(200); defaultGoPluginApiResponse.setResponseBody(GSON.toJson(jsonObject)); return defaultGoPluginApiResponse; }
From source file:ca.ualberta.cmput301w14t08.geochan.json.BitmapJsonConverter.java
License:Apache License
/** * Serializes a Bitmap to a base64 JSON string. * /*from ww w.j a v a 2s. co m*/ * @param bitmap * the Bitmap to serialize * @param type * the type * @param jsc * the JSON serialization context * * @return a JsonElement representing the serialized Bitmap. */ @Override public JsonElement serialize(Bitmap bitmap, Type type, JsonSerializationContext jsc) { JsonObject object = new JsonObject(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 90, byteArrayOutputStream); byte[] byteArray = byteArrayOutputStream.toByteArray(); String encoded = Base64.encodeToString(byteArray, Base64.NO_WRAP); object.addProperty("image", encoded); return object; }
From source file:ca.ualberta.cmput301w14t08.geochan.json.CommentJsonConverter.java
License:Apache License
/** * Serializes a Comment object into JSON format. * //w w w . ja v a2s. c om * @param comment * the Comment to serialize * @param type * the Type * @param context * the JSON serialization context * * @return A JsonElement representing the serialized Comment. */ @Override public JsonElement serialize(Comment comment, Type type, JsonSerializationContext context) { JsonObject object = new JsonObject(); object.addProperty("commentDate", comment.getCommentDate().getTime()); if (comment.getLocation() != null) { object.addProperty("location", comment.getLocation().getLatitude() + "," + comment.getLocation().getLongitude()); if (comment.getLocation().getLocationDescription() != null) { object.addProperty("locationDescription", comment.getLocation().getLocationDescription()); } } else { object.addProperty("location", "-999,-999"); } object.addProperty("user", comment.getUser()); object.addProperty("hash", comment.getHash()); object.addProperty("id", comment.getId()); object.addProperty("textPost", comment.getTextPost()); object.addProperty("hasImage", comment.hasImage()); if (comment.hasImage()) { /* * http://stackoverflow.com/questions/9224056/android-bitmap-to-base64 * -string * * Serialize just the thumbnail as the image is serialized * separately */ Bitmap bitmapThumb = comment.getImageThumb(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); bitmapThumb.compress(Bitmap.CompressFormat.JPEG, 90, byteArrayOutputStream); byte[] byteThumbArray = byteArrayOutputStream.toByteArray(); String encodedThumb = Base64.encodeToString(byteThumbArray, Base64.NO_WRAP); object.addProperty("imageThumbnail", encodedThumb); } object.addProperty("depth", comment.getDepth()); if (comment.getParent() != null) { object.addProperty("parent", comment.getParent().getId()); } return object; }
From source file:ca.ualberta.cmput301w14t08.geochan.json.CommentOfflineJsonConverter.java
License:Apache License
/** * Serializes a Comment object into JSON format (for offline storage). * // w ww .j a va 2s.c om * @param comment * the Comment to serialize * @param type * the Type * @param context * the JSON serialization context * * @return A JsonElement representing the serialized Comment. */ @Override public JsonElement serialize(Comment comment, Type type, JsonSerializationContext context) { JsonObject object = new JsonObject(); object.addProperty("commentDate", comment.getCommentDate().getTime()); if (comment.getLocation() != null) { object.addProperty("location", comment.getLocation().getLatitude() + "," + comment.getLocation().getLongitude()); if (comment.getLocation().getLocationDescription() != null) { object.addProperty("locationDescription", comment.getLocation().getLocationDescription()); } } else { object.addProperty("location", "-999,-999"); } object.addProperty("user", comment.getUser()); object.addProperty("hash", comment.getHash()); object.addProperty("id", comment.getId()); object.addProperty("textPost", comment.getTextPost()); object.addProperty("hasImage", comment.hasImage()); if (comment.hasImage()) { Bitmap bitmapThumb = comment.getImageThumb(); /* * http://stackoverflow.com/questions/9224056/android-bitmap-to-base64 * -string * * Serialize just the thumbnail as the image is serialized * separately */ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); bitmapThumb.compress(Bitmap.CompressFormat.JPEG, 90, byteArrayOutputStream); byte[] byteThumbArray = byteArrayOutputStream.toByteArray(); String encodedThumb = Base64.encodeToString(byteThumbArray, Base64.NO_WRAP); object.addProperty("imageThumbnail", encodedThumb); } object.addProperty("depth", comment.getDepth()); if (comment.getParent() != null) { object.addProperty("parent", comment.getParent().getId()); } return object; }
From source file:ca.ualberta.cmput301w14t08.geochan.json.LocationJsonConverter.java
License:Apache License
/** * Serializes data from a Location into JSON format. * // w ww .ja v a 2s . c o m * (Some of this code is taken from a stackOverflow user * Brian Roach, for details, see: http://stackoverflow.com/a/13997920) * * @param location * the Location to serialize * @param type * the Type * @param jsc * the JSON serialization context * * @return A JsonElement representing the serialized Location. * */ @Override public JsonElement serialize(Location location, Type type, JsonSerializationContext jsc) { JsonObject jo = new JsonObject(); jo.addProperty("mProvider", location.getProvider()); jo.addProperty("mAccuracy", location.getAccuracy()); jo.addProperty("latitude", location.getLatitude()); jo.addProperty("longitude", location.getLongitude()); return jo; }
From source file:ca.ualberta.cmput301w14t08.geochan.json.ThreadCommentJsonConverter.java
License:Apache License
/** * Serializes a ThreadComment object into JSON format. * /*from w w w . j a v a2 s . c o m*/ * @param thread the ThreadComment to serialize. * @param type the Type * @param context the JsonSerializationContext * * @return A JsonElement representing the serialized ThreadComment. */ @Override public JsonElement serialize(ThreadComment thread, Type type, JsonSerializationContext context) { JsonObject object = new JsonObject(); object.addProperty("title", thread.getTitle()); object.addProperty("threadDate", thread.getThreadDate().getTime()); object.addProperty("hasImage", thread.getBodyComment().hasImage()); object.addProperty("id", thread.getId()); if (thread.getBodyComment().getLocation() != null) { object.addProperty("location", thread.getBodyComment().getLocation().getLatitude() + "," + thread.getBodyComment().getLocation().getLongitude()); if (thread.getBodyComment().getLocation().getLocationDescription() != null) { object.addProperty("locationDescription", thread.getBodyComment().getLocation().getLocationDescription()); } } else { object.addProperty("location", "-999,-999"); } object.addProperty("user", thread.getBodyComment().getUser()); object.addProperty("hash", thread.getBodyComment().getHash()); object.addProperty("textPost", thread.getBodyComment().getTextPost()); if (thread.getBodyComment().hasImage()) { Bitmap bitmapThumb = thread.getBodyComment().getImageThumb(); /* * http://stackoverflow.com/questions/9224056/android-bitmap-to-base64 * -string */ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); bitmapThumb.compress(Bitmap.CompressFormat.JPEG, 90, byteArrayOutputStream); byte[] byteThumbArray = byteArrayOutputStream.toByteArray(); String encodedThumb = Base64.encodeToString(byteThumbArray, Base64.NO_WRAP); object.addProperty("imageThumbnail", encodedThumb); } return object; }
From source file:ca.ualberta.cmput301w14t08.geochan.json.ThreadCommentOfflineJsonConverter.java
License:Apache License
/** * Serializes a ThreadComment object into JSON format. * // w w w .j a v a 2 s. c om * @param thread The ThreadComment to serialize. * @param type The Type. * @param context The JsonSerializationContext * * @return A JsonElement representing the serialized ThreadComment. */ @Override public JsonElement serialize(ThreadComment thread, Type type, JsonSerializationContext context) { JsonObject object = new JsonObject(); object.addProperty("title", thread.getTitle()); object.addProperty("threadDate", thread.getThreadDate().getTime()); object.addProperty("hasImage", thread.getBodyComment().hasImage()); object.addProperty("id", thread.getId()); if (thread.getBodyComment().getLocation() != null) { object.addProperty("location", thread.getBodyComment().getLocation().getLatitude() + "," + thread.getBodyComment().getLocation().getLongitude()); if (thread.getBodyComment().getLocation().getLocationDescription() != null) { object.addProperty("locationDescription", thread.getBodyComment().getLocation().getLocationDescription()); } } else { object.addProperty("location", "-999,-999"); } object.addProperty("user", thread.getBodyComment().getUser()); object.addProperty("hash", thread.getBodyComment().getHash()); object.addProperty("textPost", thread.getBodyComment().getTextPost()); if (thread.getBodyComment().hasImage()) { Bitmap bitmapThumb = thread.getBodyComment().getImageThumb(); /* * http://stackoverflow.com/questions/9224056/android-bitmap-to-base64 * -string */ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); bitmapThumb.compress(Bitmap.CompressFormat.JPEG, 90, byteArrayOutputStream); byte[] byteThumbArray = byteArrayOutputStream.toByteArray(); String encodedThumb = Base64.encodeToString(byteThumbArray, Base64.NO_WRAP); object.addProperty("imageThumbnail", encodedThumb); } recursiveSerialize(object, thread.getBodyComment(), thread.getBodyComment().getChildren()); // Serialize all the images in the thread. return object; }
From source file:ca.ualberta.cmput301w14t08.geochan.json.ThreadCommentOfflineJsonConverter.java
License:Apache License
private void recursiveSerialize(JsonObject object, Comment parent, ArrayList<Comment> list) { object.addProperty(parent.getId(), GsonHelper.getOfflineGson().toJson(list)); for (Comment comment : list) { recursiveSerialize(object, comment, comment.getChildren()); }/*from w w w. j ava 2 s . c o m*/ }
From source file:ca.ualberta.CMPUT301W15T06.GsonAdapter.java
License:Apache License
@Override public JsonElement serialize(T src, Type type, JsonSerializationContext context) { // TODO Auto-generated method stub JsonObject retValue = new JsonObject(); String className = src.getClass().getCanonicalName(); retValue.addProperty(CLASSNAME, className); JsonElement elem = context.serialize(src); retValue.add(INSTANCE, elem);//w w w .j av a 2 s .co m return retValue; }
From source file:ca.ualberta.cs.scandaloutraveltracker.mappers.UserMapper.java
License:Apache License
@Override public JsonElement serialize(Location location, Type arg1, JsonSerializationContext arg2) { JsonObject jo = new JsonObject(); jo.addProperty("provider", location.getProvider()); jo.addProperty("accuracy", location.getAccuracy()); jo.addProperty("longitude", location.getLongitude()); jo.addProperty("latitude", location.getLatitude()); return jo;//from ww w . j a v a2 s. com }