com.nytimes.android.external.store3.middleware.moshi.MoshiStringParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of middleware-moshi3 Show documentation
Show all versions of middleware-moshi3 Show documentation
Store3 is built with RxJava2
package com.nytimes.android.external.store3.middleware.moshi;
import com.nytimes.android.external.store3.base.Parser;
import com.nytimes.android.external.store3.util.ParserException;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import java.io.IOException;
import java.lang.reflect.Type;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import io.reactivex.annotations.NonNull;
public class MoshiStringParser implements Parser {
private final JsonAdapter jsonAdapter;
@Inject
public MoshiStringParser(@Nonnull Moshi moshi, @Nonnull Type type) {
jsonAdapter = moshi.adapter(type);
}
@Override
public Parsed apply(@NonNull String s) throws ParserException {
try {
return jsonAdapter.fromJson(s);
} catch (IOException e) {
throw new ParserException(e.getMessage(), e);
}
}
}