Nothing complicated, just a note.

Question why

When checking an old function, the server found a problem, a time stamp, the server does not necessarily return, asking the mobile terminal how to receive processing. Check mobile reception for Gson library and Kotlin Data Class. If the server does not return this value, the mobile terminal defaults to 0.

Json parsing differences between Java primitive data types and encapsulated types

The answer is as follows: If there is no corresponding value in the JSON data after Gson parses it. Java code. The default int type is 0 and the default Integer type is null. Kotlin code, both ints. Int defaults to 0, Int? The default type is NULL.

The code is as follows: Content to be parsed

{
    "username": "Android"
}
Copy the code

Parsing is the basic way to use Gson. Do not add features like type adaptation.

new Gson().fromJson("", xx.class);
Copy the code

Java class 1

public class User {
    public String username;
    public intage; } username->Android, age->0
Copy the code

Java class 2

public class User {
    public String username;
    publicInteger age; } username->Android, age->null
Copy the code

kotlin data class 1

data class User(val username:String,val age:IntUsername ->Android, age->0
Copy the code

kotlin data class 2

data class User(val username:String,val age:Int?). Username ->Android, age->null
Copy the code

When does it work?

Where a value of an underlying type is used, and you need to determine whether it exists, you can’t use a method with a default value.