Some questions about fastjson's @JSONField annotation of explained in detail

  • 2020-06-12 08:55:50
  • OfStack

@JSONField

Look at the source code and it can be applied to fields and methods.

To quote from the Internet,

1. Field role

When @ES11en works on Field, its name defines the name of the output as well as the name of the input.

But in my use, I find that is not the case.

For example,


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

It is found that bean does not rotate to json as "project_id":xxx, and json does not rotate to bean as "project_id":xx.

The fastjson version is 1.1.15

2. Effect on setter and getter methods This approach is expected in the process of being used.


/**bean  turn json  When the bean In the ProjectID convert project_id */
  @JSONField(name="project_id")
  public Long getProjectID() {
    return ProjectID;
  }


/**json  turn bean  When the json In the project_id The value assigned to projectID*/
  @JSONField(name="project_id")
  public void setProjectID(Long projectID) {
    ProjectID = projectID;
  }

3. @JSONField other usage, view @ES45en annotation source code, in addition to name available, there are format, serialize, deserialize, serialzeFeatures, parseFeatures available,

The & # 8226; format, it seems useful to use fields of type Date to format the time format.

The & # 8226; serialize and deserialize are booleans


@JSONField(serialize=false) 
private Long ProjectID

It is not included at serialization time. deserialize is the opposite. One thing to note, though, is that I've looked elsewhere to say that annotations don't work on fields with final and should be on get

Or set.

The & # 8226; serialzeFeatures, I'm using this property, the default serialization rule for fastjson is when your field is null, it doesn't serialize this field for you, so I have a requirement like,


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

1 object serialized like this, 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 to the following result


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

fastjson, of course, allows you to control the serialization rules for 1.

So this is SerializerFeature, this 1 enumeration, which has a number of values in it, and if you're interested in what that means,

I only used one of them,


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

This will serialize value even when its value is null. That's what it looks like down here, and that's what I want


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

Another problem is encountered when the field type is int, such as


private int start;
private int limit;
  

If I do not have the set value, It will be serialized as follows


"limit":0,"start":0

The default is zero, and my goal is that if I don't set the values, they don't show up.

I simply changed their type to Integer. There should be other ways to solve this problem by customizing serialization behavior.


Related articles: