Example usage for com.google.gson JsonObject get

List of usage examples for com.google.gson JsonObject get

Introduction

In this page you can find the example usage for com.google.gson JsonObject get.

Prototype

public JsonElement get(String memberName) 

Source Link

Document

Returns the member with the specified name.

Usage

From source file:ca.ualberta.cmput301w14t08.geochan.json.BitmapJsonConverter.java

License:Apache License

/**
 * Deserializes a Bitmap from a base64 JSON string.
 * //  www .  j a  v  a  2s. c o m
 * @param jsonElement
 *            the JSON element to deserialize
 * @param type
 *            the Type
 * @param jsc
 *            the JSON deserialization context
 * @return The deserialized Bitmap.
 * 
 * @throws JsonParseException
 */
@Override
public Bitmap deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsc)
        throws JsonParseException {
    Bitmap image = null;
    JsonObject object = jsonElement.getAsJsonObject();
    String encodedImage = object.get("image").getAsString();
    byte[] byteArray = Base64.decode(encodedImage, Base64.NO_WRAP);

    /*
     * http://stackoverflow.com/a/5878773
     * Sando's workaround for running out of memory on decoding bitmaps.
     */
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inDither = false;
    opts.inPurgeable = true;
    opts.inInputShareable = true;
    opts.inTempStorage = new byte[32 * 1024];
    image = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length, opts);

    return image;
}

From source file:ca.ualberta.cmput301w14t08.geochan.json.CommentJsonConverter.java

License:Apache License

/**
 * Deserializes a Comment object from JSON format.
 * @param json/* ww  w . j  av a 2 s .  co m*/
 *            the JSON element to deserialize
 * @param type
 *            the Type
 * @param context
 *            the JSON deserialization context
 *            
 * @return The deserialized Comment.
 * 
 * @throws JsonParseException
 */
@Override
public Comment deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject object = json.getAsJsonObject();

    long commentDate = object.get("commentDate").getAsLong();

    String locationString = object.get("location").getAsString();
    List<String> locationEntries = Arrays.asList(locationString.split(","));
    double latitude = Double.parseDouble(locationEntries.get(0));
    double longitude = Double.parseDouble(locationEntries.get(1));
    String locationDescription = null;
    if (object.get("locationDescription") != null) {
        locationDescription = object.get("locationDescription").getAsString();
    }
    GeoLocation location = new GeoLocation(latitude, longitude);
    location.setLocationDescription(locationDescription);

    String user = object.get("user").getAsString();
    String hash = object.get("hash").getAsString();
    String id = object.get("id").getAsString();

    String textPost = object.get("textPost").getAsString();

    Bitmap thumbnail = null;
    boolean hasImage = object.get("hasImage").getAsBoolean();
    if (hasImage) {
        /*
         * http://stackoverflow.com/questions/20594833/convert-byte-array-or-
         * bitmap-to-picture
         *
         * http://stackoverflow.com/a/5878773
         * Sando's workaround for running out of memory on decoding bitmaps.
         * 
         * Only deserialize the thumbnail as the full image is downloaded
         * and deserialized separately.
         */
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inDither = false;
        opts.inPurgeable = true;
        opts.inInputShareable = true;
        opts.inTempStorage = new byte[32 * 1024];
        String encodedThumb = object.get("imageThumbnail").getAsString();
        byte[] thumbArray = Base64.decode(encodedThumb, Base64.NO_WRAP);
        thumbnail = BitmapFactory.decodeByteArray(thumbArray, 0, thumbArray.length, opts);
    }

    int depth = object.get("depth").getAsInt();

    final Comment comment = new Comment(textPost, null, location, null);
    comment.getCommentDate().setTime(commentDate);
    comment.setUser(user);
    comment.setHash(hash);
    comment.setDepth(depth);
    comment.setId(Long.parseLong(id));
    if (hasImage) {
        comment.setImageThumb(thumbnail);
    }

    return comment;
}

