This is the first day of my participation in the Gwen Challenge.More article challenges

Miaojun town building, xiaobian for the first time in nuggets to write an article, the writing is not good you big guys

Business scenarios:

Upstream services convert business objects into JSON and push them to downstream services for consumption through MQ messages;

Problems arise:

ToJSONString = null; toJSONString = null; toJSONString = null; toJSONString = null When the downstream service consumes the JSON transform object after receiving the message to MQ, it finds that some fields are missing, causing the JSON transform exception

net.sf.json.JSONException:java.lang.reflect.InvocationTargetException

The specific business code will not stick to you to write a small demo list:

@Test public void test(){ user user = new user(); User. Elegantly-named setName (" zhang "); User.setschool (" wuhan University "); String jsonString = JSON.toJSONString(user); System.out.println(" object - "JSON "+jsonString); } @Data class user{ private String name; private String age; private String school; }Copy the code

As shown above, when the age field in the User object is empty, it is converted to JSON

We can see that the age field is missing after converting to JSON, which may cause JSON conversion exceptions when we cannot receive or convert the field to an object, or we sometimes have to display the field when the field is empty or give a default value;

Solution:

Json.tojsonstring ()

As shown above, toJSONString() has multiple overloaded methods. Here we need to configure the SerializerFeature property for toJSONString

The SerializerFeature attribute has the following enumerated classes:

value instructions
QuoteFieldNames Whether to use double quotation marks when outputting keys. The default value is true
WriteMapNullValue Whether to output fields with a value of null. The default value is false
WriteNullNumberAsZero If a numeric field is null, the output is 0 instead of NULL
WriteNullListAsEmpty If the List field is null, the output is [] instead of NULL
WriteNullStringAsEmpty If a character type field is null, the output is “”, not NULL
WriteNullBooleanAsFalse If a Boolean field is null, the output is false instead of null

After modifying the code:

@Test public void test(){ user user = new user(); User. Elegantly-named setName (" zhang "); User.setschool (" wuhan University "); String jsonString = JSON.toJSONString(user, SerializerFeature.WriteNullStringAsEmpty); System.out.println(" object - "JSON "+jsonString); } @Data class user{ private String name; private String age; private String school; }}Copy the code

The output is:

Ok! The toJSONString method has many extensions. For example, if we want to output a particular character when it is null, we need to use filters. Take a look at the other methods of json.tojsonString

JSON. ToJSONString has a number of filters which are not specific clustering. If you are interested, you can take a look at the source code.

@Test public void test(){ user user = new user(); User. Elegantly-named setName (" zhang "); User.setschool (" wuhan University "); String jsonString = JSON.toJSONString(user, SerializerFeature.WriteNullStringAsEmpty); System.out.println(JSON.toJSONString(user, (ValueFilter) (name, age, school) -> { if(age == null){ return "18"; } return age; })); System.out.println(" object - "JSON "+jsonString); } @Data class user{ private String name; private String age; private String school; }Copy the code

That’s OK!

You think that’s the end of it? Avkash Noot!!

Of course, another powerful feature is PropertyFilter. For example, if some values are null and need to be filtered, and some values do not need to be filtered, the Filter will come

@Test public void test(){ user user = new user(); User. Elegantly-named setName (" zhang "); User.setschool (" wuhan University "); String jsonString = JSON.toJSONString(user, SerializerFeature.WriteNullStringAsEmpty); System.out.println(JSON.toJSONString(user, (ValueFilter) (name, age, school) -> { if(age == null){ return "18"; } return age; })); System.out.println(JSON.toJSONString(user, new PropertyFilter() { @Override public boolean apply(Object object, String name, Object value) {return name. Equals (" zhang ") | | the value! = null; } }, SerializerFeature.WriteMapNullValue)); } system.out. println(" object - "JSON "+jsonString); } @Data class user{ private String name; private String age; private String school; }Copy the code

Ok! This is the end of the article, I hope it can be helpful to you, the first writing style is not good, xiaobian will slowly improve, there are wrong places I hope you can put forward, grow together;

Neat makes for great code, and there’s only so much detail