內建型別介面卡

Moshi 內建支援讀取和編寫 Java 的核心資料型別:

  • 基元(int,float,char …)和它們的盒裝對應物(整數,浮點數,字元……)。
  • 陣列
  • 集合
  • 清單
  • 地圖字串列舉

它通過逐個欄位寫出來支援你的模型類。在上面的例子中,Moshi 使用這些類:

class BlackjackHand {
  public final Card hidden_card;
  public final List<Card> visible_cards;
  ...
}

class Card {
  public final char rank;
  public final Suit suit;
  ...
}

enum Suit {
  CLUBS, DIAMONDS, HEARTS, SPADES;
}
to read and write this JSON:

{
  "hidden_card": {
    "rank": "6",
    "suit": "SPADES"
  },
  "visible_cards": [
    {
      "rank": "4",
      "suit": "CLUBS"
    },
    {
      "rank": "A",
      "suit": "HEARTS"
    }
  ]
}