當前位置: 首頁>>代碼示例>>Java>>正文


Java JsonReader類代碼示例

本文整理匯總了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);
    }
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:25,代碼來源:ReflectiveTypeAdapterFactory.java

示例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);
    }
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:26,代碼來源:Gson.java

示例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());
            }
        }
    };
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:23,代碼來源:Gson.java

示例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);
        }
    };
}
 
開發者ID:JackChan1999,項目名稱:letv,代碼行數:17,代碼來源:TypeAdapters.java

示例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));
}
 
開發者ID:enjin,項目名稱:Enjin-Coin-Java-SDK,代碼行數:17,代碼來源:JsonUtilsTest.java

示例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;
        }
    };
}
 
開發者ID:albertogiunta,項目名稱:justintrain-client-android,代碼行數:18,代碼來源:PostProcessingEnabler.java

示例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);
    }
  };
}
 
開發者ID:odoo-mobile-intern,項目名稱:odoo-work,代碼行數:24,代碼來源:Gson.java

示例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;
        }
    };
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:20,代碼來源:TypeAdapter.java

示例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);
    }
}
 
開發者ID:pCloud,項目名稱:pcloud-sdk-java,代碼行數:19,代碼來源:RealApiClient.java

示例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);
    }
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:17,代碼來源:JsonParser.java

示例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());
    }
  };
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:22,代碼來源:Gson.java

示例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);
    }
}
 
開發者ID:osimloeff,項目名稱:PI-Web-API-Client-Java-Android,代碼行數:31,代碼來源:JSON.java

示例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);
}
 
開發者ID:cocolocomoco21,項目名稱:ULCRS,代碼行數:24,代碼來源:SessionControllerTest.java

示例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();
}
 
開發者ID:LightSun,項目名稱:data-mediator,代碼行數:25,代碼來源:TypeHandler.java

示例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());
    }
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:11,代碼來源:TypeAdapters.java


注:本文中的com.google.gson.stream.JsonReader類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。