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


Java Moshi.adapter方法代碼示例

本文整理匯總了Java中com.squareup.moshi.Moshi.adapter方法的典型用法代碼示例。如果您正苦於以下問題:Java Moshi.adapter方法的具體用法?Java Moshi.adapter怎麽用?Java Moshi.adapter使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.squareup.moshi.Moshi的用法示例。


在下文中一共展示了Moshi.adapter方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: writeTranslationList

import com.squareup.moshi.Moshi; //導入方法依賴的package包/類
void writeTranslationList(TranslationList list) {
  File cacheFile = getCachedFile();
  try {
    File directory = cacheFile.getParentFile();
    boolean directoryExists = directory.mkdirs() || directory.isDirectory();
    if (directoryExists) {
      if (cacheFile.exists()) {
        cacheFile.delete();
      }
      Moshi moshi = new Moshi.Builder().build();
      JsonAdapter<TranslationList> jsonAdapter = moshi.adapter(TranslationList.class);
      BufferedSink sink = Okio.buffer(Okio.sink(cacheFile));
      jsonAdapter.toJson(sink, list);
      sink.close();
      quranSettings.setLastUpdatedTranslationDate(System.currentTimeMillis());
    }
  } catch (Exception e) {
    cacheFile.delete();
    Crashlytics.logException(e);
  }
}
 
開發者ID:Elias33,項目名稱:Quran,代碼行數:22,代碼來源:TranslationManagerPresenter.java

示例2: API

import com.squareup.moshi.Moshi; //導入方法依賴的package包/類
API(Context context, Level apiLevel) {
    this.apiLevel = apiLevel;
    final Moshi moshi = new Moshi.Builder()
            .add(Folder.class, new FolderTypeAdapter())
            .add(Feed.class, new FeedTypeAdapter())
            .add(Item.class, new ItemTypeAdapter())
            .add(User.class, new UserTypeAdapter())
            .add(Status.class, new StatusTypeAdapter())
            .build();

    converterFactory = MoshiConverterFactory.create(moshi);

    errorJsonAdapter = moshi.adapter(NewsError.class);

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    String username = Preferences.USERNAME.getString(sharedPreferences);
    if (username != null) {
        String password = Preferences.PASSWORD.getString(sharedPreferences);
        String url = Preferences.URL.getString(sharedPreferences);
        setupApi(new HttpManager(username, password, HttpUrl.parse(url)));
    }
}
 
開發者ID:schaal,項目名稱:ocreader,代碼行數:23,代碼來源:API.java

示例3: getFavorites

import com.squareup.moshi.Moshi; //導入方法依賴的package包/類
public List<Movie> getFavorites() throws IOException
{
    Map<String, ?> allEntries = pref.getAll();
    ArrayList<Movie> movies = new ArrayList<>(24);
    Moshi moshi = new Moshi.Builder().build();

    for (Map.Entry<String, ?> entry : allEntries.entrySet())
    {
        String movieJson = pref.getString(entry.getKey(), null);

        if (!TextUtils.isEmpty(movieJson))
        {
            JsonAdapter<Movie> jsonAdapter = moshi.adapter(Movie.class);

            Movie movie = jsonAdapter.fromJson(movieJson);
            movies.add(movie);
        } else
        {
            // Do nothing;
        }
    }
    return movies;
}
 
開發者ID:rohanoid5,項目名稱:CineBuff,代碼行數:24,代碼來源:FavoritesStore.java

示例4: getDocumentAdapter

import com.squareup.moshi.Moshi; //導入方法依賴的package包/類
public <T extends ResourceIdentifier> JsonAdapter<Document<T>> getDocumentAdapter(Class<T> typeParameter,
                                                                                  Class<? extends Resource>... knownTypes) {
    Moshi moshi;
    if (typeParameter == null) {
        return (JsonAdapter) TestUtil.moshi(knownTypes).adapter(Document.class);
    } else if (typeParameter.getAnnotation(JsonApi.class) != null) {
        Class<? extends Resource>[] types = new Class[knownTypes.length + 1];
        types[0] = (Class) typeParameter;
        for (int i = 0; i != knownTypes.length; i++) {
            types[i + 1] = knownTypes[i];
        }
        moshi = TestUtil.moshi(types);
    } else {
        moshi = TestUtil.moshi(knownTypes);
    }
    return moshi.adapter(Types.newParameterizedType(Document.class, typeParameter));
}
 
