Java Code Examples for com.squareup.moshi.Types#newParameterizedType()

The following examples show how to use com.squareup.moshi.Types#newParameterizedType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReadJsonList.java    From moshi with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 2
Source File: MoshiMessageBodyWriterTest.java    From jax-rs-moshi with Apache License 2.0 5 votes vote down vote up
@Test public void writesParameterized() throws IOException {
  Buffer data = new Buffer();
  MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
  Type genericType = Types.newParameterizedType(List.class, String.class);
  writer.writeTo(singletonList("hey"), List.class, genericType, new Annotation[0],
      APPLICATION_JSON_TYPE, headers, data.outputStream());
  assertEquals("[\"hey\"]", data.readUtf8());
  assertTrue(headers.isEmpty());
}
 
Example 3
Source File: MoshiMessageBodyReaderTest.java    From jax-rs-moshi with Apache License 2.0 5 votes vote down vote up
@Test public void readsParameterized() throws IOException {
  Buffer data = new Buffer().writeUtf8("[\"hey\"]");
  Class<Object> type = (Class) List.class;
  Type genericType = Types.newParameterizedType(List.class, String.class);
  MultivaluedMap<String, String> headers = new MultivaluedHashMap<>();
  Object result = reader.readFrom(type, genericType, new Annotation[0], APPLICATION_JSON_TYPE,
      headers, data.inputStream());
  assertEquals(singletonList("hey"), result);
  assertTrue(headers.isEmpty());
}
 
Example 4
Source File: MoshiParamConverterFactoryTest.java    From jax-rs-moshi with Apache License 2.0 5 votes vote down vote up
@Test public void jsonAnnotationReturnsConverterParameterized() {
  Type genericType = Types.newParameterizedType(List.class, String.class);
  ParamConverter<List<String>> converter =
      (ParamConverter) provider.getConverter(List.class, genericType, new Annotation[] {
          Annotations.real()
      });
  List<String> value = converter.fromString("[\"hey\"]");
  assertEquals(singletonList("hey"), value);
  String json = converter.toString(singletonList("hey"));
  assertEquals("[\"hey\"]", json);
}
 
Example 5
Source File: ArrayUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenSerializingList_thenJsonArrayProduced() {
    Moshi moshi = new Moshi.Builder()
      .build();
    Type type = Types.newParameterizedType(List.class, String.class);
    JsonAdapter<List<String>> jsonAdapter = moshi.adapter(type);

    String json = jsonAdapter.toJson(Arrays.asList("One", "Two", "Three"));
    System.out.println(json);
}
 
Example 6
Source File: ArrayUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenDeserializingJsonArray_thenListProduced() throws IOException {
    Moshi moshi = new Moshi.Builder()
      .build();
    Type type = Types.newParameterizedType(List.class, String.class);
    JsonAdapter<List<String>> jsonAdapter = moshi.adapter(type);

    String json = "[\"One\",\"Two\",\"Three\"]";
    List<String> result = jsonAdapter.fromJson(json);
    System.out.println(result);
}
 
Example 7
Source File: FlickrDiskRepository.java    From chaining-rxjava with Apache License 2.0 4 votes vote down vote up
public FlickrDiskRepository(Context context) {
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    Moshi moshi = new Moshi.Builder().build();
    Type adapterType = Types.newParameterizedType(Timestamped.class, RecentPhotosResponse.class);
    flickrPhotosJsonAdapter = moshi.adapter(adapterType);
}
 
Example 8
Source File: ElementAtJsonAdapter.java    From moshi-lazy-adapters with Apache License 2.0 4 votes vote down vote up
ElementAtJsonAdapter(Type type, Moshi moshi, int index) {
  Type listType = Types.newParameterizedType(List.class, type);
  delegate = moshi.adapter(listType);
  this.index = index;
}
 
Example 9
Source File: LastElementJsonAdapter.java    From moshi-lazy-adapters with Apache License 2.0 4 votes vote down vote up
LastElementJsonAdapter(Type type, Moshi moshi) {
  Type listType = Types.newParameterizedType(List.class, type);
  delegate = moshi.adapter(listType);
}
 
Example 10
Source File: MoshiSpeaker.java    From Jolyglot with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override public ParameterizedType newParameterizedType(Type rawType,
    Type... typeArguments) {
  return Types.newParameterizedType(rawType, typeArguments);
}