当前位置: 首页>>代码示例>>Java>>正文


Java JsonDataException类代码示例

本文整理汇总了Java中com.squareup.moshi.JsonDataException的典型用法代码示例。如果您正苦于以下问题:Java JsonDataException类的具体用法?Java JsonDataException怎么用?Java JsonDataException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


JsonDataException类属于com.squareup.moshi包,在下文中一共展示了JsonDataException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: failOnNotFound

import com.squareup.moshi.JsonDataException; //导入依赖的package包/类
@Test public void failOnNotFound() throws Exception {
  JsonAdapter<Data2> adapter = moshi.adapter(Data2.class);

  try {
    adapter.fromJson("{\n"
        + "  \"data\": {\n"
        + "    \"1\": {\n"
        + "      \"2\": null\n"
        + "    }\n"
        + "  }\n"
        + "}");
    fail();
  } catch (JsonDataException ex) {
    assertThat(ex).hasMessage(
        "Wrapped Json expected at path: [1, 2]. Found null at $.data.1.2");
  }
}
 
开发者ID:serj-lotutovici,项目名称:moshi-lazy-adapters,代码行数:18,代码来源:WrappedJsonAdapterTest.java

示例2: notNullSafe

import com.squareup.moshi.JsonDataException; //导入依赖的package包/类
@Test public void notNullSafe() throws Exception {
  JsonAdapter<Data2> adapter = moshi.adapter(Data2.class);

  try {
    adapter.fromJson("{\n"
        + "  \"data\": null\n"
        + "}");
    fail();
  } catch (JsonDataException expected) {
  }

  Data2 data2 = new Data2();
  String toJson = adapter.toJson(data2);
  assertThat(toJson).isEqualTo("{}");

  toJson = adapter.serializeNulls().toJson(data2);
  assertThat(toJson).isEqualTo("{\"data\":{\"1\":{\"2\":null}}}");
}
 
开发者ID:serj-lotutovici,项目名称:moshi-lazy-adapters,代码行数:19,代码来源:WrappedJsonAdapterTest.java

示例3: fromJsonOnIncorrectPath

import com.squareup.moshi.JsonDataException; //导入依赖的package包/类
@Test public void fromJsonOnIncorrectPath() throws Exception {
  JsonAdapter<Data2> adapter = moshi.adapter(Data2.class);

  try {
    adapter.fromJson("{\n"
        + "  \"data\": {\n"
        + "    \"2\": {\n"
        + "      \"1\": null\n"
        + "    }\n"
        + "  }\n"
        + "}");
    fail();
  } catch (JsonDataException e) {
    assertThat(e).hasMessage("Wrapped Json expected at path: [1, 2]. Actual: $.data");
  }
}
 
开发者ID:serj-lotutovici,项目名称:moshi-lazy-adapters,代码行数:17,代码来源:WrappedJsonAdapterTest.java

示例4: fromJson

import com.squareup.moshi.JsonDataException; //导入依赖的package包/类
@FromJson
boolean fromJson(final Object json) {
    final String jsonString = json.toString();
    final Integer jsonInteger = toIntOrNull(jsonString);

    if (jsonInteger != null) {
        if (jsonInteger == 1) {
            return true;
        } else if (jsonInteger == 0) {
            return false;
        }
    } else {
        if ("true".equals(jsonString)) {
            return true;
        } else if ("false".equals(jsonString)) {
            return false;
        }
    }

    throw new JsonDataException("Unable to map " + json + " to a boolean value");
}
 
开发者ID:proxer,项目名称:ProxerLibJava,代码行数:22,代码来源:PrimitiveBooleanAdapter.java

示例5: handleResponse

import com.squareup.moshi.JsonDataException; //导入依赖的package包/类
private void handleResponse(final Response response) throws IOException {
    final String responseBody = peekResponseBody(response);
    final Matcher errorMatcher = ERROR_PATTERN.matcher(responseBody);
    final HttpUrl url = response.request().url();

    if (errorMatcher.find()) {
        final int errorCode = Integer.parseInt(errorMatcher.group(1));
        final ServerErrorType errorType = ServerErrorType.fromErrorCodeOrNull(errorCode);

        if (errorType != null && isLoginError(errorType)) {
            loginTokenManager.persist(null);
        }
    } else if (url.pathSegments().equals(LOGIN_PATH)) {
        final Matcher loginTokenMatcher = LOGIN_TOKEN_PATTERN.matcher(responseBody);

        if (loginTokenMatcher.find()) {
            loginTokenManager.persist(loginTokenMatcher.group(1).trim());
        } else {
            throw new JsonDataException("No token found after successful login.");
        }
    } else if (url.pathSegments().equals(LOGOUT_PATH)) {
        loginTokenManager.persist(null);
    }
}
 
开发者ID:proxer,项目名称:ProxerLibJava,代码行数:25,代码来源:LoginTokenInterceptor.java

示例6: failOnUnknown

