Exemplo n.º 1
0
 @Override
 public T fromBody(ResponseBody body) throws IOException {
   Reader reader = body.charStream();
   try {
     return typeAdapter.fromJson(reader);
   } finally {
     closeQuietly(reader);
   }
 }
 public void checkSimpleComposedObject() throws IOException {
   ValidatedConstructor x = new ValidatedConstructor(Optional.of(0), 1);
   ValidatedConstructor y = new ValidatedConstructor(Optional.of(1), 2);
   TypeAdapter<ComposedObjects> adapter =
       parameterizedCtorFactory.create(gson, TypeToken.get(ComposedObjects.class));
   assertEquals(
       new ComposedObjects(x, y),
       adapter.fromJson("{\"x\":{\"foo\":0,\"bar\":1},\"y\":{\"foo\":1,\"bar\":2}}"));
 }
  public void testGenericParamsCopiedIn() throws IOException {
    TypeAdapter<GenericParamsCopiedIn> adapter =
        parameterizedCtorFactory.create(gson, TypeToken.get(GenericParamsCopiedIn.class));
    List<String> inputFoo = Lists.newArrayList();
    inputFoo.add("one");
    Map<String, String> inputBar = Maps.newHashMap();
    inputBar.put("2", "two");

    GenericParamsCopiedIn toTest =
        adapter.fromJson("{ \"foo\":[\"one\"], \"bar\":{ \"2\":\"two\"}}");
    assertEquals(inputFoo, toTest.foo);
    assertNotSame(inputFoo, toTest.foo);
    assertEquals(inputBar, toTest.bar);
  }
Exemplo n.º 4
0
 public T fromBody(ResponseBody value, Request request) throws IOException {
   String string = value.string();
   System.out.println("网络请求到的字符串:" + string);
   Reader reader =
       new InputStreamReader((new ByteArrayInputStream(string.getBytes(UTF8))), Util.UTF_8);
   try {
     T t = typeAdapter.fromJson(reader);
     System.out.println("转换的最终对象:" + t);
     String mimeType = value.contentType().toString();
     parseCache(request, t, string, mimeType);
     return t;
   } finally {
     closeQuietly(reader);
   }
 }
 @Test(expectedExceptions = NullPointerException.class)
 public void testPartialObjectStillThrows() throws IOException {
   TypeAdapter<ComposedObjects> adapter =
       parameterizedCtorFactory.create(gson, TypeToken.get(ComposedObjects.class));
   assertNull(adapter.fromJson("{\"x\":{\"foo\":0,\"bar\":1}}"));
 }
 public void testEmptyObjectIsNull() throws IOException {
   TypeAdapter<ComposedObjects> adapter =
       parameterizedCtorFactory.create(gson, TypeToken.get(ComposedObjects.class));
   assertNull(adapter.fromJson("{}"));
 }
 public void testRenamedFields() throws IOException {
   TypeAdapter<RenamedFields> adapter =
       parameterizedCtorFactory.create(gson, TypeToken.get(RenamedFields.class));
   assertEquals(new RenamedFields(0, 1), adapter.fromJson("{\"foo\":0,\"_bar\":1}"));
   assertEquals(adapter.toJson(new RenamedFields(0, 1)), "{\"foo\":0,\"_bar\":1}");
 }