From source file:ca.ualberta.cmput301w14t08.geochan.json.CommentOfflineJsonConverter.java

License:Apache License

/**
 * Deserializes a Comment object from JSON format (for offline storage).
 * @param json//  w  w w .j  a v a 2  s .  com
 *            the JSON element to deserialize
 * @param type
 *            the Type
 * @param context
 *            the JSON deserialization context
 *            
 * @return The deserialized Comment.
 * 
 * @throws JsonParseException
 */
@Override
public Comment deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject object = json.getAsJsonObject();

    long commentDate = object.get("commentDate").getAsLong();

    String locationString = object.get("location").getAsString();
    List<String> locationEntries = Arrays.asList(locationString.split(","));
    double latitude = Double.parseDouble(locationEntries.get(0));
    double longitude = Double.parseDouble(locationEntries.get(1));
    String locationDescription = null;
    if (object.get("locationDescription") != null) {
        locationDescription = object.get("locationDescription").getAsString();
    }
    GeoLocation location = new GeoLocation(latitude, longitude);
    location.setLocationDescription(locationDescription);

    String user = object.get("user").getAsString();
    String hash = object.get("hash").getAsString();
    String id = object.get("id").getAsString();

    String textPost = object.get("textPost").getAsString();

    Bitmap thumbnail = null;
    boolean hasImage = object.get("hasImage").getAsBoolean();
    if (hasImage) {
        /*
         * http://stackoverflow.com/a/5878773
         * Sando's workaround for running out of memory on decoding bitmaps.
         * 
         * Only deserialize the thumbnail as the full image is deserialized 
         * separately.
         */
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inDither = false;
        opts.inPurgeable = true;
        opts.inInputShareable = true;
        opts.inTempStorage = new byte[32 * 1024];
        String encodedThumb = object.get("imageThumbnail").getAsString();
        byte[] thumbArray = Base64.decode(encodedThumb, Base64.NO_WRAP);
        thumbnail = BitmapFactory.decodeByteArray(thumbArray, 0, thumbArray.length, opts);
    }

    int depth = object.get("depth").getAsInt();

    final Comment comment = new Comment(textPost, null, location, null);
    comment.getCommentDate().setTime(commentDate);
    comment.setUser(user);
    comment.setHash(hash);
    comment.setDepth(depth);
    comment.setId(Long.parseLong(id));
    if (hasImage) {
        comment.setImageThumb(thumbnail);
    }

    return comment;
}

From source file:ca.ualberta.cmput301w14t08.geochan.json.ThreadCommentJsonConverter.java

License:Apache License

/**
 * Deserializes a ThreadComment object from JSON format.
 * //from w w w. ja va 2  s  .  c  o  m
 * @param json the JsonElement
 * @param type the Type
 * @param context the JsonDeserializationContext
 * 
 * @return The deserialized ThreadComment.
 * 
 * @throws JsonParseException
 */
