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


Java JsonAdapter.fromJson方法代碼示例

本文整理匯總了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());
  }
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:18,代碼來源:SlackApi.java

示例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;
}
 
開發者ID:IFTTT,項目名稱:IFTTTSDK-Android,代碼行數:27,代碼來源:AppletListJsonAdapter.java

示例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.
}
 
開發者ID:JakeWharton,項目名稱:jax-rs-moshi,代碼行數:12,代碼來源:MoshiMessageBodyReader.java

示例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");
  }
}
 
開發者ID:serj-lotutovici,項目名稱:moshi-lazy-adapters,代碼行數:17,代碼來源:WrappedJsonAdapterTest.java

示例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\"]}");
}
 
開發者ID:serj-lotutovici,項目名稱:moshi-lazy-adapters,代碼行數:17,代碼來源:FirstElementJsonAdapterTest.java

示例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);
}
 
開發者ID:serj-lotutovici,項目名稱:moshi-lazy-adapters,代碼行數:18,代碼來源:LazyAdaptersAutoValueTest.java

示例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}}}}");
}
 
開發者ID:serj-lotutovici,項目名稱:moshi-lazy-adapters,代碼行數:21,代碼來源:WrappedJsonAdapterTest.java

示例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);
}
 
開發者ID:serj-lotutovici,項目名稱:moshi-lazy-adapters,代碼行數:20,代碼來源:WrappedJsonAdapterTest.java

示例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);
    }
}
 
開發者ID:philipwhiuk,項目名稱:q-mail,代碼行數:14,代碼來源:PendingCommandSerializer.java

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

示例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);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:12,代碼來源:Moshi1IT.java

示例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);
}
 
開發者ID:Commit451,項目名稱:Regalia,代碼行數:10,代碼來源:RealmListJsonAdapterFactoryTests.java

示例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);
    }
}
 
開發者ID:thebagchi,項目名稱:heimdall-proxy,代碼行數:12,代碼來源:HeimdallProxyApplication.java

示例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);
  }
}
 
開發者ID:rogues-dev,項目名稱:hoard,代碼行數:12,代碼來源:MoshiSerializer.java

示例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


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