Search in sources :

Example 26 with JsonReader

use of com.google.gson.stream.JsonReader in project immutables by immutables.

the class JaxrsTest method propagateGsonAttributes.

@Test
public void propagateGsonAttributes() {
    Gson gson = new GsonBuilder().serializeNulls().disableHtmlEscaping().setPrettyPrinting().create();
    GsonOptions options = new GsonOptions(gson, true);
    JsonReader reader = new JsonReader(new StringReader(""));
    options.setReaderOptions(reader);
    check(reader.isLenient());
    JsonWriter writer = new JsonWriter(new StringWriter());
    options.setWriterOptions(writer);
    check(writer.isLenient());
    check(!writer.isHtmlSafe());
    check(writer.getSerializeNulls());
    // checks pretty printing
    check(gson.toJson(Collections.singletonMap("k", "v"))).is("{\n  \"k\": \"v\"\n}");
}
Also used : GsonOptions(org.immutables.gson.stream.GsonMessageBodyProvider.GsonOptions) StringWriter(java.io.StringWriter) GsonBuilder(com.google.gson.GsonBuilder) StringReader(java.io.StringReader) Gson(com.google.gson.Gson) JsonReader(com.google.gson.stream.JsonReader) JsonWriter(com.google.gson.stream.JsonWriter) Test(org.junit.Test)

Example 27 with JsonReader

use of com.google.gson.stream.JsonReader in project okhttp-OkGo by jeasonlzy.

the class NewsCallback method convertSuccess.

/**
     * 这里的数据解析是根据 http://apistore.baidu.com/apiworks/servicedetail/688.html 返回的数据来写的
     * 实际使用中,自己服务器返回的数据格式和上面网站肯定不一样,所以以下是参考代码,根据实际情况自己改写
     */
@Override
public T convertSuccess(Response response) throws Exception {
    //以下代码是通过泛型解析实际参数,泛型必须传
    Type genType = getClass().getGenericSuperclass();
    Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
    Type type = params[0];
    if (!(type instanceof ParameterizedType))
        throw new IllegalStateException("没有填写泛型参数");
    JsonReader jsonReader = new JsonReader(response.body().charStream());
    Type rawType = ((ParameterizedType) type).getRawType();
    if (rawType == NewsResponse.class) {
        NewsResponse newsResponse = Convert.fromJson(jsonReader, type);
        if (newsResponse.showapi_res_code == 0) {
            response.close();
            //noinspection unchecked
            return (T) newsResponse;
        } else {
            response.close();
            throw new IllegalStateException(newsResponse.showapi_res_error);
        }
    } else {
        response.close();
        throw new IllegalStateException("基类错误无法解析!");
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JsonReader(com.google.gson.stream.JsonReader) NewsResponse(com.lzy.demo.model.NewsResponse)

Example 28 with JsonReader

use of com.google.gson.stream.JsonReader in project okhttp-OkGo by jeasonlzy.

the class NewsConvert method convertSuccess.

@Override
public T convertSuccess(Response response) throws Exception {
    //以下代码是通过泛型解析实际参数,泛型必须传
    Type genType = getClass().getGenericSuperclass();
    Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
    Type type = params[0];
    if (!(type instanceof ParameterizedType))
        throw new IllegalStateException("没有填写泛型参数");
    JsonReader jsonReader = new JsonReader(response.body().charStream());
    Type rawType = ((ParameterizedType) type).getRawType();
    if (rawType == NewsResponse.class) {
        NewsResponse newsResponse = Convert.fromJson(jsonReader, type);
        if (newsResponse.showapi_res_code == 0) {
            response.close();
            //noinspection unchecked
            return (T) newsResponse;
        } else {
            response.close();
            throw new IllegalStateException(newsResponse.showapi_res_error);
        }
    } else {
        response.close();
        throw new IllegalStateException("基类错误无法解析!");
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JsonReader(com.google.gson.stream.JsonReader) NewsResponse(com.lzy.demo.model.NewsResponse)

Example 29 with JsonReader

use of com.google.gson.stream.JsonReader in project pinpoint by naver.

the class GsonIT method testFromV1_6.

@Test
public void testFromV1_6() throws Exception {
    if (!v1_6) {
        return;
    }
    final Gson gson = new Gson();
    final JsonElement jsonElement = getParseElements();
    /**
         * @see Gson#fromJson(JsonReader, InterceptPoint)
         */
    gson.fromJson(new JsonReader(new StringReader(json)), String.class);
    Method fromJson5 = Gson.class.getDeclaredMethod("fromJson", JsonReader.class, Type.class);
    /**
         * @see Gson#toJson(Object, InterceptPoint, JsonWriter)
         * @see Gson#toJson(JsonElement, JsonWriter)
         */
    gson.toJson(java, String.class, new JsonWriter(new StringWriter()));
    gson.toJson(jsonElement, new JsonWriter(new StringWriter()));
    Method toJson5 = Gson.class.getDeclaredMethod("toJson", Object.class, Type.class, JsonWriter.class);
    Method toJson8 = Gson.class.getDeclaredMethod("toJson", JsonElement.class, JsonWriter.class);
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
    verifier.verifyTrace(event(serviceType, fromJson5));
    verifier.verifyTrace(event(serviceType, toJson5), event(serviceType, toJson8));
    // No more traces
    verifier.verifyTraceCount(0);
}
Also used : StringWriter(java.io.StringWriter) JsonElement(com.google.gson.JsonElement) StringReader(java.io.StringReader) Gson(com.google.gson.Gson) JsonReader(com.google.gson.stream.JsonReader) Method(java.lang.reflect.Method) JsonWriter(com.google.gson.stream.JsonWriter) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) Test(org.junit.Test)

Example 30 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)

Aggregations

JsonReader (com.google.gson.stream.JsonReader)95 StringReader (java.io.StringReader)36 JsonElement (com.google.gson.JsonElement)30 Test (org.junit.Test)19 JsonObject (com.google.gson.JsonObject)17 IOException (java.io.IOException)17 InputStreamReader (java.io.InputStreamReader)17 JsonParser (com.google.gson.JsonParser)11 HumanReadableException (com.facebook.buck.util.HumanReadableException)10 Gson (com.google.gson.Gson)9 TypeToken (com.google.gson.reflect.TypeToken)8 JsonWriter (com.google.gson.stream.JsonWriter)8 Map (java.util.Map)7 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 BufferedReader (java.io.BufferedReader)4