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}");
}
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("基类错误无法解析!");
}
}
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("基类错误无法解析!");
}
}
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);
}
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);
}
}
Aggregations