Package com.google.gson

Examples of com.google.gson.JsonElement


                response.setEntity(new StringEntity(new Gson().toJson(object).toString(), HttpJobClient.CONTENT_TYPE));
            }
            if (request instanceof HttpEntityEnclosingRequest) {
                HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
                String content = EntityUtils.toString(entity, "UTF-8");
                JsonElement element = parse(content);
                if (element instanceof JsonObject) {
                    requestElement = (JsonObject) element;
                }
            }
        }
View Full Code Here


            new DefaultResourceSpecification(2, 1024, 2, 100, 100,
                    Arrays.asList("one1", "two2"), Arrays.asList("three3"));
    final String actualString = gson.toJson(expected);
    Assert.assertEquals(expectedString, actualString);

    final JsonElement expectedJson = gson.toJsonTree(expected);
    final ResourceSpecification actual = gson.fromJson(expectedJson, DefaultResourceSpecification.class);
    final JsonElement actualJson = gson.toJsonTree(actual);

    Assert.assertEquals(expectedJson, actualJson);
    ReflectionAssert.assertLenientEquals(expected, actual);
  }
View Full Code Here

        LOG.warn("Instance node was updated but data is null.");
        return;
      }
      try {
        Gson gson = new Gson();
        JsonElement json = gson.fromJson(new String(nodeData.getData(), Charsets.UTF_8), JsonElement.class);
        if (json.isJsonObject()) {
          JsonElement data = json.getAsJsonObject().get("data");
          if (data != null) {
            this.liveData = gson.fromJson(data, ContainerLiveNodeData.class);
            LOG.info("Container LiveNodeData updated: " + new String(nodeData.getData(), Charsets.UTF_8));
          }
        }
View Full Code Here

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

    Gson gson = new Gson();
    JsonElement json = gson.fromJson(new String(data, Charsets.UTF_8), JsonElement.class);
    if (!json.isJsonObject()) {
      LOG.warn("Unable to decode live data node.");
      return null;
    }

    JsonObject jsonObj = json.getAsJsonObject();
    json = jsonObj.get("data");
    if (!json.isJsonObject()) {
      LOG.warn("Property data not found in live data node.");
      return null;
    }

    try {
View Full Code Here

        assertThat(unwrapped).isEqualTo(node.getAsInt());
    }

    @Test
    public void longs_are_unwrapped() {
        JsonElement node =  using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.long-max-property");
        long val =  using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.long-max-property", Long.class);

        assertThat(val).isEqualTo(Long.MAX_VALUE);
        assertThat(val).isEqualTo(node.getAsLong());
    }
View Full Code Here

        assertThat(node.get("string-property").getAsString()).isEqualTo("string-value");
    }

    @Test
    public void strings_are_unwrapped() {
        JsonElement node =  using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.string-property");
        String unwrapped =  using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.string-property", String.class);

        assertThat(unwrapped).isEqualTo("string-value");
        assertThat(unwrapped).isEqualTo(node.getAsString());
    }
View Full Code Here

        assertThat(unwrapped).isEqualTo(node.getAsString());
    }

    @Test
    public void ints_are_unwrapped() {
        JsonElement node =  using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.int-max-property");
        int unwrapped =  using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.int-max-property", int.class);

        assertThat(unwrapped).isEqualTo(Integer.MAX_VALUE);
        assertThat(unwrapped).isEqualTo(node.getAsInt());
    }
View Full Code Here

        }
        if (!(o instanceof JsonElement)) {
            return o;
        }

        JsonElement e = (JsonElement) o;

        if (e.isJsonNull()) {
            return null;
        } else if (e.isJsonPrimitive()) {

            JsonPrimitive p = e.getAsJsonPrimitive();
            if (p.isString()) {
                return p.getAsString();
            } else if (p.isBoolean()) {
                return p.getAsBoolean();
            } else if (p.isNumber()) {
View Full Code Here

            return toJsonArray(obj).size();
        } else if (isMap(obj)) {
            return toJsonObject(obj).entrySet().size();
        } else {
            if (obj instanceof JsonElement) {
                JsonElement element = toJsonElement(obj);
                if (element.isJsonPrimitive()) {
                    return element.toString().length();
                }
            }
        }
        throw new JsonPathException("length operation can not applied to " + obj != null ? obj.getClass().getName() : "null");
    }
View Full Code Here

*/
public class JsonRpcResponseGsonAdaptorRobotTest extends TestCase {

  public void testDeserializeJsonRpcErrorResponse() throws Exception {
    String response = "{'id':'op1','error':{'message':'Not authorized!'}}";
    JsonElement jsonElement = new JsonParser().parse(response);

    JsonRpcResponseGsonAdaptor adaptor = new JsonRpcResponseGsonAdaptor();
    JsonRpcResponse result = adaptor.deserialize(jsonElement, null, null);
    assertTrue(result.isError());
    assertEquals("op1", result.getId());
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.