Search in sources :

Example 1 with Moshi

use of com.squareup.moshi.Moshi in project MovieGuide by esoxjem.

the class FavoritesStore method setFavorite.

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();
}
Also used : Movie(com.esoxjem.movieguide.Movie) Moshi(com.squareup.moshi.Moshi) SharedPreferences(android.content.SharedPreferences)

Example 2 with Moshi

use of com.squareup.moshi.Moshi in project moshi by square.

the class RecoverFromTypeMismatch method run.

public void run() throws Exception {
    String json = "[\"DIAMONDS\", \"STARS\", \"HEARTS\"]";
    Moshi moshi = new Moshi.Builder().add(DefaultOnDataMismatchAdapter.newFactory(Suit.class, Suit.CLUBS)).build();
    JsonAdapter<List<Suit>> jsonAdapter = moshi.adapter(Types.newParameterizedType(List.class, Suit.class));
    List<Suit> suits = jsonAdapter.fromJson(json);
    System.out.println(suits);
}
Also used : Moshi(com.squareup.moshi.Moshi) List(java.util.List) Suit(com.squareup.moshi.recipes.models.Suit)

Example 3 with Moshi

use of com.squareup.moshi.Moshi in project moshi by square.

the class WriteJson method run.

public void run() throws Exception {
    BlackjackHand blackjackHand = new BlackjackHand(new Card('6', SPADES), Arrays.asList(new Card('4', CLUBS), new Card('A', HEARTS)));
    Moshi moshi = new Moshi.Builder().build();
    JsonAdapter<BlackjackHand> jsonAdapter = moshi.adapter(BlackjackHand.class);
    String json = jsonAdapter.toJson(blackjackHand);
    System.out.println(json);
}
Also used : Moshi(com.squareup.moshi.Moshi) BlackjackHand(com.squareup.moshi.recipes.models.BlackjackHand) Card(com.squareup.moshi.recipes.models.Card)

Example 4 with Moshi

use of com.squareup.moshi.Moshi in project moshi by square.

the class CustomFieldName method run.

public void run() throws Exception {
    String json = "" + "{" + "  \"username\": \"jesse\"," + "  \"lucky number\": 32" + "}\n";
    Moshi moshi = new Moshi.Builder().build();
    JsonAdapter<Player> jsonAdapter = moshi.adapter(Player.class);
    Player player = jsonAdapter.fromJson(json);
    System.out.println(player);
}
Also used : Player(com.squareup.moshi.recipes.models.Player) Moshi(com.squareup.moshi.Moshi)

Example 5 with Moshi

use of com.squareup.moshi.Moshi in project moshi by square.

the class CustomTypeAdapter method run.

public void run() throws Exception {
    String json = "" + "{\n" + "  \"hidden_card\": \"6S\",\n" + "  \"visible_cards\": [\n" + "    \"4C\",\n" + "    \"AH\"\n" + "  ]\n" + "}\n";
    Moshi moshi = new Moshi.Builder().add(new CardAdapter()).build();
    JsonAdapter<BlackjackHand> jsonAdapter = moshi.adapter(BlackjackHand.class);
    BlackjackHand blackjackHand = jsonAdapter.fromJson(json);
    System.out.println(blackjackHand);
}
Also used : Moshi(com.squareup.moshi.Moshi) BlackjackHand(com.squareup.moshi.recipes.models.BlackjackHand)

Aggregations

Moshi (com.squareup.moshi.Moshi)22 HttpUrl (okhttp3.HttpUrl)6 Request (okhttp3.Request)6 RequestBody (okhttp3.RequestBody)6 Response (okhttp3.Response)6 LoginFailedException (com.pokegoapi.exceptions.request.LoginFailedException)5 IOException (java.io.IOException)5 BlackjackHand (com.squareup.moshi.recipes.models.BlackjackHand)3 Card (com.squareup.moshi.recipes.models.Card)3 ArrayList (java.util.ArrayList)3 Movie (com.esoxjem.movieguide.Movie)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Gson (com.google.gson.Gson)2 InvalidCredentialsException (com.pokegoapi.exceptions.request.InvalidCredentialsException)2 Type (java.lang.reflect.Type)2 List (java.util.List)2 SharedPreferences (android.content.SharedPreferences)1 Response (com.bluelinelabs.logansquare.demo.model.Response)1 GsonParser (com.bluelinelabs.logansquare.demo.parsetasks.GsonParser)1 JacksonDatabindParser (com.bluelinelabs.logansquare.demo.parsetasks.JacksonDatabindParser)1