public SlackApi(String clientId, String clientSecret, int port) { this.httpClient = new OkHttpClient.Builder() .build(); this.moshi = new Moshi.Builder() .add(new SlackJsonAdapters()) .build(); this.clientId = clientId; this.clientSecret = clientSecret; this.port = port; }
private PendingCommandSerializer() { Moshi moshi = new Moshi.Builder().build(); HashMap<String, JsonAdapter<? extends PendingCommand>> adapters = new HashMap<>(); adapters.put(MessagingControllerCommands.COMMAND_MOVE_OR_COPY, moshi.adapter(PendingMoveOrCopy.class)); adapters.put(MessagingControllerCommands.COMMAND_APPEND, moshi.adapter(PendingAppend.class)); adapters.put(MessagingControllerCommands.COMMAND_EMPTY_TRASH, moshi.adapter(PendingEmptyTrash.class)); adapters.put(MessagingControllerCommands.COMMAND_EXPUNGE, moshi.adapter(PendingExpunge.class)); adapters.put(MessagingControllerCommands.COMMAND_MARK_ALL_AS_READ, moshi.adapter(PendingMarkAllAsRead.class)); adapters.put(MessagingControllerCommands.COMMAND_SET_FLAG, moshi.adapter(PendingSetFlag.class)); this.adapters = Collections.unmodifiableMap(adapters); }
public void run() throws Exception { Moshi moshi = new Moshi.Builder() .add(new SortedSetAdapterFactory()) .build(); JsonAdapter<SortedSet<String>> jsonAdapter = moshi.adapter( Types.newParameterizedType(SortedSet.class, String.class)); TreeSet<String> model = new TreeSet<>(); model.add("a"); model.add("b"); model.add("c"); String json = jsonAdapter.toJson(model); System.out.println(json); }
public void run() throws Exception { String json = "" + "[\n" + " {\n" + " \"rank\": \"4\",\n" + " \"suit\": \"CLUBS\"\n" + " },\n" + " {\n" + " \"rank\": \"A\",\n" + " \"suit\": \"HEARTS\"\n" + " },\n" + " {\n" + " \"rank\": \"J\",\n" + " \"suit\": \"SPADES\"\n" + " }\n" + "]"; Moshi moshi = new Moshi.Builder().build(); Type listOfCardsType = Types.newParameterizedType(List.class, Card.class); JsonAdapter<List<Card>> jsonAdapter = moshi.adapter(listOfCardsType); List<Card> cards = jsonAdapter.fromJson(json); System.out.println(cards); }
public void run() throws Exception { String json = "" + "{\n" + " \"hidden_card\": {\n" + " \"rank\": \"6\",\n" + " \"suit\": \"SPADES\"\n" + " },\n" + " \"visible_cards\": [\n" + " {\n" + " \"rank\": \"4\",\n" + " \"suit\": \"CLUBS\"\n" + " },\n" + " {\n" + " \"rank\": \"A\",\n" + " \"suit\": \"HEARTS\"\n" + " }\n" + " ]\n" + "}\n"; Moshi moshi = new Moshi.Builder().build(); JsonAdapter<BlackjackHand> jsonAdapter = moshi.adapter(BlackjackHand.class); BlackjackHand blackjackHand = jsonAdapter.fromJson(json); System.out.println(blackjackHand); }
public void run() throws Exception { Moshi moshi = new Moshi.Builder() .add(Date.class, new Rfc3339DateJsonAdapter()) .build(); JsonAdapter<Tournament> jsonAdapter = moshi.adapter(Tournament.class); // The RFC3339 JSON adapter can read dates with a timezone offset like '-05:00'. String lastTournament = "" + "{" + " \"location\":\"Chainsaw\"," + " \"name\":\"21 for 21\"," + " \"start\":\"2015-09-01T20:00:00-05:00\"" + "}"; System.out.println("Last tournament: " + jsonAdapter.fromJson(lastTournament)); // The RFC3339 JSON adapter always writes dates with UTC, using a 'Z' suffix. Tournament nextTournament = new Tournament( "Waterloo Classic", "Bauer Kitchen", newDate(2015, 10, 1, 20, -5)); System.out.println("Next tournament JSON: " + jsonAdapter.toJson(nextTournament)); }
public static void main(String[] args) throws IOException { // init class Place place = new Place(); place.setName("World"); Human human = new Human(); human.setMessage("Hi"); human.setPlace(place); // convert to json Moshi moshi = new Moshi.Builder().build(); JsonAdapter<Human> jsonAdapter = moshi.adapter(Human.class); String jsonString = jsonAdapter.toJson(human); System.out.println("json " + jsonString); //print "json {"message":"Hi","place":{"name":"World"}}" // convert from json Human newHuman = jsonAdapter.fromJson(jsonString); newHuman.say(); // print "Hi , World!" }
public void run() throws Exception { // For some reason our JSON has date and time as separate fields. We will clean that up during // parsing: Moshi will first parse the JSON directly to an EventJson and from that the // EventJsonAdapter will create the actual Event. String json = "" + "{\n" + " \"title\": \"Blackjack tournament\",\n" + " \"begin_date\": \"20151010\",\n" + " \"begin_time\": \"17:04\"\n" + "}\n"; Moshi moshi = new Moshi.Builder().add(new EventJsonAdapter()).build(); JsonAdapter<Event> jsonAdapter = moshi.adapter(Event.class); Event event = jsonAdapter.fromJson(json); System.out.println(event); System.out.println(jsonAdapter.toJson(event)); }
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); }
public void run() throws Exception { String json = "" + "{\n" + " \"color\": \"#ff0000\",\n" + " \"height\": 768,\n" + " \"width\": 1024\n" + "}\n"; Moshi moshi = new Moshi.Builder() .add(new ColorAdapter()) .build(); JsonAdapter<Rectangle> jsonAdapter = moshi.adapter(Rectangle.class); Rectangle rectangle = jsonAdapter.fromJson(json); System.out.println(rectangle); }
public static void main(String[] args) throws Exception { String json = "" + "{\"data\":" + " {\n" + " \"rank\": \"4\",\n" + " \"suit\": \"CLUBS\"\n" + " }" + "}"; Moshi moshi = new Moshi.Builder().add(EnvelopeJsonAdapter.FACTORY).build(); JsonAdapter<Card> adapter = moshi.adapter(Card.class, Enveloped.class); Card out = adapter.fromJson(json); System.out.println(out); }
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); }
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); }
server.enqueue(new MockResponse().setBody("{\"name\": \"Gson\"}")); com.squareup.moshi.Moshi moshi = new com.squareup.moshi.Moshi.Builder().build(); com.google.gson.Gson gson = new GsonBuilder().create(); MoshiConverterFactory moshiConverterFactory = MoshiConverterFactory.create(moshi);
public void run() throws Exception { Moshi moshi = new Moshi.Builder() .add(new MultipleFormatsCardAdapter()) .add(new CardStringAdapter()) .build(); JsonAdapter<Card> cardAdapter = moshi.adapter(Card.class); // Decode cards from one format or the other. System.out.println(cardAdapter.fromJson("\"5D\"")); System.out.println(cardAdapter.fromJson("{\"suit\": \"SPADES\", \"rank\": 5}")); // Cards are always encoded as strings. System.out.println(cardAdapter.toJson(new Card('5', Suit.CLUBS))); }
public void run() throws Exception { String json = "\"TW9zaGksIE9saXZlLCBXaGl0ZSBDaGluPw\""; Moshi moshi = new Moshi.Builder() .add(ByteString.class, new Base64ByteStringAdapter()) .build(); JsonAdapter<ByteString> jsonAdapter = moshi.adapter(ByteString.class); ByteString byteString = jsonAdapter.fromJson(json); System.out.println(byteString); }
public void run() throws Exception { // We want to match any Stage that starts with 'in-progress' as Stage.IN_PROGRESS // and leave the rest of the enum values as to match as normal. Moshi moshi = new Moshi.Builder().add(new StageAdapter()).build(); JsonAdapter<Stage> jsonAdapter = moshi.adapter(Stage.class); System.out.println(jsonAdapter.fromJson("\"not-started\"")); System.out.println(jsonAdapter.fromJson("\"in-progress\"")); System.out.println(jsonAdapter.fromJson("\"in-progress-step1\"")); }
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); }
public static void main(String[] args) throws Exception { Moshi moshi = new Moshi.Builder() .add(FallbackEnumJsonAdapter.FACTORY) .build(); JsonAdapter<Example> adapter = moshi.adapter(Example.class); System.out.println(adapter.fromJson("{\"transportation\":\"CARS\"}")); }
/** Create an instance using a default {@link Moshi} instance for conversion. */ public static MoshiConverterFactory create() { return create(new Moshi.Builder().build()); }