本文整理汇总了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");
}
}
示例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}}}");
}
示例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");
}
}
示例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");
}
示例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);
}
}
示例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");
}
}
示例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");
}
}
示例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 $");
}
}
示例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");
}
}
示例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");
}
}
示例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");
}
}
示例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;
}
}
示例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);
}
}
示例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);
}
}
示例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);
}
}