English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Some questions about the @JSONField annotation of fastjson (detailed explanation)

@JSONField

Looking at the source code, it can act on fields and methods.

As mentioned online,

One, Function

When @JSONField is applied to Field, its name not only defines the input key name, but also defines the output name.

But in my use, I found that it is not as described above.

For example

@JSONField(name="project_id")
private Long ProjectID

I found that when bean is converted to json, it is not in the form of "project_id":xxx, and when json is converted to bean, the content of "project_id":xx will not be set to ProjectID inside.

The version of fastjson is1.1.15

2. Applied to setter and getter methods This method is actually expected during use.

/**When bean is converted to json, ProjectID in bean will be converted to project_id */
  @JSONField(name="project_id")
  public Long getProjectID() {
    return ProjectID;
  }
/**When json is converted to bean, the value of project_id in json will be assigned to projectID*/
  @JSONField(name="project_id")
  public void setProjectID(Long projectID) {
    ProjectID = projectID;
  }

3. Other usages of @JSONField, check the source code of @JSONField annotation, in addition to name, format, serialize, deserialize, serialzeFeatures, parseFeatures are also available,

•format,It seems to be useful for formatting date formats in fields of Date type.

•serializeand deserialize are boolean types, the usage is

@JSONField(serialize=false) 
private Long ProjectID

It is not included in the serialization when serializing. The opposite is deserialize. However, there is something to note, I see other places say that when the field is final, the annotation placed on the field does not take effect, and it should be placed on the get

or on the set method.

•serialzeFeaturesI use this property, and fastjson's default serialization rule is that it will not serialize this field when the field value is null, for example, I have such a requirement

{"fieldName":"project_id","operator":"is not","value":null}

An object is serialized in this way, and my code is as follows

CriteriaVO criteriaVO = new CriteriaVO();
    criteriaVO.setFieldName("project_id");
    criteriaVO.setOperator("is not");
    criteriaVO.setValue(null);

By default, it will only serialize the following result

{"fieldName":"project_id","operator":"is not"}

Of course, fastjson still allows you to control the serialization rules.

This uses SerializerFeature, which is an enumeration with several values, the specific meanings of which everyone can understand if they are interested,

I only used one of them,

@JSONField(serialzeFeatures=SerializerFeature.WriteMapNullValue)
private String value;

In this way, when the value of value is null, it will still serialize its value. That is, the following is the result I want,

{"fieldName":"project_id","operator":"is not","value":null} 

I encountered another problem, when the field type is int type, such as

private int start;
private int limit;

If I don't set the value, it will be serialized as follows

"limit":0,"start":0

The default is all 0, and my goal is that they will not appear when no value is set.

I simply changed their type to Integer. There should be other ways to solve it by customizing the serialization behavior, but it is not studied for the time being.

This article about some questions (detailed explanation) on the @JSONField annotation of fastjson is all the content that the editor shares with everyone. I hope it can be a reference for everyone and I hope everyone will support the呐喊 tutorial more.

You May Also Like