Fastjson ToString Filters The specified object properties __js

Source: Internet
Author: User

Recently in making a demand, encountered a problem, in the Java object with Fastjson into a JSON string, you need to filter out the specified attributes in the Java object, through the search data, solve the problem, here to make a summary, the specific implementation can refer to the several ways described in this article. example of a data sample

The Java object structure and the object instances used in this article are as follows: object Structure

public class Persion {

    private String ID;
    private String name;
    private int age;

    Public persion (string ID, string name, int age) {
        this.id = ID;
        this.name = name;
        This.age = age;
    }

    Public String GetId () {return
        ID;
    }

    public void SetId (String id) {
        this.id = ID;
    }

    Public String GetName () {return
        name;
    }

    public void SetName (String name) {
        this.name = name;
    }

    public int getage () {return age
        ;
    }

    public void Setage (int age) {
        this.age = age;
    }

}
Object Instance
Persion persion = new Persion ("10010", "Jason", 101);
the normal JSON string is as follows:
{"Age": "id": "10010", "name": "Jason"}
first, use the transient keyword

Remove the need to filter the attributes using the transient keyword modifier, so that when the JSON method is invoked, the transient-decorated properties do not appear in the final JSON string, for example:

private transient String ID;

When the Json.tostring () method is called again, the id attribute is filtered out and the result is as follows:

{"Age": "Name": "Jason"}
Ii. use of annotations

Or as an example of an id attribute, use the annotation @jsonfield (Serialize=false), as follows:

@JSONField (serialize=false)
private String ID;

Similarly, when the Json.tostring () method is invoked, the id attribute is filtered out, and the result is as follows:

{"Age": "Name": "Jason"}
third, use the attribute filter PropertyFilter

Using the filter propertyfilter, use the following example:

        Create Java object
        persion persion = new Persion ("10010", "Jason",);
        Creates a PropertyFilter object and overloads the Apply method
        PropertyFilter PropertyFilter = new PropertyFilter () {
            Boolean flag = true;
            @Override Public
            Boolean apply (Object obj, String name, object value) {
                if (name.equalsignorecase ("id")) {
                    Flag = false;
                }
                return flag;
            }

        ;
        When the toJSONString method is invoked, the incoming PropertyFilter object
        String jsonstr = json.tojsonstring (persion, propertyfilter);

The resulting JSON string is as follows:

{"Age": "Name": "Jason"}
PropertyFilter Property Filter Description:

(1) When the Apply method returns True, the property is exported to the JSON string, the Java doc for the Apply method is as follows:

    /**
     * @param object The owner of the property
     * @param name The name of the property
     * @param value
     the property * @return True if the property is filtered out, false otherwise *
    /Boolean apply (Object o Bject, String name, Object value);

(2) You can use both property names and property values for filtering, as shown in the following example:

            @Override Public
            Boolean apply (Object obj, String name, object value) {
                if (name.equalsignorecase ("id") & & "10011". Equals (value)) {
                    flag = false;
                } else{
                    flag = true;
                }
                return flag;
            }

The effect of the implementation is to filter only the attributes whose attribute name is ID and the ID value is 10011, and the result is as follows:

{"Age": "id": "10010", "name": "Jason"}

The ID is not filtered out because value is not 10011.

(3) Warning:

If a property makes the flag value false, all subsequent flag will be fasle, causing the property to not output, and in order to avoid such an error, the example in (2) is added else{flag = true;} Code. Iv. using the attribute filter Simplepropertyprefilter

For example, the only properties that appear in the Simplepropertyprefilter constructor argument are exported:

Simplepropertyprefilter simplepropertyprefilter = 
new Simplepropertyprefilter (Persion.class, "name", "Age");

The output JSON results are as follows:

{"Age": "Name": "Jason"}

The property ID was not output because the ID did not pass in the Simplepropertyprefilter constructor parameter. The way to contrast transient keywords and annotations have precedence over the property filter, which will cause the property filter to not work. :
Use both the Transient keyword and the attribute filter, the annotation and the attribute filter, and the most flexible use of the property filter propertyfilter, which can be filtered not only by the attribute name, but also by the value of the attribute.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.