開發者ID:kamikat,項目名稱:moshi-jsonapi,代碼行數:18,代碼來源:DocumentTest.java

示例5: IftttApiClient

import com.squareup.moshi.Moshi; //導入方法依賴的package包/類
private IftttApiClient() {
    Moshi moshi = new Moshi.Builder().add(new HexColorJsonAdapter())
            .add(Date.class, new Rfc3339DateJsonAdapter().nullSafe())
            .add(new AppletJsonAdapter())
            .add(new AppletListJsonAdapter())
            .build();
    JsonAdapter<ErrorResponse> errorResponseJsonAdapter = moshi.adapter(ErrorResponse.class);

    tokenInterceptor = new TokenInterceptor();
    inviteCodeInterceptor = new InviteCodeInterceptor();
    OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(tokenInterceptor)
            .addInterceptor(inviteCodeInterceptor)
            .addInterceptor(new SdkInfoInterceptor())
            .build();
    Retrofit retrofit = new Retrofit.Builder().addConverterFactory(MoshiConverterFactory.create(moshi))
            .baseUrl("https://api.ifttt.com")
            .client(okHttpClient)
            .build();

    RetrofitAppletConfigApi retrofitAppletConfigApi = retrofit.create(RetrofitAppletConfigApi.class);
    appletConfigApi = new InternalAppletConfigApi(retrofitAppletConfigApi, errorResponseJsonAdapter);

    RetrofitAppletsApi retrofitAppletsApi = retrofit.create(RetrofitAppletsApi.class);
    appletsApi = new InternalAppletsApi(retrofitAppletsApi, errorResponseJsonAdapter);

    RetrofitUserApi retrofitUserApi = retrofit.create(RetrofitUserApi.class);
    userApi = new InternalUserApi(retrofitUserApi, errorResponseJsonAdapter);
}
 
開發者ID:IFTTT,項目名稱:IFTTTSDK-Android,代碼行數:29,代碼來源:IftttApiClient.java

示例6: setUp

import com.squareup.moshi.Moshi; //導入方法依賴的package包/類
@Before
public void setUp() throws Exception {
    Moshi moshi = new Moshi.Builder().add(Date.class, new Rfc3339DateJsonAdapter().nullSafe())
            .add(new TestHexColorJsonAdapter())
            .add(new AppletJsonAdapter())
            .build();
    adapter = moshi.adapter(Applet.class);
}
 
開發者ID:IFTTT,項目名稱:IFTTTSDK-Android,代碼行數:9,代碼來源:AppletTest.java

示例7: loadConfigurationFromFile

import com.squareup.moshi.Moshi; //導入方法依賴的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

示例8: setFavorite

import com.squareup.moshi.Moshi; //導入方法依賴的package包/類
public void setFavorite(Movie movie)
{
    SharedPreferences.Editor editor = pref.edit();
    Moshi moshi = new Moshi.Builder().build();
    JsonAdapter<Movie> jsonAdapter = moshi.adapter(Movie.class);
    String movieJson = jsonAdapter.toJson(movie);
    editor.putString(movie.getId(), movieJson);
    editor.apply();
}
 
開發者ID:rohanoid5,項目名稱:CineBuff,代碼行數:10,代碼來源:FavoritesStore.java

示例9: deserialize_object_to_object_typed_document

import com.squareup.moshi.Moshi; //導入方法依賴的package包/類
@Test
public void deserialize_object_to_object_typed_document() throws Exception {
    Moshi moshi = TestUtil.moshi(Article.class);
    JsonAdapter<?> adapter = moshi.adapter(Types.newParameterizedType(ObjectDocument.class, Article.class));
    assertThat(adapter, instanceOf(ResourceAdapterFactory.DocumentAdapter.class));
    ObjectDocument<Article> objectDocument = ((ObjectDocument<Article>) adapter.fromJson(TestUtil.fromResource("/single.json")));
    assertThat(objectDocument, instanceOf(ObjectDocument.class));
    assertOnArticle1(objectDocument.asObjectDocument().get());
}
 
