Search in sources :

Example 31 with JsonReader

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

the class WxMpServiceImpl method getUserSummary.

@Override
public List<WxMpUserSummary> getUserSummary(Date beginDate, Date endDate) throws WxErrorException {
    String url = "https://api.weixin.qq.com/datacube/getusersummary";
    JsonObject param = new JsonObject();
    param.addProperty("begin_date", SIMPLE_DATE_FORMAT.format(beginDate));
    param.addProperty("end_date", SIMPLE_DATE_FORMAT.format(endDate));
    String responseContent = post(url, param.toString());
    JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
    return WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement.getAsJsonObject().get("list"), new TypeToken<List<WxMpUserSummary>>() {
    }.getType());
}
Also used : JsonElement(com.google.gson.JsonElement) TypeToken(com.google.gson.reflect.TypeToken) StringReader(java.io.StringReader) JsonObject(com.google.gson.JsonObject) JsonReader(com.google.gson.stream.JsonReader) WxMpUserSummary(me.chanjar.weixin.mp.bean.result.WxMpUserSummary)

Example 32 with JsonReader

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

the class WxMpServiceImpl method getUserCumulate.

@Override
public List<WxMpUserCumulate> getUserCumulate(Date beginDate, Date endDate) throws WxErrorException {
    String url = "https://api.weixin.qq.com/datacube/getusercumulate";
    JsonObject param = new JsonObject();
    param.addProperty("begin_date", SIMPLE_DATE_FORMAT.format(beginDate));
    param.addProperty("end_date", SIMPLE_DATE_FORMAT.format(endDate));
    String responseContent = post(url, param.toString());
    JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
    return WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement.getAsJsonObject().get("list"), new TypeToken<List<WxMpUserCumulate>>() {
    }.getType());
}
Also used : WxMpUserCumulate(me.chanjar.weixin.mp.bean.result.WxMpUserCumulate) JsonElement(com.google.gson.JsonElement) TypeToken(com.google.gson.reflect.TypeToken) StringReader(java.io.StringReader) JsonObject(com.google.gson.JsonObject) JsonReader(com.google.gson.stream.JsonReader)

Example 33 with JsonReader

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

the class WxMpServiceImpl method shortUrl.

public String shortUrl(String long_url) throws WxErrorException {
    String url = "https://api.weixin.qq.com/cgi-bin/shorturl";
    JsonObject o = new JsonObject();
    o.addProperty("action", "long2short");
    o.addProperty("long_url", long_url);
    String responseContent = execute(new SimplePostRequestExecutor(), url, o.toString());
    JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
    return tmpJsonElement.getAsJsonObject().get("short_url").getAsString();
}
Also used : JsonElement(com.google.gson.JsonElement) StringReader(java.io.StringReader) JsonObject(com.google.gson.JsonObject) JsonReader(com.google.gson.stream.JsonReader) SimplePostRequestExecutor(me.chanjar.weixin.common.util.http.SimplePostRequestExecutor)

Example 34 with JsonReader

use of com.google.gson.stream.JsonReader in project zipkin by openzipkin.

the class JsonCodec method readTraces.

public List<List<Span>> readTraces(byte[] bytes) {
    JsonReader reader = jsonReader(bytes);
    // cause we don't know how long it will be
    List<List<Span>> result = new LinkedList<>();
    try {
        reader.beginArray();
        while (reader.hasNext()) {
            reader.beginArray();
            // cause we don't know how long it will be
            List<Span> trace = new LinkedList<>();
            while (reader.hasNext()) {
                trace.add(SPAN_ADAPTER.fromJson(reader));
            }
            reader.endArray();
            result.add(trace);
        }
        reader.endArray();
        return result;
    } catch (Exception e) {
        throw exceptionReading("List<List<Span>>", bytes, e);
    }
}
Also used : JsonReader(com.google.gson.stream.JsonReader) LinkedList(java.util.LinkedList) List(java.util.List) Span(zipkin.Span) LinkedList(java.util.LinkedList) IOException(java.io.IOException) MalformedJsonException(com.google.gson.stream.MalformedJsonException)

Example 35 with JsonReader

use of com.google.gson.stream.JsonReader in project zipkin by openzipkin.

the class JsonCodec method readList.

static <T> List<T> readList(JsonAdapter<T> adapter, byte[] bytes) {
    JsonReader reader = jsonReader(bytes);
    List<T> result;
    try {
        reader.beginArray();
        if (reader.hasNext()) {
            // cause we don't know how long it will be
            result = new LinkedList<>();
        } else {
            result = Collections.emptyList();
        }
        while (reader.hasNext()) {
            result.add(adapter.fromJson(reader));
        }
        reader.endArray();
        return result;
    } catch (Exception e) {
        throw exceptionReading("List<" + adapter + ">", bytes, e);
    }
}
Also used : JsonReader(com.google.gson.stream.JsonReader) IOException(java.io.IOException) MalformedJsonException(com.google.gson.stream.MalformedJsonException)

Aggregations

JsonReader (com.google.gson.stream.JsonReader)100 StringReader (java.io.StringReader)38 JsonElement (com.google.gson.JsonElement)31 IOException (java.io.IOException)20 JsonObject (com.google.gson.JsonObject)19 Test (org.junit.Test)19 InputStreamReader (java.io.InputStreamReader)18 JsonParser (com.google.gson.JsonParser)12 HumanReadableException (com.facebook.buck.util.HumanReadableException)10 Gson (com.google.gson.Gson)10 JsonWriter (com.google.gson.stream.JsonWriter)10 TypeToken (com.google.gson.reflect.TypeToken)8 Map (java.util.Map)8 JsonToken (com.google.gson.stream.JsonToken)6 HashMap (java.util.HashMap)6 InputStream (java.io.InputStream)5 StringWriter (java.io.StringWriter)5 Type (java.lang.reflect.Type)5 ArrayList (java.util.ArrayList)5 GsonBuilder (com.google.gson.GsonBuilder)4