@Override
public ThreadComment deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject object = json.getAsJsonObject();
    String title = object.get("title").getAsString();
    long threadDate = object.get("threadDate").getAsLong();
    boolean hasImage = object.get("hasImage").getAsBoolean();
    String locationString = object.get("location").getAsString();
    List<String> locationEntries = Arrays.asList(locationString.split(","));
    double latitude = Double.parseDouble(locationEntries.get(0));
    double longitude = Double.parseDouble(locationEntries.get(1));
    String user = object.get("user").getAsString();
    String hash = object.get("hash").getAsString();
    String id = object.get("id").getAsString();
    String textPost = object.get("textPost").getAsString();
    String locationDescription = null;
    if (object.get("locationDescription") != null) {
        locationDescription = object.get("locationDescription").getAsString();
    }
    Bitmap thumbnail = null;
    if (hasImage) {
        /*
         * http://stackoverflow.com/questions/20594833/convert-byte-array-or-
         * bitmap-to-picture
         */
        // http://stackoverflow.com/a/5878773
        // Sando's workaround for running out of memory on decoding bitmaps.
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inDither = false; // Disable Dithering mode
        opts.inPurgeable = true; // Tell to gc that whether it needs free
                                 // memory, the Bitmap can be cleared
        opts.inInputShareable = true; // Which kind of reference will be
                                      // used to recover the Bitmap data
                                      // after being clear, when it will be
                                      // used in the future
        opts.inTempStorage = new byte[32 * 1024];

        String encodedThumb = object.get("imageThumbnail").getAsString();
        byte[] thumbArray = Base64.decode(encodedThumb, Base64.NO_WRAP);
        thumbnail = BitmapFactory.decodeByteArray(thumbArray, 0, thumbArray.length, opts);
    }
    GeoLocation location = new GeoLocation(latitude, longitude);
    location.setLocationDescription(locationDescription);
    final Comment c = new Comment(textPost, null, location, null);
    c.getCommentDate().setTime(threadDate);
    c.setUser(user);
    c.setHash(hash);
    c.setId(Long.parseLong(id));
    if (hasImage) {
        c.setImageThumb(thumbnail);
    }
    final ThreadComment comment = new ThreadComment(c, title);
    comment.setThreadDate(new Date(threadDate));
    comment.setId(Long.parseLong(id));
    return comment;
}

From source file:ca.ualberta.cmput301w14t08.geochan.json.ThreadCommentOfflineJsonConverter.java

License:Apache License

/**
 * Deserializes a ThreadComment object from JSON format.
 * //  w w w  .jav a 2 s .  c  o  m
 * @param json The JsonElement to deserialize.
 * @param type The Type.
 * @param context The JsonDeserializationContext.
 * 
 * @return The deserialized ThreadComment.
 */
@Override
public ThreadComment deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject object = json.getAsJsonObject();
    String title = object.get("title").getAsString();
    long threadDate = object.get("threadDate").getAsLong();
    boolean hasImage = object.get("hasImage").getAsBoolean();
    String locationString = object.get("location").getAsString();
    List<String> locationEntries = Arrays.asList(locationString.split(","));
    double latitude = Double.parseDouble(locationEntries.get(0));
    double longitude = Double.parseDouble(locationEntries.get(1));
    String user = object.get("user").getAsString();
    String hash = object.get("hash").getAsString();
    String id = object.get("id").getAsString();
    String textPost = object.get("textPost").getAsString();
    String locationDescription = null;
    if (object.get("locationDescription") != null) {
        locationDescription = object.get("locationDescription").getAsString();
    }
    ArrayList<Comment> topList = new ArrayList<Comment>();
    recursiveDeserialize(object, id, topList);
    Bitmap thumbnail = null;
    if (hasImage) {
        /*
         * http://stackoverflow.com/questions/20594833/convert-byte-array-or-
         * bitmap-to-picture
         */
        String encodedThumb = object.get("imageThumbnail").getAsString();
        byte[] thumbArray = Base64.decode(encodedThumb, Base64.NO_WRAP);
        thumbnail = BitmapFactory.decodeByteArray(thumbArray, 0, thumbArray.length);
    }
    GeoLocation location = new GeoLocation(latitude, longitude);
    location.setLocationDescription(locationDescription);
    final Comment c = new Comment(textPost, null, location, null);
    c.getCommentDate().setTime(threadDate);
    c.setUser(user);
    c.setHash(hash);
    c.setId(Long.parseLong(id));
    c.setChildren(topList);
    if (hasImage) {
        c.setImageThumb(thumbnail);
    }
    final ThreadComment comment = new ThreadComment(c, title);
    comment.setThreadDate(new Date(threadDate));
    comment.setId(Long.parseLong(id));
    return comment;
}

From source file:ca.ualberta.cmput301w14t08.geochan.json.ThreadCommentOfflineJsonConverter.java

License:Apache License

