本文整理汇总了Java中com.google.gson.stream.JsonReader类的典型用法代码示例。如果您正苦于以下问题:Java JsonReader类的具体用法?Java JsonReader怎么用?Java JsonReader使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JsonReader类属于com.google.gson.stream包,在下文中一共展示了JsonReader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: read
import com.google.gson.stream.JsonReader; //导入依赖的package包/类
public T read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}
T instance = this.constructor.construct();
try {
in.beginObject();
while (in.hasNext()) {
BoundField field = (BoundField) this.boundFields.get(in.nextName());
if (field == null || !field.deserialized) {
in.skipValue();
} else {
field.read(in, instance);
}
}
in.endObject();
return instance;
} catch (Throwable e) {
throw new JsonSyntaxException(e);
} catch (IllegalAccessException e2) {
throw new AssertionError(e2);
}
}
示例2: fromJson
import com.google.gson.stream.JsonReader; //导入依赖的package包/类
public <T> T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException,
JsonSyntaxException {
boolean isEmpty = true;
boolean oldLenient = reader.isLenient();
reader.setLenient(true);
try {
reader.peek();
isEmpty = false;
T object = getAdapter(TypeToken.get(typeOfT)).read(reader);
reader.setLenient(oldLenient);
return object;
} catch (Throwable e) {
if (isEmpty) {
reader.setLenient(oldLenient);
return null;
}
throw new JsonSyntaxException(e);
} catch (Throwable e2) {
throw new JsonSyntaxException(e2);
} catch (Throwable e22) {
throw new JsonSyntaxException(e22);
} catch (Throwable th) {
reader.setLenient(oldLenient);
}
}
示例3: longAdapter
import com.google.gson.stream.JsonReader; //导入依赖的package包/类
private TypeAdapter<Number> longAdapter(LongSerializationPolicy longSerializationPolicy) {
if (longSerializationPolicy == LongSerializationPolicy.DEFAULT) {
return TypeAdapters.LONG;
}
return new TypeAdapter<Number>() {
public Number read(JsonReader in) throws IOException {
if (in.peek() != JsonToken.NULL) {
return Long.valueOf(in.nextLong());
}
in.nextNull();
return null;
}
public void write(JsonWriter out, Number value) throws IOException {
if (value == null) {
out.nullValue();
} else {
out.value(value.toString());
}
}
};
}
示例4: create
import com.google.gson.stream.JsonReader; //导入依赖的package包/类
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
if (typeToken.getRawType() != Timestamp.class) {
return null;
}
final TypeAdapter<Date> dateTypeAdapter = gson.getAdapter(Date.class);
return new TypeAdapter<Timestamp>() {
public Timestamp read(JsonReader in) throws IOException {
Date date = (Date) dateTypeAdapter.read(in);
return date != null ? new Timestamp(date.getTime()) : null;
}
public void write(JsonWriter out, Timestamp value) throws IOException {
dateTypeAdapter.write(out, value);
}
};
}
示例5: testConvertJsonFromFileReaderToObject_Success
import com.google.gson.stream.JsonReader; //导入依赖的package包/类
@Test
public void testConvertJsonFromFileReaderToObject_Success() throws Exception {
FileReader mockFileReader = Mockito.mock(FileReader.class);
Class<?> responseClass = GetEventResponseVO.class;
Gson mockGson = PowerMockito.mock(Gson.class);
JsonReader mockJsonReader = Mockito.mock(JsonReader.class);
PowerMockito.whenNew(JsonReader.class).withArguments(Mockito.isA(FileReader.class)).thenReturn(mockJsonReader);
Mockito.when(mockGson.fromJson(Mockito.isA(JsonReader.class), Mockito.isA(Class.class))).thenReturn(ImmutableGetEventResponseVO.builder().build());
Object responseObject = JsonUtils.convertJsonFromFileReaderToObject(mockGson, mockFileReader, responseClass);
assertThat(responseObject).isNotNull();
PowerMockito.verifyNew(JsonReader.class, Mockito.times(1)).withArguments(Mockito.isA(FileReader.class));
Mockito.verify(mockGson, Mockito.times(1)).fromJson(Mockito.isA(JsonReader.class), Mockito.isA(Class.class));
}
示例6: create
import com.google.gson.stream.JsonReader; //导入依赖的package包/类
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
return new TypeAdapter<T>() {
public void write(JsonWriter out, T value) throws IOException {
delegate.write(out, value);
}
public T read(JsonReader in) throws IOException {
T obj = delegate.read(in);
if (obj instanceof PostProcessable) {
((PostProcessable) obj).postProcess();
}
return obj;
}
};
}
示例7: floatAdapter
import com.google.gson.stream.JsonReader; //导入依赖的package包/类
private TypeAdapter<Number> floatAdapter(boolean serializeSpecialFloatingPointValues) {
if (serializeSpecialFloatingPointValues) {
return TypeAdapters.FLOAT;
}
return new TypeAdapter<Number>() {
@Override public Float read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}
return (float) in.nextDouble();
}
@Override public void write(JsonWriter out, Number value) throws IOException {
if (value == null) {
out.nullValue();
return;
}
float floatValue = value.floatValue();
checkValidFloatingPoint(floatValue);
out.value(value);
}
};
}
示例8: nullSafe
import com.google.gson.stream.JsonReader; //导入依赖的package包/类
public final TypeAdapter<T> nullSafe() {
return new TypeAdapter<T>() {
public void write(JsonWriter out, T value) throws IOException {
if (value == null) {
out.nullValue();
} else {
TypeAdapter.this.write(out, value);
}
}
public T read(JsonReader reader) throws IOException {
if (reader.peek() != JsonToken.NULL) {
return TypeAdapter.this.read(reader);
}
reader.nextNull();
return null;
}
};
}
示例9: deserializeResponseBody
import com.google.gson.stream.JsonReader; //导入依赖的package包/类
private <T> T deserializeResponseBody(Response response, Class<? extends T> bodyType) throws IOException {
try {
if (!response.isSuccessful()) {
throw new APIHttpException(response.code(), response.message());
}
JsonReader reader = new JsonReader(new BufferedReader(new InputStreamReader(response.body().byteStream())));
try {
return gson.fromJson(reader, bodyType);
} catch (JsonSyntaxException e) {
throw new IOException("Malformed JSON response.", e);
} finally {
closeQuietly(reader);
}
} finally {
closeQuietly(response);
}
}
示例10: parse
import com.google.gson.stream.JsonReader; //导入依赖的package包/类
public JsonElement parse(Reader json) throws JsonIOException, JsonSyntaxException {
try {
JsonReader jsonReader = new JsonReader(json);
JsonElement element = parse(jsonReader);
if (element.isJsonNull() || jsonReader.peek() == JsonToken.END_DOCUMENT) {
return element;
}
throw new JsonSyntaxException("Did not consume the entire document.");
} catch (Throwable e) {
throw new JsonSyntaxException(e);
} catch (Throwable e2) {
throw new JsonIOException(e2);
} catch (Throwable e22) {
throw new JsonSyntaxException(e22);
}
}
示例11: longAdapter
import com.google.gson.stream.JsonReader; //导入依赖的package包/类
private TypeAdapter<Number> longAdapter(LongSerializationPolicy longSerializationPolicy) {
if (longSerializationPolicy == LongSerializationPolicy.DEFAULT) {
return TypeAdapters.LONG;
}
return new TypeAdapter<Number>() {
@Override public Number read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}
return in.nextLong();
}
@Override public void write(JsonWriter out, Number value) throws IOException {
if (value == null) {
out.nullValue();
return;
}
out.value(value.toString());
}
};
}
示例12: deserialize
import com.google.gson.stream.JsonReader; //导入依赖的package包/类
/**
* Deserialize the given JSON string to Java object.
*
* @param <T> Type
* @param body The JSON string
* @param returnType The type to deserialize into
* @return The deserialized Java object
*/
@SuppressWarnings("unchecked")
public <T> T deserialize(String body, Type returnType) {
try {
if (apiClient.isLenientOnJson()) {
JsonReader jsonReader = new JsonReader(new StringReader(body));
// see https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/stream/JsonReader.html#setLenient(boolean)
jsonReader.setLenient(true);
return gson.fromJson(jsonReader, returnType);
} else {
return gson.fromJson(body, returnType);
}
} catch (JsonParseException e) {
// Fallback processing when failed to parse JSON form response body:
// return the response body string directly for the String return type;
// parse response body into date or datetime for the Date return type.
if (returnType.equals(String.class))
return (T) body;
else if (returnType.equals(Date.class))
return (T) apiClient.parseDateOrDatetime(body);
else throw(e);
}
}
示例13: init
import com.google.gson.stream.JsonReader; //导入依赖的package包/类
@Before
public void init() throws Exception {
MockitoAnnotations.initMocks(this);
sessionControllerTest = new SessionController();
directoryTest = new File(TEST_WORKSPACE_PATH);
fileReaderTest = new FileReader(TEST_WORKSPACE_PATH + "2.json");
Mockito.when(requestMock.params(Mockito.eq(":name"))).thenReturn("2");
InputStream is = SessionController.class.getClassLoader().getResourceAsStream("mockSessionSchedule.json");
JsonReader reader = new JsonReader(new InputStreamReader(is));
Schedule schedule = gson.fromJson(reader, new TypeToken<Schedule>() {
}.getType());
String scheduleStrTest = gson.toJson(schedule);
Mockito.when(requestMock.body()).thenReturn(scheduleStrTest);
PowerMockito.whenNew(File.class).withParameterTypes(String.class)
.withArguments(Mockito.eq(WORKSPACE_PATH)).thenReturn(directoryTest);
PowerMockito.whenNew(FileReader.class).withParameterTypes(String.class)
.withArguments(Mockito.eq(WORKSPACE_PATH + "2.json")).thenReturn(fileReaderTest);
}
示例14: read
import com.google.gson.stream.JsonReader; //导入依赖的package包/类
@Override
public void read(JsonReader in, GsonProperty property, Object t) throws IOException {
final Class<?> simpleType = property.getType();
List list = new ArrayList();
in.beginArray();
if (simpleType.isPrimitive() || isBoxedClass(simpleType)) {
while (in.hasNext()){
list.add(SupportUtils.readPrimitiveOrItsBox(in, property));
}
}else{
TypeAdapter adapter = getTypeAdapter(simpleType);
while (in.hasNext()){
list.add(adapter.read(in));
}
}
//copy to array
Object array = Array.newInstance(simpleType, list.size());
for(int i = 0, size = list.size() ; i < size ; i ++){
Array.set(array, i, list.get(i));
}
setValue(property, t, array);
in.endArray();
}
示例15: read
import com.google.gson.stream.JsonReader; //导入依赖的package包/类
public Boolean read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
} else if (in.peek() == JsonToken.STRING) {
return Boolean.valueOf(Boolean.parseBoolean(in.nextString()));
} else {
return Boolean.valueOf(in.nextBoolean());
}
}