本文整理汇总了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);
}
}
示例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)));
}
}
示例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;
}
示例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));
}
示例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);
}
示例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);
}
示例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);
}
}
示例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();
}
示例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());
}
示例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);
}
}
示例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);
}
示例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);
}
}
示例13: BookmarkJsonModel
import com.squareup.moshi.Moshi; //导入方法依赖的package包/类
@Inject
BookmarkJsonModel() {
Moshi moshi = new Moshi.Builder().build();
jsonAdapter = moshi.adapter(BookmarkData.class);
}
示例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);
}
示例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);
}