private void recursiveDeserialize(JsonObject object, String id, ArrayList<Comment> list) {
    JsonParser parser = new JsonParser();
    JsonArray array = parser.parse(object.get(id).getAsString()).getAsJsonArray();
    for (int i = 0; i < array.size(); ++i) {
        list.add(GsonHelper.getOfflineGson().fromJson(array.get(i), Comment.class));
    }//from  w  ww  .  ja v  a 2s .  co  m
    for (Comment comment : list) {
        ArrayList<Comment> childList = new ArrayList<Comment>();
        recursiveDeserialize(object, comment.getId(), childList);
        comment.setChildren(childList);
    }
}

From source file:ca.ualberta.CMPUT301W15T06.GsonAdapter.java

License:Apache License

@Override
public T deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    // TODO Auto-generated method stub
    JsonObject jsonObject = json.getAsJsonObject();
    JsonPrimitive prim = (JsonPrimitive) jsonObject.get(CLASSNAME);
    String className = prim.getAsString();

    Class<?> klass = null;//from  w  w  w  .jav  a 2 s  .  co m
    try {
        klass = Class.forName(className);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        throw new JsonParseException(e.getMessage());
    }
    return context.deserialize(jsonObject.get(INSTANCE), klass);
}

From source file:ca.uhn.fhir.jpa.util.jsonpatch.JsonPatchUtils.java

License:Apache License

public static <T extends IBaseResource> T apply(FhirContext theCtx, T theResourceToUpdate,
        String thePatchBody) {//from  w  w w.  ja v  a  2  s.c o  m
    JsonPatch parsedPatch = new JsonPatch();

    // Parse the patch
    Gson gson = JsonParser.newGson();
    JsonElement jsonElement = gson.fromJson(thePatchBody, JsonElement.class);
    JsonArray array = jsonElement.getAsJsonArray();
    for (JsonElement nextElement : array) {
        JsonObject nextElementAsObject = (JsonObject) nextElement;

        String opName = nextElementAsObject.get("op").getAsString();
        if ("add".equals(opName)) {
            AddOperation op = new AddOperation(toPath(nextElementAsObject), nextElementAsObject.get("value"));
            parsedPatch.add(op);
        } else if ("remove".equals(opName)) {
            RemoveOperation op = new RemoveOperation(toPath(nextElementAsObject));
            parsedPatch.add(op);
        } else if ("replace".equals(opName)) {
            ReplaceOperation op = new ReplaceOperation(toPath(nextElementAsObject),
                    nextElementAsObject.get("value"));
            parsedPatch.add(op);
        } else if ("copy".equals(opName)) {
            CopyOperation op = new CopyOperation(toPath(nextElementAsObject), toFromPath(nextElementAsObject));
            parsedPatch.add(op);
        } else if ("move".equals(opName)) {
            MoveOperation op = new MoveOperation(toPath(nextElementAsObject), toFromPath(nextElementAsObject));
            parsedPatch.add(op);
        } else {
            throw new InvalidRequestException("Invalid JSON PATCH operation: " + opName);
        }

    }

    @SuppressWarnings("unchecked")
    Class<T> clazz = (Class<T>) theResourceToUpdate.getClass();

    JsonElement originalJsonDocument = gson
            .fromJson(theCtx.newJsonParser().encodeResourceToString(theResourceToUpdate), JsonElement.class);
    JsonElement target = parsedPatch.apply(originalJsonDocument);
    T retVal = theCtx.newJsonParser().parseResource(clazz, gson.toJson(target));

    return retVal;
}

From source file:ca.uhn.fhir.jpa.util.jsonpatch.JsonPatchUtils.java

License:Apache License

private static JsonPath toFromPath(JsonObject nextElementAsObject) {
    return new JsonPath(nextElementAsObject.get("from").getAsString());
}

From source file:ca.uhn.fhir.jpa.util.jsonpatch.JsonPatchUtils.java

License:Apache License

private static JsonPath toPath(JsonObject nextElementAsObject) {
    return new JsonPath(nextElementAsObject.get("path").getAsString());
}