All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nytimes.android.external.store3.middleware.moshi.MoshiStringParser Maven / Gradle / Ivy

There is a newer version: 3.1.1
Show newest version
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);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy