Skip to content

Commit

Permalink
Replace some TypeToken.getType() usages
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcono1234 committed Sep 18, 2022
1 parent 853d9f5 commit c34ba6c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
4 changes: 2 additions & 2 deletions UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ Collection<Integer> ints = Arrays.asList(1,2,3,4,5);
String json = gson.toJson(ints); // ==> json is [1,2,3,4,5]

// Deserialization
Type collectionType = new TypeToken<Collection<Integer>>(){}.getType();
TypeToken<Collection<Integer>> collectionType = new TypeToken<Collection<Integer>>(){};
Collection<Integer> ints2 = gson.fromJson(json, collectionType);
// ==> ints2 is same as ints
```
Expand Down Expand Up @@ -263,7 +263,7 @@ For deserialization Gson uses the `read` method of the `TypeAdapter` registered

```java
Gson gson = new Gson();
Type mapType = new TypeToken<Map<String, String>>(){}.getType();
TypeToken<Map<String, String>> mapType = new TypeToken<Map<String, String>>(){};
String json = "{\"key\": \"value\"}";

// Deserialization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@
*/
public class CollectionsDeserializationBenchmark {

private static final Type LIST_TYPE = new TypeToken<List<BagOfPrimitives>>(){}.getType();
private static final TypeToken<List<BagOfPrimitives>> LIST_TYPE_TOKEN = new TypeToken<List<BagOfPrimitives>>(){};
private static final Type LIST_TYPE = LIST_TYPE_TOKEN.getType();
private Gson gson;
private String json;

public static void main(String[] args) {
NonUploadingCaliperRunner.run(CollectionsDeserializationBenchmark.class, args);
}

@BeforeExperiment
void setUp() throws Exception {
this.gson = new Gson();
Expand All @@ -51,12 +52,12 @@ void setUp() throws Exception {
this.json = gson.toJson(bags, LIST_TYPE);
}

/**
/**
* Benchmark to measure Gson performance for deserializing an object
*/
public void timeCollectionsDefault(int reps) {
for (int i=0; i<reps; ++i) {
gson.fromJson(json, LIST_TYPE);
gson.fromJson(json, LIST_TYPE_TOKEN);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ private enum Document {
READER_SHORT(new TypeToken<Feed>() {}, new TypeReference<Feed>() {}),
READER_LONG(new TypeToken<Feed>() {}, new TypeReference<Feed>() {});

private final Type gsonType;
private final TypeToken<?> gsonType;
private final TypeReference<?> jacksonType;

private Document(TypeToken<?> typeToken, TypeReference<?> typeReference) {
this.gsonType = typeToken.getType();
this.gsonType = typeToken;
this.jacksonType = typeReference;
}
}
Expand Down

0 comments on commit c34ba6c

Please sign in to comment.