開發者ID:kamikat,項目名稱:moshi-jsonapi,代碼行數:10,代碼來源:DocumentTest.java

示例10: prepareRouteResponse

import com.squareup.moshi.Moshi; //導入方法依賴的package包/類
private RouteResponse prepareRouteResponse() {
	String json = new TestHelper().getTextContentFromResourcesFile("data.json");
	Moshi moshi = new Moshi.Builder().build();
	JsonAdapter<RouteResponse> jsonAdapter = moshi.adapter(RouteResponse.class);
	try {
		return jsonAdapter.fromJson(json);
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
}
 
開發者ID:Zlate87,項目名稱:sample-transport-app,代碼行數:11,代碼來源:ViewModelMappingServiceTest.java

示例11: onCreate

import com.squareup.moshi.Moshi; //導入方法依賴的package包/類
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTransaction = null;
    if (savedInstanceState == null) {
        try {
            Moshi moshi = new Moshi.Builder()
                    .add(new JsonDateAdapter())
                    .add(MyAdapterFactory.create())
                    .build();
            InputStream inputStream = getResources().openRawResource(R.raw.data);
            Source source = Okio.source(inputStream);
            BufferedSource bufferedSource = Okio.buffer(source);
            String json = bufferedSource.readUtf8();
            Log.d("AutoValue", "Raw JSON: " + json);
            JsonAdapter<Transaction> transactionJsonAdapter = moshi.adapter(Transaction.class);
            mTransaction = transactionJsonAdapter.fromJson(json);
        } catch (IOException e) {
            Log.e("AutoValue", "Error parsing JSON!", e);
        }
    } else {
        mTransaction = savedInstanceState.getParcelable("transaction");
    }

    Log.d("AutoValue", "Read: " + mTransaction);
}
 
開發者ID:ErikHellman,項目名稱:AutoValueDemo,代碼行數:29,代碼來源:MainActivity.java

示例12: deserializeJson

import com.squareup.moshi.Moshi; //導入方法依賴的package包/類
/**
 * Method that will deserialize a JSON string into {@code RouteResponse}.
 *
 * @param routeResponseJson the JSON string
 * @return the deserialized {@code RouteResponse}
 *
 * @throws RuntimeException if there is a problem with the deserialization of the JSON.
 */
protected RouteResponse deserializeJson(String routeResponseJson) {
	Moshi moshi = new Moshi.Builder().build();
	JsonAdapter<RouteResponse> jsonAdapter = moshi.adapter(RouteResponse.class);
	try {
		return jsonAdapter.fromJson(routeResponseJson);
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
}
 
開發者ID:Zlate87,項目名稱:sample-transport-app,代碼行數:18,代碼來源:AbstractJsonRoutingServer.java

示例13: BookmarkJsonModel

import com.squareup.moshi.Moshi; //導入方法依賴的package包/類
@Inject
BookmarkJsonModel() {
  Moshi moshi = new Moshi.Builder().build();
  jsonAdapter = moshi.adapter(BookmarkData.class);
}
 
開發者ID:Elias33,項目名稱:Quran,代碼行數:6,代碼來源:BookmarkJsonModel.java

示例14: newRealmListAdapter

import com.squareup.moshi.Moshi; //導入方法依賴的package包/類
private static <T extends RealmObject> JsonAdapter<RealmList<T>> newRealmListAdapter(Type type, Moshi moshi) {
    Type elementType = Types.collectionElementType(type, RealmList.class);
    JsonAdapter<T> elementAdapter = moshi.adapter(elementType);
    return new RealmListAdapter<>(elementAdapter);
}
 
開發者ID:Commit451,項目名稱:Regalia,代碼行數:6,代碼來源:RealmListJsonAdapterFactory.java

示例15: fromJson

import com.squareup.moshi.Moshi; //導入方法依賴的package包/類
public static RequestJson fromJson(final String json) throws IOException {
  // TODO: verify type=start
  Moshi moshi = new Moshi.Builder().build();
  JsonAdapter<RequestJson> adapter = moshi.adapter(RequestJson.class);
  return adapter.fromJson(json);
}
 
開發者ID:strykeforce,項目名稱:thirdcoast,代碼行數:7,代碼來源:Subscription.java


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