Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. [
  2. {
  3. "__typename": "Car",
  4. "id": "123",
  5. "name": "Toyota Prius",
  6. "numDoors": 4
  7. },
  8. {
  9. "__typename": "Boat",
  10. "id": "4567",
  11. "name": "U.S.S. Constitution",
  12. "propulsion": "SAIL"
  13. }
  14. ]
  15.  
  16. public abstract class Vehicle {
  17. public final String id;
  18. public final String name;
  19. }
  20.  
  21. public class Car extends Vehicle {
  22. public final Integer numDoors;
  23. }
  24.  
  25. public class Boat extends Vehicle {
  26. public final String propulsion;
  27. }
  28.  
  29. public class SemiParsedKindOfVehicle {
  30. public final String id;
  31. public final String name;
  32. public final Integer numDoors;
  33. public final String propulsion;
  34. public final String __typename;
  35. }
  36.  
  37. import com.squareup.moshi.FromJson;
  38. import com.squareup.moshi.JsonAdapter;
  39. import com.squareup.moshi.Moshi;
  40. import com.squareup.moshi.ToJson;
  41. import com.squareup.moshi.Types;
  42. import org.junit.Assert;
  43. import org.junit.Test;
  44. import java.io.IOException;
  45. import java.lang.reflect.Type;
  46. import java.util.List;
  47. import java.util.Map;
  48. import static org.junit.Assert.assertEquals;
  49.  
  50. public class Foo {
  51. static abstract class Vehicle {
  52. public String id;
  53. public String name;
  54. }
  55.  
  56. static class Car extends Vehicle {
  57. public Integer numDoors;
  58. }
  59.  
  60. static class Boat extends Vehicle {
  61. public String propulsion;
  62. }
  63.  
  64. static class VehicleAdapter {
  65. @FromJson
  66. Vehicle fromJson(Map<String, Object> raw) {
  67. String typename=raw.get("__typename").toString();
  68. Vehicle result;
  69.  
  70. if (typename.equals("Car")) {
  71. Car car=new Car();
  72.  
  73. car.numDoors=((Double)raw.get("numDoors")).intValue();
  74. result=car;
  75. }
  76. else if (typename.equals("Boat")) {
  77. Boat boat=new Boat();
  78.  
  79. boat.propulsion=raw.get("propulsion").toString();
  80. result=boat;
  81. }
  82. else {
  83. throw new IllegalStateException("Could not identify __typename: "+typename);
  84. }
  85.  
  86. result.id=raw.get("id").toString();
  87. result.name=raw.get("name").toString();
  88.  
  89. return(result);
  90. }
  91.  
  92. @ToJson
  93. String toJson(Vehicle vehicle) {
  94. throw new UnsupportedOperationException("Um, why is this required?");
  95. }
  96. }
  97.  
  98. static final String JSON="[n"+
  99. " {n"+
  100. " "__typename": "Car",n"+
  101. " "id": "123",n"+
  102. " "name": "Toyota Prius",n"+
  103. " "numDoors": 4n"+
  104. " },n"+
  105. " {n"+
  106. " "__typename": "Boat",n"+
  107. " "id": "4567",n"+
  108. " "name": "U.S.S. Constitution",n"+
  109. " "propulsion": "SAIL"n"+
  110. " }n"+
  111. "]";
  112.  
  113. @Test
  114. public void deserializeGeneric() throws IOException {
  115. Moshi moshi=new Moshi.Builder().add(new VehicleAdapter()).build();
  116. Type payloadType=Types.newParameterizedType(List.class, Vehicle.class);
  117. JsonAdapter<List<Vehicle>> jsonAdapter=moshi.adapter(payloadType);
  118. List<Vehicle> result=jsonAdapter.fromJson(JSON);
  119.  
  120. assertEquals(2, result.size());
  121.  
  122. assertEquals(Car.class, result.get(0).getClass());
  123.  
  124. Car car=(Car)result.get(0);
  125.  
  126. assertEquals("123", car.id);
  127. assertEquals("Toyota Prius", car.name);
  128. assertEquals((long)4, (long)car.numDoors);
  129.  
  130. assertEquals(Boat.class, result.get(1).getClass());
  131.  
  132. Boat boat=(Boat)result.get(1);
  133.  
  134. assertEquals("4567", boat.id);
  135. assertEquals("U.S.S. Constitution", boat.name);
  136. assertEquals("SAIL", boat.propulsion);
  137. }
  138. }
  139.  
  140. private val moshi = Moshi.Builder()
  141. .add(
  142. PolymorphicJsonAdapterFactory.of(Vehicle::class.java, "__typename")
  143. .withSubtype(Car::class.java, "Car")
  144. .withSubtype(Boat::class.java, "Boat")
  145. )
  146. .build()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement