Example usage for com.google.gson JsonElement getAsJsonObject

List of usage examples for com.google.gson JsonElement getAsJsonObject

Introduction

In this page you can find the example usage for com.google.gson JsonElement getAsJsonObject.

Prototype

public JsonObject getAsJsonObject() 

Source Link

Document

convenience method to get this element as a JsonObject .

Usage

From source file:com.collective.celos.ci.testing.fixtures.convert.JsonExpandConverter.java

License:Apache License

@Override
public FixFile convert(TestRun tr, FixFile ff) throws Exception {

    List<String> stringList = Lists.newArrayList();

    BufferedReader reader = new BufferedReader(new InputStreamReader(ff.getContent()));
    String line;//from w  w  w. j  ava  2 s  .  c o  m
    while ((line = reader.readLine()) != null) {
        JsonElement jsonElement = jsonParser.parse(line);
        for (String field : expandFields) {
            try {
                String strField = jsonElement.getAsJsonObject().get(field).getAsString();
                JsonElement fieldElem = jsonParser.parse(strField);
                jsonElement.getAsJsonObject().add(field, fieldElem);
            } catch (Exception e) {
                throw new Exception("Error caused in line: " + line + " trying to expand field: " + field, e);
            }
        }
        stringList.add(gson.toJson(jsonElement));
    }

    return new FixFile(IOUtils.toInputStream(StringUtils.join(stringList, "\n")));
}

From source file:com.contentful.java.cda.ResourceTypeAdapter.java

License:Apache License

/**
 * De-serialize a resource of type Content Type.
 *
 * @param jsonElement the resource in JSON form
 * @param context gson context/*from www . ja v a  2s  .com*/
 * @param sys map of system attributes
 * @return {@code CDAContentType} result object
 */
private CDAContentType deserializeContentType(JsonElement jsonElement, JsonDeserializationContext context,
        JsonObject sys) {
    // Display field
    JsonObject attrs = jsonElement.getAsJsonObject();
    String displayField = getFieldAsString(attrs, "displayField");

    // Name
    String name = getFieldAsString(attrs, "name");

    // Description
    String userDescription = getFieldAsString(attrs, "description");

    CDAContentType result = new CDAContentType(displayField, name, userDescription);
    setBaseFields(result, sys, jsonElement, context);
    return result;
}

From source file:com.contentful.java.cda.ResourceTypeAdapter.java

License:Apache License

/**
 * De-serialize a resource of type Space.
 *
 * @param jsonElement the resource in JSON form
 * @param context gson context/*from   w  ww  .  j  a va 2  s  . c om*/
 * @param sys map of system attributes
 * @return {@code CDASpace} result object
 */
private CDASpace deserializeSpace(JsonElement jsonElement, JsonDeserializationContext context, JsonObject sys) {
    // Name
    String name = jsonElement.getAsJsonObject().get("name").getAsString();

    // Locales
    JsonArray localesArray = jsonElement.getAsJsonObject().get("locales").getAsJsonArray();
    Type t = new TypeToken<ArrayList<CDALocale>>() {
    }.getType();
    ArrayList<CDALocale> locales = context.deserialize(localesArray, t);

    // Default locale
    String defaultLocale = Constants.DEFAULT_LOCALE;
    for (CDALocale l : locales) {
        if (l.isDefault()) {
            defaultLocale = l.getCode();
            break;
        }
    }

    CDASpace result = new CDASpace(defaultLocale, locales, name);
    setBaseFields(result, sys, jsonElement, context);
    return result;
}

From source file:com.contentful.java.cda.ResourceTypeAdapter.java

License:Apache License

/**
 * Sets the base fields for a resource./*from  w  w  w  .  ja  v a  2s.  co m*/
 * This will set the list fields based on the target's type, depending on whether
 * it is an instance of the {@code ResourceWithMap} class or the {@code ResourceWithList},
 * different results will be provided.
 *
 * This method will also set the map of system attributes for the resource.
 *
 * @param target Target {@link CDAResource} object to set fields for.
 * @param sys JsonObject representing the system attributes of the resource in JSON form.
 * @param jsonElement JsonElement representing the resource in JSON form.
 * @param context De-serialization context.
 * @throws JsonParseException on failure.
 */
@SuppressWarnings("unchecked")
private void setBaseFields(CDAResource target, JsonObject sys, JsonElement jsonElement,
        JsonDeserializationContext context) {
    CDASpace space = spaceWrapper.get();

    // System attributes
    Map<String, Object> sysMap = context.deserialize(sys, Map.class);
    if (sysMap.containsKey("space")) {
        sysMap.put("space", space);
    }
    target.setSys(sysMap);

    // Fields
    JsonElement fields = jsonElement.getAsJsonObject().get("fields");
    if (target instanceof ResourceWithMap) {
        ResourceWithMap res = (ResourceWithMap) target;
        target.setLocale(space.getDefaultLocale());
        res.setRawFields(context.<Map<String, Object>>deserialize(fields.getAsJsonObject(), Map.class));
        res.getLocalizedFieldsMap().put(space.getDefaultLocale(), res.getRawFields());
    } else if (target instanceof ResourceWithList) {
        ResourceWithList<Object> res = (ResourceWithList<Object>) target;
        res.setFields(context.<List<Object>>deserialize(fields.getAsJsonArray(), List.class));
    }
}

