JSON serialization of Datasupport objects in Litepal __js

Source: Internet
Author: User
Tags serialization tojson
up
Litepal is what, but Baidu.
Datasupport is Litepal's most basic database object, similar to JPA's repository. The
problem is: When the Datasupport subclass is serialized with Gson, datasupport its own attributes are excluded.
Underwriting
You do not want to define another Dto object, but a subclass that uses datasupport directly will be affected by this problem.
then use the Gson gsonbuilder to create a custom filtering policy, and then remove the corresponding attributes.
public class Mylitepalgson extends Mygson {public Mylitepalgson () {super (Customizegson ()); } private static string[] Litepalexcludes = {"Baseobjid", "ASSOCIATEDMODELSMAPWITHFK", "associatedmodels MAPWITHOUTFK "," associatedmodelsmapforjointable "," Listtoclearselffk "," LISTTOCLEARASSOCIATEDFK "," FieldsToSet
    Todefault "}; private static Gson Customizegson () {return new Gsonbuilder (). Addserializationexclusionstrategy (New Exclusionstra  Tegy () {@Override public boolean Shouldskipfield (FieldAttributes f) {for (String
                    Exclude:litepalexcludes) {if (Exclude.equals (F.getname ())) {return true;
            return false;
            @Override public boolean Shouldskipclass (class<?> clazz) {return false;
    ). Create (); }} public class myGson {private Gson Gson;
    Public Mygson () {Gson = new Gson ();
    Public Mygson (Gson gson) {This.gson = Gson; Public <T> T Fromjson (String json, Type typeoft) {try {return Gson.fromjson (JSON, TYPEO
        FT);
            catch (Jsonsyntaxexception e) {e.printstacktrace ();
        return null; } public <T> T Fromjson (jsonelement json, class<t> classoft) {try {return GSO
        N.fromjson (JSON, classoft);
            catch (Jsonsyntaxexception e) {e.printstacktrace ();
        return null;
    } public String Tojson (Object src) {return Gson.tojson (SRC); }

}
Knot

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.