Package com.google.gson

Examples of com.google.gson.JsonElement


public final class JsonToResource {
  private JsonToResource() {
  }

  public static Resource parse(InputStream inputStream) {
    JsonElement element = new JsonParser().parse(new InputStreamReader(inputStream));
    return parseResource(element.getAsJsonObject(), "/", null);
  }
View Full Code Here


  private static Resource parseResource(JsonObject object, String name, Resource parent) {
    Map<String, String> properties = new LinkedHashMap<String, String>();
    Map<String, JsonObject> children = new LinkedHashMap<String, JsonObject>();
    for (Entry<String, JsonElement> entry : object.entrySet()) {
      JsonElement value = entry.getValue();
      if (value.isJsonPrimitive()) {
        properties.put(entry.getKey(), value.getAsString());
      } else if (value.isJsonObject()) {
        children.put(entry.getKey(), value.getAsJsonObject());
      }
    }

    ResourceMock resource = new ResourceMock(parent, name);
    for (Entry<String, String> entry : properties.entrySet()) {
View Full Code Here

     * @param gson the gson instance to use
     * @param json the json response content
     * @return the InstagramErrorResponse object
     */
    public static InstagramErrorResponse parse(Gson gson, String json) {
        JsonElement jsonElement = gson.fromJson(json, JsonElement.class);
        JsonElement metaMember = jsonElement.getAsJsonObject().get("meta");
        final Meta meta;
        if (metaMember != null) {
            meta = gson.fromJson(metaMember, Meta.class);
        } else {
            meta = gson.fromJson(jsonElement, Meta.class);
View Full Code Here

   *            the json content
   * @return the json element
   */
  private JsonElement unmarshallToJson(InputStream jsonContent) {
    try {
      JsonElement element = parser.parse(new InputStreamReader(
          jsonContent,
          UTF_8_CHAR_SET));
      if (element.isJsonObject()) {
        return element.getAsJsonObject();
      } else if (element.isJsonArray()) {
        return element.getAsJsonArray();
      } else {
        throw new IllegalStateException(
            "Unknown content found in response." + element);
      }
    } catch (Exception e) {
View Full Code Here

    // Get metadata for the Module
    List<JsonElement> requestElements = new ArrayList<JsonElement>();
    JsonObject moduleJsonObject = gson.fromJson(output, JsonObject.class);

    JsonElement moduleNameElement = moduleJsonObject.get("moduleName");
    JsonElement moduleDescriptionElement = moduleJsonObject.get("moduleDescription");
    JsonElement moduleVersionElement = moduleJsonObject.get("moduleDescription");

    String moduleName = "default_module_name";
    String moduleDescription = "default_module_description";
    String moduleVersion = "1.0";

    if (moduleNameElement != null && !moduleNameElement.getAsString().isEmpty()) {
      moduleName = moduleNameElement.getAsString();
    }

    if (moduleDescriptionElement != null && !moduleDescriptionElement.getAsString().isEmpty()) {
      moduleDescription = moduleDescriptionElement.getAsString();
    }
   
    if (moduleVersionElement != null && !moduleDescriptionElement.getAsString().isEmpty()) {
      moduleDescription = moduleVersionElement.getAsString();
    }

    // Get all 'functions' as JsonObjects from the file and add them as a
    // list
    JsonElement functions = JsonUtils.findElement(moduleJsonObject, "functions");
    if (functions != null) {
      if (functions.isJsonArray()) {
        JsonArray requests_array = functions.getAsJsonArray();
        for (JsonElement requestElement : requests_array) {
          if (requestElement.isJsonObject()) {
            JsonObject request = requestElement.getAsJsonObject();
            requestElements.add(request);
          }
View Full Code Here

    stack.add(element);
    return search(stack, elementName);
  }

  private static JsonElement search(Queue<JsonElement> queue, String elementName) {
    JsonElement ret = null;
    while (queue.size() > 0) {
      JsonElement element = queue.poll();
      if (element.isJsonObject()) {
        JsonObject object = element.getAsJsonObject();
        Set<Entry<String, JsonElement>> members = object.entrySet();
        for (Entry<String, JsonElement> member : members) {
          if (member.getKey().equals(elementName)) {
            return member.getValue();
          } else {
            queue.add(member.getValue());
          }
        }

      } else if (element.isJsonArray()) {
        JsonArray array = element.getAsJsonArray();
        for (JsonElement array_element : array) {
          queue.add(array_element);
        }
      }
    }
View Full Code Here

    this.setPostData(JsonUtils.findElement(jsonRequest, "postData"));
    this.setResponseFields(JsonUtils.findElement(jsonRequest, "response"));
  }
 
  private void setResponseFields(JsonElement jsonResponse) {
    JsonElement responseFields = null;
    if(jsonResponse != null) responseFields = JsonUtils.findElement(jsonResponse, "fields");
   
    if (responseFields != null) {
      for (JsonElement responseField : JsonUtils.asJsonArray(responseFields)) {
        JsonObject response = JsonUtils.asJsonObject(responseField);
View Full Code Here

      }
    }
  }

  private void setFunctionName(JsonObject jsonRequest) {
    JsonElement functionName = jsonRequest.get("functionName");
    if (functionName != null) {
      this.functionName = functionName.getAsString();
    } else {
      System.out.println("ERROR: Request object did not contain function name");
    }
  }
View Full Code Here

        pivotSpec.addFilter("has_boris", BooleanComparison.EQUALS, true);

        Assert.assertEquals(1, pivotSpec.getFilters().size());
        for (PivotFilter pf : pivotSpec.getFilters()) {
            Assert.assertTrue(pf instanceof BooleanPivotFilter);
            JsonElement obj = pf.toJson();
            Assert.assertTrue(obj instanceof JsonObject);
            JsonObject o = (JsonObject)obj;

            Assert.assertTrue(o.has("fieldName"));
            Assert.assertEquals(new JsonPrimitive("has_boris"), o.get("fieldName"));
View Full Code Here

        pivotSpec.addFilter("host", StringComparison.CONTAINS, "abc");

        Assert.assertEquals(1, pivotSpec.getFilters().size());
        for (PivotFilter pf : pivotSpec.getFilters()) {
            Assert.assertTrue(pf instanceof StringPivotFilter);
            JsonElement obj = pf.toJson();
            Assert.assertTrue(obj instanceof JsonObject);
            JsonObject o = (JsonObject)obj;

            Assert.assertTrue(o.has("fieldName"));
            Assert.assertEquals(new JsonPrimitive("host"), o.get("fieldName"));
View Full Code Here

TOP

Related Classes of com.google.gson.JsonElement

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.