From source file:com.continusec.client.ContinusecClient.java

License:Apache License

/**
 * Fetch the list of logs held by this account.
 * @return list of logs/*w w w .ja  v a  2 s . co  m*/
 * @throws ContinusecException upon error
 */
public List<LogInfo> listLogs() throws ContinusecException {
    ResponseData rd = this.makeRequest("GET", "/logs", null, null);
    try {
        JsonObject o = new JsonParser().parse(new String(rd.data, "UTF-8")).getAsJsonObject();
        ArrayList<LogInfo> rv = new ArrayList<LogInfo>();
        for (JsonElement e : o.getAsJsonArray("results")) {
            rv.add(new LogInfo(e.getAsJsonObject().getAsJsonPrimitive("name").getAsString()));
        }
        return rv;
    } catch (UnsupportedEncodingException e) {
        throw new ContinusecException(e);
    }
}

From source file:com.continusec.client.ContinusecClient.java

License:Apache License

/**
 * Fetch the list of maps held by this account.
 * @return list of maps//from   ww w. j av  a2  s.  c om
 * @throws ContinusecException upon error
 */
public List<MapInfo> listMaps() throws ContinusecException {
    ResponseData rd = this.makeRequest("GET", "/maps", null, null);
    try {
        JsonObject o = new JsonParser().parse(new String(rd.data, "UTF-8")).getAsJsonObject();
        ArrayList<MapInfo> rv = new ArrayList<MapInfo>();
        for (JsonElement e : o.getAsJsonArray("results")) {
            rv.add(new MapInfo(e.getAsJsonObject().getAsJsonPrimitive("name").getAsString()));
        }
        return rv;
    } catch (UnsupportedEncodingException e) {
        throw new ContinusecException(e);
    }
}

From source file:com.continusec.client.ObjectHash.java

License:Apache License

/**
 * Calculate the objecthash for a Gson JsonElement object, with a custom redaction prefix string.
 * @param o the Gson JsonElement to calculated the objecthash for.
 * @param r the string to use as a prefix to indicate that a string should be treated as a redacted subobject.
 * @return the objecthash for this object
 * @throws ContinusecException upon error
 *//* w w w. ja v a  2s  . c o  m*/
public static final byte[] objectHashWithRedaction(JsonElement o, String r) throws ContinusecException {
    if (o == null || o.isJsonNull()) {
        return hashNull();
    } else if (o.isJsonArray()) {
        return hashArray(o.getAsJsonArray(), r);
    } else if (o.isJsonObject()) {
        return hashObject(o.getAsJsonObject(), r);
    } else if (o.isJsonPrimitive()) {
        JsonPrimitive p = o.getAsJsonPrimitive();
        if (p.isBoolean()) {
            return hashBoolean(p.getAsBoolean());
        } else if (p.isNumber()) {
            return hashDouble(p.getAsDouble());
        } else if (p.isString()) {
            return hashString(p.getAsString(), r);
        } else {
            throw new InvalidObjectException();
        }
    } else {
        throw new InvalidObjectException();
    }
}

From source file:com.continusec.client.ObjectHash.java

License:Apache License

/**
 * Strip away object values that are marked as redacted, and switch nonce-tuples back to normal values.
 * This is useful when an object has been stored with Redactable nonces added, but now it has been retrieved
 * and normal processing needs to be performed on it.
 * @param o the Gson JsonElement that contains the redacted elements and nonce-tuples.
 * @param r the redaction prefix that indicates if a string represents a redacted sub-object.
 * @return a new cleaned up JsonElement/*from  w ww.j a  va2  s  .  c  o  m*/
 * @throws ContinusecException upon error
 */
public static final JsonElement shedRedactable(JsonElement o, String r) throws ContinusecException {
    if (o == null) {
        return null;
    } else if (o.isJsonArray()) {
        return shedArray(o.getAsJsonArray(), r);
    } else if (o.isJsonObject()) {
        return shedObject(o.getAsJsonObject(), r);
    } else {
        return o;
    }
}

From source file:com.continuuity.loom.codec.json.current.AddServicesRequestCodec.java

License:Apache License

@Override
public AddServicesRequest deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObj = json.getAsJsonObject();
    Set<String> services = context.deserialize(jsonObj.get("services"), new TypeToken<Set<String>>() {
    }.getType());// www.  j a  v a2s  .c  o  m

    return new AddServicesRequest(services);
}

From source file:com.continuuity.loom.codec.json.current.AdministrationCodec.java

License:Apache License

@Override
public Administration deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObj = json.getAsJsonObject();

    LeaseDuration leaseDuration = context.deserialize(jsonObj.get("leaseduration"), LeaseDuration.class);

    return new Administration(leaseDuration);
}