If jsonschema2pojo had not been used, the number of Gson-related issues on SO would have been reduced by half. There are reasons for this:Automatic generators are very naively mapping without taking into account the dynamic data structures of the type. List♪ Map etc.;They're not very good friends with the jeans.They often do not even make a minimum " operating " analysis to determine that a field may simply have different types (i.e. polymorphnes may be the consequence of the previous claim);I'm not sure about that service, but at least default settings generate a lot of noise.In addition,son does not exist. JSON*** - The type of "JSON" in the name. It's from org.json, while Gson always -- Json***♪ Now, if you're going to use the mappings, you can use them in almost every case, and you can't mix them with the creatures that represent the JSON objects in the form of ancient structures (i.e., all the family. JsonElement) In addition: for objects with dynamic names in Gson there is family support Map<K,V> (It is also true for Java, however, as the number of names is not known in advance at the compilation stage).By summing up the above, you just need to write the right mappings.final class Response<T> {
final String status = null;
final Meta meta = null;
final T data = null;
}
final class Meta {
final int count = Integer.valueOf(0);
}
final class Leader {
@SerializedName("leader_id")
final int leaderId = Integer.valueOf(0);
}
The difference is that the service generates:Improved support for polymorphism (initial)Response<T>);There are no assumptions about sewn names;Almost there. @SerializedName and no. @Expose On the one hand, it's controversy and contradicts "the obvious one that's better off than the other, less noise;It's a primitive data bags, and there's no accessories.set***/get***() -- Facilities designed exclusively for the transfer of data between components of the system (DTO) and which are to be used exclusively by the accompanying components may detract from this requirement of good tone, as there is nothing to be encapsulated;The example of the JSON document is similar to the answer from the service, so the mapping fields can be removed from changes by making them final - No change. final- in the course of performance; exclusion from such "pattern": primitive types as int type default values may not be assigned = 0'Cause the compilator is just inline, and Gson won't be able to change the field (in fact, the places that this station is inline) - Integer.valueOf(0) It's kind of a hack to blind the compilator.Example of use:private static final Gson gson = new Gson();
private static final Type leaderMapResponseType = new TypeToken<Response<Map<Integer, Leader>>>() {
}.getType();
public static void main(final String... args)
throws IOException {
try ( final Reader reader = getPackageResourceReader(Q660527.class, "response.json") ) {
final Response<Map<Integer, Leader>> response = gson.fromJson(reader, leaderMapResponseType);
System.out.println(response.status);
System.out.println(response.meta.count);
for ( final Entry<Integer, Leader> e : response.data.entrySet() ) {
System.out.println(e.getKey() + " => " + e.getValue().leaderId);
}
}
}
Installations Gson and Type (at least those generated using TypeToken<T>(a) May be considered immutable and insecure, so they can be wrapped without re-establishing copies with the same characteristics. A few words about what "type token" is. In Java, due to the specificity of the implementation, there is no possibility of writing Response<Map<String,Leader>>.classbecause Class<T> No information relevant Parameterization except for the parameterization of the parent class in inheritance. Maybe it would be very cool if Java could create an ad-hoc implementation. Type/ParameterizedType Like, Response<Map<String,Leader>>.typebut alas. How does Gson solve this problem? As I said above, the parameterization remains in succession, so Gson requires at least anonymous implementation of the class. TypeToken<T>♪ When creating a copy TypeToken<T> Gson has sufficient information on how parameterized Response<T>and his call. getType() returns the information already analysed parameterized typeNot just class. It's equivalent to manual implementation ParameterizedTypebut with the previous mechanism, the recording is slightly shorter and more beautiful. And yes, "type token"-- you don't need to if, for example, parameterization information is already available, for example, from a kind of field in a classroom (i.e., normal.) private final Map<String, Float> map; already contains sufficient type information (grade still -- simple Map(c)).Outcome:ok1179746 = sector 3239400