I don’t know if you noticed, but no matter how much you test your finished program at the time, it’s going to Bug over time. Here’s another example that happens every day: when you check a blog post immediately after you’ve written it, you can’t find your typos.

It’s said that there’s some logic to how the brain processes things in the moment, and there’s some psychology involved, which I don’t understand. Anyway, now it’s a Bug! Bug!! Bug!!!!!!

The thing is, when the client requests the server to open the interface, we return it as a JSON string, which would normally look like this:

{"name": "mafly", "age": "18", "sex": "male"}Copy the code

But this guy named Mafly he doesn’t want you to know if he’s a man or a woman. The default value is null, and something unexpected happens when you serialize JSON.

Reproduce that Bug

There are many tool classes for converting Java objects to Json strings and converting Json strings to objects, such as Gson, Jackson, FastJson, etc. We use Gson, which was developed by Google and is rumored to be more powerful. Yeah, that’s what we use. Take a look at the sample code:

public static void main(String[] args) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("name", "mafly"); map.put("age", "18"); map.put("sex", null); String jsonString = new Gson().toJson(map); System.err.println(jsonString); }Copy the code

Do you guess it will output the expected Json string above? But it doesn’t.

When Sex=null, Gson’s toJSON method ignores the null value and outputs a Json string that does not contain the value of the property. {“sex”: “”} or {“sex”: null}.

Through search engines, there are probably three solutions on the Internet:

  • Call the toJson(Object SRC, Type typeOfSrc, JsonWriter writer) method
  • Register a custom TypeAdapter
  • Set the serializeNulls property value (recommended)

The first two ways I did not test, recommend the third way, simple and convenient, the source code is as follows:

GsonBuilder gsonBuilder = new GsonBuilder();
String jsonString1 = gsonBuilder.serializeNulls().create().toJson(map);
System.err.println(jsonString1);
Copy the code

{“sex”: null},

Simple to look at the source code, in addition to this one setting there are many, it is really too convenient. After calling serializeNulls(), Gson sets the serializeNulls property to true. The default value is false.

public GsonBuilder serializeNulls() {
   this.serializeNulls = true;
   return this;
}
Copy the code

Reintroduce Gson

After looking through a few lines of Gson’s source code, I realized that it is configurable and has numerous features. You can rename properties with the @serializedName annotation, use the @expose annotation to indicate that properties are not serialized, support Map keys as complex objects, and convert date types to specific formats. There are also separate versions of the display, which are very common in everyday projects, but we just seem to have implemented them ourselves.

I simply created a UserInfo entity class that contains username, age, and sex. Tried one or two of them out:

1.@Expose who wants to export,

Set the excludeFieldsWithoutExposeAnnotation (),

2.@SerializedName display whatever you want,

I want the age to show maflyAge when I serialize the Json, and that’s it now

3.@Since, @Until Different versions have different data.

SetVersion (3.2) is set here, so @since will only be printed in version 3.2 or later and @until will only be printed before version 3.2.

Of course, there are a lot of features, such as field capitalization, result formatting, etc., I throw a brick here, the rest of the need to try.

To summarize

This is due to the small bugs found after a Json serialization output, could not be a Bug, because it is our tools for their own use classes do not understand, so, to try to get to know you use any of the open source tools or project, for the problems can be well solved, which can also improve yourself.