Search in sources :

Example 36 with JsonElement

use of com.google.gson.JsonElement in project camel by apache.

the class CouchDbProducerTest method testStringBodyIsConvertedToJsonTree.

@Test
public void testStringBodyIsConvertedToJsonTree() throws Exception {
    when(msg.getMandatoryBody()).thenReturn("{ \"name\" : \"coldplay\" }");
    when(client.save(anyObject())).thenAnswer(new Answer<Response>() {

        @Override
        public Response answer(InvocationOnMock invocation) throws Throwable {
            assertTrue(invocation.getArguments()[0].getClass() + " but wanted " + JsonElement.class, invocation.getArguments()[0] instanceof JsonElement);
            return new Response();
        }
    });
    producer.process(exchange);
    verify(client).save(any(JsonObject.class));
}
Also used : Response(org.lightcouch.Response) InvocationOnMock(org.mockito.invocation.InvocationOnMock) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) Test(org.junit.Test)

Example 37 with JsonElement

use of com.google.gson.JsonElement in project camel by apache.

the class CouchDbProducer method process.

@Override
public void process(Exchange exchange) throws Exception {
    JsonElement json = getBodyAsJsonElement(exchange);
    String operation = exchange.getIn().getHeader(CouchDbConstants.HEADER_METHOD, String.class);
    if (ObjectHelper.isEmpty(operation)) {
        Response save = saveJsonElement(json);
        if (save == null) {
            throw new CouchDbException("Could not save document [unknown reason]", exchange);
        }
        if (log.isTraceEnabled()) {
            log.trace("Document saved [_id={}, _rev={}]", save.getId(), save.getRev());
        }
        exchange.getIn().setHeader(CouchDbConstants.HEADER_DOC_REV, save.getRev());
        exchange.getIn().setHeader(CouchDbConstants.HEADER_DOC_ID, save.getId());
    } else {
        if (operation.equalsIgnoreCase("DELETE")) {
            Response delete = deleteJsonElement(json);
            if (delete == null) {
                throw new CouchDbException("Could not delete document [unknown reason]", exchange);
            }
            if (log.isTraceEnabled()) {
                log.trace("Document saved [_id={}, _rev={}]", delete.getId(), delete.getRev());
            }
            exchange.getIn().setHeader(CouchDbConstants.HEADER_DOC_REV, delete.getRev());
            exchange.getIn().setHeader(CouchDbConstants.HEADER_DOC_ID, delete.getId());
        }
    }
}
Also used : Response(org.lightcouch.Response) JsonElement(com.google.gson.JsonElement)

Example 38 with JsonElement

use of com.google.gson.JsonElement in project che by eclipse.

the class StackMessageBodyAdapter method adapt.

@Override
protected String adapt(String body) {
    if (!isOldFormat(body)) {
        return body;
    }
    final JsonElement stackEl = new JsonParser().parse(body);
    if (!stackEl.isJsonObject()) {
        return body;
    }
    stackJsonAdapter.adaptModifying(stackEl.getAsJsonObject());
    return stackEl.toString();
}
Also used : JsonElement(com.google.gson.JsonElement) JsonParser(com.google.gson.JsonParser)

Example 39 with JsonElement

use of com.google.gson.JsonElement in project che by eclipse.

the class ServerDtoTest method testCloneWithNullAny.

@Test
public void testCloneWithNullAny() throws Exception {
    DtoWithAny dto1 = dtoFactory.createDto(DtoWithAny.class);
    DtoWithAny dto2 = dtoFactory.clone(dto1);
    Assert.assertEquals(dto1, dto2);
    JsonElement json = new JsonParser().parse(dtoFactory.toJson(dto1));
    JsonObject expJson = new JsonObject();
    expJson.addProperty("id", 0);
    expJson.add("objects", new JsonArray());
    assertEquals(expJson, json);
}
Also used : JsonArray(com.google.gson.JsonArray) DtoWithAny(org.eclipse.che.dto.definitions.DtoWithAny) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser) Test(org.testng.annotations.Test)

Example 40 with JsonElement

use of com.google.gson.JsonElement in project weixin-java-tools by chanjarster.

the class WxCpServiceImpl method tagGet.

@Override
public List<WxCpTag> tagGet() throws WxErrorException {
    String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/list";
    String responseContent = get(url, null);
    JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
    return WxCpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement.getAsJsonObject().get("taglist"), new TypeToken<List<WxCpTag>>() {
    }.getType());
}
Also used : WxCpTag(me.chanjar.weixin.cp.bean.WxCpTag) JsonElement(com.google.gson.JsonElement) TypeToken(com.google.gson.reflect.TypeToken) StringReader(java.io.StringReader) JsonReader(com.google.gson.stream.JsonReader)

Aggregations

JsonElement (com.google.gson.JsonElement)682 JsonObject (com.google.gson.JsonObject)381 JsonArray (com.google.gson.JsonArray)166 JsonPrimitive (com.google.gson.JsonPrimitive)107 JsonParser (com.google.gson.JsonParser)105 Gson (com.google.gson.Gson)69 Map (java.util.Map)63 ArrayList (java.util.ArrayList)51 HashMap (java.util.HashMap)50 IOException (java.io.IOException)48 Test (org.testng.annotations.Test)45 JsonReader (com.google.gson.stream.JsonReader)31 InputStreamReader (java.io.InputStreamReader)28 GsonBuilder (com.google.gson.GsonBuilder)26 Pager (com.willshex.blogwt.shared.api.Pager)26 Type (java.lang.reflect.Type)25 StringReader (java.io.StringReader)21 Test (org.junit.Test)21 User (com.willshex.blogwt.shared.api.datatype.User)19 JsonParseException (com.google.gson.JsonParseException)17