carolineartz
9/4/2016 - 7:09 PM

gistfile1.java

package com.crowdlab.deserializers;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

public class DeserializerHelper {

  private JsonObject mJsonObject;

  public DeserializerHelper(JsonObject obj) {

    mJsonObject = obj;
  }

  private JsonElement getElement(String key) {

    if (mJsonObject == null) { return null; }

    JsonElement element = mJsonObject.get(key);

    if (!element.isJsonNull()) {

      return element;
    }
  }

  public Long deserializeLong(String key) {

    Long value = -1L;
    JsonElement obj = getElement(key);

    if (obj != null) {
      
      value = obj.getAsLong();
    }

    return value;
  }

  public String deserializeString(String key) {

    String value = new String();

    JsonElement obj = getElement(key);

    if (obj != null) {
      
      value = obj.getAsString();
    }