本文整理匯總了Java中com.squareup.moshi.JsonAdapter.fromJson方法的典型用法代碼示例。如果您正苦於以下問題:Java JsonAdapter.fromJson方法的具體用法?Java JsonAdapter.fromJson怎麽用?Java JsonAdapter.fromJson使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.squareup.moshi.JsonAdapter
的用法示例。
在下文中一共展示了JsonAdapter.fromJson方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: exchangeCode
import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
/** See https://api.slack.com/methods/oauth.access. */
public OAuthSession exchangeCode(String code, HttpUrl redirectUrl) throws IOException {
HttpUrl url = baseUrl.newBuilder("oauth.access")
.addQueryParameter("client_id", clientId)
.addQueryParameter("client_secret", clientSecret)
.addQueryParameter("code", code)
.addQueryParameter("redirect_uri", redirectUrl.toString())
.build();
Request request = new Request.Builder()
.url(url)
.build();
Call call = httpClient.newCall(request);
try (Response response = call.execute()) {
JsonAdapter<OAuthSession> jsonAdapter = moshi.adapter(OAuthSession.class);
return jsonAdapter.fromJson(response.body().source());
}
}
示例2: fromJson
import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
@FromJson
List<Applet> fromJson(JsonReader jsonReader, JsonAdapter<Applet> delegate) throws IOException {
List<Applet> applets = new ArrayList<>();
jsonReader.beginObject();
while (jsonReader.hasNext()) {
int index = jsonReader.selectName(OPTIONS);
switch (index) {
case -1:
jsonReader.skipValue();
break;
case 0:
jsonReader.beginArray();
while (jsonReader.hasNext()) {
Applet applet = delegate.fromJson(jsonReader);
applets.add(applet);
}
jsonReader.endArray();
break;
default:
throw new IllegalStateException("Unknown index: " + index);
}
}
jsonReader.endObject();
return applets;
}
示例3: readFrom
import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
@Override public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations,
MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
throws IOException, WebApplicationException {
JsonAdapter<Object> adapter = moshi.adapter(genericType);
BufferedSource source = Okio.buffer(Okio.source(entityStream));
if (!source.request(1)) {
throw new NoContentException("Stream is empty");
}
return adapter.fromJson(source);
// Note: we do not close the InputStream per the interface documentation.
}
示例4: fromJsonOnIncorrectPath
import com.squareup.moshi.JsonAdapter; //導入方法依賴的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");
}
}
示例5: first
import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
@Test public void first() throws Exception {
JsonAdapter<Data> adapter = moshi.adapter(Data.class);
Data fromJson = adapter.fromJson("{\n"
+ " \"obj\": [\n"
+ " \"one\",\n"
+ " \"two\"\n"
+ " ]\n"
+ "}");
assertThat(fromJson.str).isEqualTo("one");
String toJson = adapter.toJson(fromJson);
// The excluded data is lost during parsing
// Adapter under test assumes that the consumer doesn't need that data
assertThat(toJson).isEqualTo("{\"obj\":[\"one\"]}");
}
示例6: unwrap
import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
@Test public void unwrap() throws Exception {
JsonAdapter<Data> adapter = moshi.adapter(Data.class);
Data fromJson = adapter.fromJson("{\n"
+ " \"name\": \"data_name\",\n"
+ " \"meta\": {\n"
+ " \"1\": {\n"
+ " \"value1\": \"value1\",\n"
+ " \"value2\": 2\n"
+ " }\n"
+ " }\n"
+ "}");
assertThat(fromJson.name()).isEqualTo("data_name");
assertThat(fromJson.meta().value1).isEqualTo("value1");
assertThat(fromJson.meta().value2).isEqualTo(2);
}
示例7: oneObject
import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
@Test public void oneObject() throws Exception {
JsonAdapter<Data2> adapter = moshi.adapter(Data2.class);
Data2 fromJson = adapter.fromJson("{\n"
+ " \"data\": {\n"
+ " \"1\": {\n"
+ " \"2\": {\n"
+ " \"str\": \"test\",\n"
+ " \"val\": 42\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ "}");
assertThat(fromJson).isNotNull();
assertThat(fromJson.data.str).isEqualTo("test");
assertThat(fromJson.data.val).isEqualTo(42);
String toJson = adapter.toJson(fromJson);
assertThat(toJson).isEqualTo("{\"data\":{\"1\":{\"2\":{\"str\":\"test\",\"val\":42}}}}");
}
示例8: fromJsonSkipsNonPathValues
import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
@Test public void fromJsonSkipsNonPathValues() throws Exception {
JsonAdapter<Data2> adapter = moshi.adapter(Data2.class);
Data2 fromJson = adapter.fromJson("{\n"
+ " \"data\": {\n"
+ " \"should_be_skipped\": null,\n"
+ " \"1\": {\n"
+ " \"2\": {\n"
+ " \"str\": \"works\",\n"
+ " \"val\": 11\n"
+ " }\n"
+ " }\n"
+ "\n"
+ " }\n"
+ "}");
assertThat(fromJson.data.str).isEqualTo("works");
assertThat(fromJson.data.val).isEqualTo(11);
}
示例9: unserialize
import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
public PendingCommand unserialize(long databaseId, String commandName, String data) {
JsonAdapter<? extends PendingCommand> adapter = adapters.get(commandName);
if (adapter == null) {
throw new IllegalArgumentException("Unsupported pending command type!");
}
try {
PendingCommand command = adapter.fromJson(data);
command.databaseId = databaseId;
return command;
} catch (IOException e) {
throw new IOError(e);
}
}
示例10: rtmStart
import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
/** See https://api.slack.com/methods/rtm.start. */
public RtmStartResponse rtmStart(String accessToken) throws IOException {
HttpUrl url = baseUrl.newBuilder("rtm.start")
.addQueryParameter("token", accessToken)
.build();
Request request = new Request.Builder()
.url(url)
.build();
Call call = httpClient.newCall(request);
try (Response response = call.execute()) {
JsonAdapter<RtmStartResponse> jsonAdapter = moshi.adapter(RtmStartResponse.class);
return jsonAdapter.fromJson(response.body().source());
}
}
示例11: assertJsonRoundTrip
import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
@SuppressWarnings({"unchecked", "rawtypes"})
private void assertJsonRoundTrip(ClassLoader resultsClassLoader, String className, String jsonResource) throws ClassNotFoundException, IOException {
Class generatedType = resultsClassLoader.loadClass(className);
String expectedJson = IOUtils.toString(getClass().getResource(jsonResource));
JsonAdapter<Object> jsonAdapter = moshi.adapter(generatedType);
Object javaInstance = jsonAdapter.fromJson(expectedJson);
String actualJson = jsonAdapter.toJson(javaInstance);
assertEqualsJson(expectedJson, actualJson);
}
示例12: parseList
import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
@Test
public void parseList() throws Exception {
String json = TestUtil.getFileText("cat-with-list-of-friends.json");
JsonAdapter<Cat> jsonAdapter = moshi.adapter(Cat.class);
Cat cat = jsonAdapter.fromJson(json);
Assert.assertNotNull(cat);
Assert.assertNotNull(cat.name);
Assert.assertNotNull(cat.friends);
}
示例13: loadConfigurationFromFile
import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
private void loadConfigurationFromFile() {
try {
String json = FileUtils.readFileToString(new File(HeimdallApplicationConstants.CONFIG_FILE));
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<HeimdallApplicationConfiguration> adaper =
moshi.adapter(HeimdallApplicationConfiguration.class);
configuration = adaper.fromJson(json);
} catch (Exception e) {
log.error("Failed loading configuration from file.", e);
}
}
示例14: deserialize
import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
@Override
public <T> T deserialize(Type type, InputStream inputStream) throws IOException {
BufferedSource buffer = null;
try {
buffer = Okio.buffer(Okio.source(inputStream));
JsonAdapter<T> adapter = moshi.adapter(type);
return adapter.fromJson(buffer);
} finally {
IOUtils.close(buffer);
}
}
示例15: serializesOnlyNonEmptyIntArray
import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
@Test public void serializesOnlyNonEmptyIntArray() throws Exception {
JsonAdapter<Data1> adapter = moshi.adapter(Data1.class);
Data1 fromJson = adapter.fromJson("{\n"
+ "\"intArray\": [1]\n"
+ "}");
assertThat(fromJson.intArray).containsExactly(1);
fromJson.intArray = new int[0];
assertThat(adapter.toJson(fromJson)).isEqualTo("{}");
fromJson.intArray = new int[] { 5 };
assertThat(adapter.toJson(fromJson)).isEqualTo("{\"intArray\":[5]}");
}
開發者ID:serj-lotutovici,項目名稱:moshi-lazy-adapters,代碼行數:15,代碼來源:SerializeOnlyNonEmptyJsonAdapterTest.java