Search in sources :

Example 1 with JsonElement

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

the class CouchDbConsumerIntegrationTest method testInsertsOnly.

@Test
public void testInsertsOnly() throws InterruptedException {
    to.expectedHeaderReceived(CouchDbConstants.HEADER_METHOD, "UPDATE");
    to.expectedMessageCount(1);
    JsonElement obj = new Gson().toJsonTree("{ \"randomString\" : \"" + UUID.randomUUID() + "\" }");
    Response resp = client.save(obj);
    client.remove(resp.getId(), resp.getRev());
    to.assertIsSatisfied();
}
Also used : Response(org.lightcouch.Response) JsonElement(com.google.gson.JsonElement) Gson(com.google.gson.Gson) Test(org.junit.Test)

Example 2 with JsonElement

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

the class CouchDbConsumerIntegrationTest method testMessages.

@Test
public void testMessages() throws InterruptedException {
    final int messageCount = 100;
    to.expectedMessageCount(messageCount);
    // insert json manually into couch, camel will
    // then pick that up and send to mock endpoint
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            for (int k = 0; k < messageCount; k++) {
                JsonElement obj = new Gson().toJsonTree("{ \"randomString\" : \"" + UUID.randomUUID() + "\" }");
                client.save(obj);
            }
        }
    });
    t.start();
    t.join();
    to.assertIsSatisfied();
}
Also used : JsonElement(com.google.gson.JsonElement) Gson(com.google.gson.Gson) Endpoint(org.apache.camel.Endpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 3 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 4 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 5 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)398 JsonObject (com.google.gson.JsonObject)221 JsonArray (com.google.gson.JsonArray)121 JsonParser (com.google.gson.JsonParser)88 Map (java.util.Map)55 HashMap (java.util.HashMap)49 JsonPrimitive (com.google.gson.JsonPrimitive)48 Gson (com.google.gson.Gson)47 ArrayList (java.util.ArrayList)45 Test (org.testng.annotations.Test)45 IOException (java.io.IOException)40 JsonReader (com.google.gson.stream.JsonReader)30 StringReader (java.io.StringReader)21 Test (org.junit.Test)19 GsonBuilder (com.google.gson.GsonBuilder)18 InputStreamReader (java.io.InputStreamReader)18 Type (java.lang.reflect.Type)17 JsonParseException (com.google.gson.JsonParseException)15 AssetManager (android.content.res.AssetManager)10 List (java.util.List)10