Package com.google.gson

Examples of com.google.gson.JsonElement


    assertEquals("Not authorized!", result.getErrorMessage());
  }

  public void testDeserializeJsonRpcResponse() throws Exception {
    String response = "{'id':'op1','data':{'newBlipId':'blip1','unknown':'value'}}";
    JsonElement jsonElement = new JsonParser().parse(response);

    JsonDeserializationContext mockContext = mock(JsonDeserializationContext.class);
    when(mockContext.deserialize(any(JsonElement.class), eq(String.class))).thenAnswer(
        new Answer<String>() {
          public String answer(InvocationOnMock invocation) {
View Full Code Here


public class OperationRequestGsonAdaptorRobotTest extends TestCase {

  public void testDeserialize() throws Exception {
    String operation = "{'id':'op1','method':'wavelet.setTitle','params':{" +
        "'waveId':'1','waveletId':'2','waveletTitle':'Title','unknown':'value'}}";
    JsonElement jsonElement = new JsonParser().parse(operation);

    JsonDeserializationContext mockContext = mock(JsonDeserializationContext.class);
    when(mockContext.deserialize(any(JsonElement.class), eq(String.class))).thenAnswer(
        new Answer<String>() {
          public String answer(InvocationOnMock invocation) {
View Full Code Here

    return writer.toString();
  }

  private <T extends Message> T fetchWaverRefAndParse(WaveRef waveref, Class<T> klass) throws Exception {
    String message = fetchWaveRef(waveref);
    JsonElement json = new JsonParser().parse(message);
    return protoSerializer.fromJson(json, klass);
  }
View Full Code Here

   * @throws GsonException
   */
  public static <T extends GsonSerializable> void parseJson(T object,
      Gson gson, String json, RawStringData data) throws GsonException {
    try {
      JsonElement root = new JsonParser().parse(json);
      object.fromGson(root, gson, data);
    } catch (JsonParseException e) {
      throw new GsonException("Unable to parse Json", e);
    }
  }
View Full Code Here

      JsonDeserializationContext context) throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();

    String id = jsonObject.get(ResponseProperty.ID.key()).getAsString();
    if (jsonObject.has(ResponseProperty.ERROR.key())) {
      JsonElement errorObject = jsonObject.get(ResponseProperty.ERROR.key());
      String errorMessage = errorObject.getAsJsonObject().get("message").getAsString();
      return JsonRpcResponse.error(id, errorMessage);
    }

    // Deserialize the data.
    Map<ParamsProperty, Object> properties = new HashMap<ParamsProperty, Object>();
    JsonElement data = jsonObject.get(ResponseProperty.DATA.key());
    if (data != null && data.isJsonObject()) {
      for (Entry<String, JsonElement> parameter : data.getAsJsonObject().entrySet()) {
        ParamsProperty parameterType = ParamsProperty.fromKey(parameter.getKey());
        if (parameterType == null) {
          // Skip this unknown parameter.
          continue;
        }
View Full Code Here

      // JsonPrimitive.toString() wraps the text inside double quotes, that we
      // need to strip out.
      String quotedKey = context.serialize(entry.getKey()).toString();
      String key = quotedKey.substring(1, quotedKey.length() - 1);

      JsonElement value = context.serialize(entry.getValue());
      properties.add(key, value);
    }

    jsonObject.add(PROPERTIES_TAG, properties);
    return jsonObject;
View Full Code Here

   * @param jsonObject the {@code JsonObject} to get the property from.
   * @param key the key of the property.
   * @return the property as {@link String}, or {@code null} if not found.
   */
  private static String getPropertyAsStringThenRemove(JsonObject jsonObject, ParamsProperty key) {
    JsonElement property = jsonObject.get(key.key());
    if (property != null) {
      jsonObject.remove(key.key());
      if (property.isJsonNull()) {
        return null;
      }
      return property.getAsString();
    }
    return null;
  }
View Full Code Here

      JsonObject properties = json.get(PROPERTIES).getAsJsonObject();

      // Set the blip id field, that can be null for certain events, such as
      // OPERATION_ERROR.
      JsonElement blipId = properties.get(BLIP_ID);
      if (blipId != null && !(blipId instanceof JsonNull)) {
        setField(event, rootClass.getDeclaredField(BLIP_ID), blipId.getAsString());
      }

      // Set the additional fields.
      for (Field field : clazz.getDeclaredFields()) {
        String fieldName = field.getName();
View Full Code Here

   */
  protected abstract void sendMessageString(String data) throws IOException;

  @Override
  public void sendMessage(int sequenceNo, Message message) {
    JsonElement json;
    String str;

    Timer timer = Timing.start("serializeMessage");
    try {
      json = serializer.toJson(message);
View Full Code Here

      this.messageType = messageType;
      this.message = message;
    }

    public static MessageWrapper deserialize(Gson gson, String data) {
      JsonElement e = parser.parse(data);
      JsonObject obj = e.getAsJsonObject();
      String type = obj.get("messageType").getAsString();
      int seqno = obj.get("sequenceNumber").getAsInt();
      JsonElement message = obj.get("message");
      return new MessageWrapper(seqno, type, message);
    }
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.