import com.squareup.moshi.JsonDataException; //导入依赖的package包/类
@Test public void failOnUnknown() throws IOException, InterruptedException {
  server.enqueue(new MockResponse().setBody("{\"taco\":\"delicious\"}"));

  Call<AnImplementation> call = serviceFailOnUnknown.anImplementation(new AnImplementation(null));
  try {
    call.execute();
    fail();
  } catch (JsonDataException e) {
    assertThat(e).hasMessage("Cannot skip unexpected STRING at $.taco");
  }
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:12,代码来源:MoshiConverterFactoryTest.java

示例7: fromJsonExpectsAnArray

import com.squareup.moshi.JsonDataException; //导入依赖的package包/类
@Test public void fromJsonExpectsAnArray() throws Exception {
  JsonAdapter<Data> adapter = moshi.adapter(Data.class);

  try {
    adapter.fromJson("{\n"
        + "  \"obj\": \"this_will_throw\"\n"
        + "}");
    fail();
  } catch (JsonDataException e) {
    // Moshi's Collection adapter will throw
    assertThat(e).hasMessage("Expected BEGIN_ARRAY but was STRING at path $.obj");
  }
}
 
开发者ID:serj-lotutovici,项目名称:moshi-lazy-adapters,代码行数:14,代码来源:ElementAtJsonAdapterTest.java

示例8: ignoresUnannotatedEnums

import com.squareup.moshi.JsonDataException; //导入依赖的package包/类
@Test public void ignoresUnannotatedEnums() throws Exception {
  JsonAdapter<Regular> adapter = moshi.adapter(Regular.class).lenient();
  assertThat(adapter.fromJson("\"ONE\"")).isEqualTo(Regular.ONE);

  try {
    adapter.fromJson("\"TWO\"");
    fail();
  } catch (JsonDataException expected) {
    assertThat(expected).hasMessage(
        "Expected one of [ONE] but was TWO at path $");
  }
}
 
开发者ID:serj-lotutovici,项目名称:moshi-lazy-adapters,代码行数:13,代码来源:FallbackEnumJsonAdapterTest.java

示例9: failOnNotFound2

import com.squareup.moshi.JsonDataException; //导入依赖的package包/类
@Test public void failOnNotFound2() throws Exception {
  JsonAdapter<Data2> adapter = moshi.adapter(Data2.class);

  try {
    adapter.fromJson("{\n"
        + "  \"data\": {\n"
        + "    \"1\": null\n"
        + "  }\n"
        + "}");
    fail();
  } catch (JsonDataException ex) {
    assertThat(ex).hasMessage(
        "Wrapped Json expected at path: [1, 2]. Found null at $.data.1");
  }
}
 
开发者ID:serj-lotutovici,项目名称:moshi-lazy-adapters,代码行数:16,代码来源:WrappedJsonAdapterTest.java

示例10: fromJsonDoesNotSwallowJsonDataExceptions

import com.squareup.moshi.JsonDataException; //导入依赖的package包/类
@Test public void fromJsonDoesNotSwallowJsonDataExceptions() throws Exception {
  JsonAdapter<Data3> adapter = moshi.adapter(Data3.class);

  try {
    adapter.fromJson("{\n"
        + "  \"str\": {\n"
        + "    \"1\": false\n"
        + "  }\n"
        + "}");
    fail();
  } catch (JsonDataException e) {
    assertThat(e).hasMessage("Expected a string but was BOOLEAN at path $.str.1");
  }
}
 
开发者ID:serj-lotutovici,项目名称:moshi-lazy-adapters,代码行数:15,代码来源:WrappedJsonAdapterTest.java

示例11: fromJsonExpectsAnArray

import com.squareup.moshi.JsonDataException; //导入依赖的package包/类
@Test public void fromJsonExpectsAnArray() throws Exception {
  JsonAdapter<LastElementJsonAdapterTest.Data> adapter = moshi.adapter(Data.class);

  try {
    adapter.fromJson("{\n"
        + "  \"obj\": \"this_will_throw\"\n"
        + "}");
    fail();
  } catch (JsonDataException e) {
    // Moshi's Collection adapter will throw
    assertThat(e).hasMessage("Expected BEGIN_ARRAY but was STRING at path $.obj");
  }
}
 
开发者ID:serj-lotutovici,项目名称:moshi-lazy-adapters,代码行数:14,代码来源:LastElementJsonAdapterTest.java

示例12: fromJson

import com.squareup.moshi.JsonDataException; //导入依赖的package包/类
@Override public T fromJson(JsonReader reader) throws IOException {
  Object jsonValue = reader.readJsonValue();

  try {
    return delegate.fromJsonValue(jsonValue);
  } catch (JsonDataException ignore) {
    return defaultValue;
  }
}
 
开发者ID:serj-lotutovici,项目名称:moshi-lazy-adapters,代码行数:10,代码来源:DefaultOnDataMismatchAdapter.java

示例13: messageFrom

import com.squareup.moshi.JsonDataException; //导入依赖的package包/类
public Message messageFrom(final String payload) throws IOException {
    try {
        return messageAdapter.fromJson(payload);
    } catch (final JsonDataException ex) {
        throw new IOException(ex);
    }
}
 
开发者ID:toshiapp,项目名称:toshi-android-client,代码行数:8,代码来源:SofaAdapters.java

示例14: txRequestFrom

import com.squareup.moshi.JsonDataException; //导入依赖的package包/类
public PaymentRequest txRequestFrom(final String payload) throws IOException {
    try {
        return paymentRequestAdapter.fromJson(payload);
    } catch (final JsonDataException ex) {
        throw new IOException(ex);
    }
}
 
开发者ID:toshiapp,项目名称:toshi-android-client,代码行数:8,代码来源:SofaAdapters.java

示例15: unsignedW3TransactionFrom

import com.squareup.moshi.JsonDataException; //导入依赖的package包/类
public UnsignedW3Transaction unsignedW3TransactionFrom(final String payload) throws IOException {
    try {
        return unsignedW3TransactionAdapter.fromJson(payload);
    } catch (final JsonDataException ex) {
        throw new IOException(ex);
    }
}
 
开发者ID:toshiapp,项目名称:toshi-android-client,代码行数:8,代码来源:SofaAdapters.java


注:本文中的com.squareup.moshi.JsonDataException类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。