PackageDemo4;Importjava.io.Serializable;Importnet.sf.json.JSONString; Public classUserImplementsjsonstring,serializable{/** * */ Private Static Final LongSerialversionuid = 1L; Private LongID; PrivateString name; PrivateString password; Public LonggetId () {returnID; } Public voidSetId (LongID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; } PublicUser (LongID, string name, string password) { Super(); This. ID =ID; This. Name =name; This. Password =password; } PublicUser () {Super(); } PublicString tojsonstring () {return"{\" id\ ":" + This. id+ ", \" name\ ": \" "+ This. name+ "\"} "; } }User.java
PackageDemo4; Public classTeacher {Private intID; PrivateString name; PrivateString password; Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; } PublicTeacher (intID, string name, string password) { Super(); This. ID =ID; This. Name =name; This. Password =password; } PublicTeacher () {Super(); } }Teacher.java
PackageDemo4;ImportJava.text.SimpleDateFormat;Importjava.util.Arrays;Importjava.util.Date;ImportJava.util.HashMap;ImportJava.util.Map;ImportNet.sf.json.JSONObject;ImportNet.sf.json.JSONSerializer;ImportNet.sf.json.JsonConfig;ImportNet.sf.json.processors.JsonBeanProcessor;ImportNet.sf.json.processors.JsonValueProcessor;ImportNet.sf.json.util.PropertyFilter;Importorg.junit.Test; Public classDemo {/* public void Registerjsonbeanprocessor (Class target, Jsonbeanprocessor jsonbe Anprocessor) registers a jsonbeanprocessor. [Java-JSON] Parameters:target-the class to use as key Jsonbeanprocessor-the processor to register publ IC void Registerpropertyexclusion (class target, * Register non-convertible properties in class * String PropertyName ) Registers a exclusion for a target class. [Java-JSON] Parameters:target-the class to use as key Propertyname-the property to is excluded public void s Etexcludes (string[] excludes) sets the properties that are not converted sets the excludes to use. Would set default value ([]) if null. [Java-JSON] PropertyFilter: Attribute Filter One method: Apply Boolean apply (Object source, String name, Ob Ject value) parameters:source-the owner of the property Name-the name of the property value-the value of the Returns:true if the property wi ll be filtered out, false otherwise*/ /*** Ignore unnecessary attributes override this object to specify toJSONString method test*/@Test Public voidFun () {User User=NewUser (12, "learned", "gz1234"); Jsonobject Jo=jsonobject.fromobject (user); System.out.println (Jo); } /*** Ignore unnecessary attributes and use jsonconfig implementation * to easily add or remove attributes that contain and need to be excluded through Jsonconfig instances*/@Test Public voidfun1 () {Teacher T=NewTeacher ("Guodaxia", "gz1234"); Jsonconfig Config=NewJsonconfig (); Config.setexcludes (Newstring[]{"Password"});//setting the Exclude Password propertyJsonobject jo=jsonobject.fromobject (T, config); System.out.println (Jo); } /*** Test using attribute filters to achieve previous effects * Using PropertyFilter allows you to control both properties and classes that need to be excluded, which can also be bidirectional or applied to JSON strings to Java objects*/@Test Public voidfun2 () {Teacher T=NewTeacher ("Guodaxia", "gz1234"); Jsonconfig Config=NewJsonconfig (); Config.setjsonpropertyfilter (NewPropertyFilter () { Public Booleanapply (object source, String PropertyName, Object value) {/*** This filters out the password attribute in the teacher class*///return source instanceof Teacher && "password". Equalsignorecase (PropertyName); return"Password". Equalsignorecase (PropertyName);//This is testing it can be bidirectional filtered } }); Jsonobject Jo=jsonobject.fromobject (T, config); System.out.println (Jo); Jsonobject Jo1= (jsonobject) Jsonserializer.tojson ("{' id ':", ' name ': ' gz ', ' Password ': ' a12345 '} ", config);//here, the JSON object obtained using Jsonserializer is valid for conversion to teacher object, Jsonobject.fromobject No, I don't know whyTeacher tt= (Teacher) Jo1.tobean (Jo1, Teacher.class ); System.out.println (Tt.getid ()+ "--" +tt.getname () + "--" +Tt.getpassword ()); //jsonobject Jo1=jsonobject.fromobject ("{' id ':", ' name ': ' gz ', ' Password ': ' a12345 '} ", config);//Object tt= Jsonobject.tobean (jo1);//System.out.println (TT); } /*** Use Registerpropertyexclusion to achieve the previous effect*/@Test Public voidFun3 () {Teacher T=NewTeacher ("Guodaxia", "gz1234"); Jsonconfig Config=NewJsonconfig (); Config.registerpropertyexclusion (Teacher.class, "Password"); Jsonobject Jo=jsonobject.fromobject (T, config); System.out.println (Jo); } /*** Test Using custom Jsonbeanprocessor * Jsonbeanprocessor is similar to implementing jsonstring, returning a legitimate jsonobject representing the original target object **/@Test Public voidFun4 () {jsonconfig config=NewJsonconfig (); Config.registerjsonbeanprocessor (Teacher.class,Newjsonbeanprocessor () { Publicjsonobject Processbean (Object bean, jsonconfig config) {Teacher tea=(Teacher) bean; return NewJsonobject (). Element ("id", Tea.getid ()). Element ("name", Tea.getname ()); } }); Teacher T=NewTeacher ("JSON", "JSON")); System.out.println (Jsonobject.fromobject (t,config)); } /*** Custom Jsonvalueprocessor * For example, we want to control the formatting of the Date object during JSON serialization and the formatting of the numeric value, Jsonvalueprocessor is the best choice * This method can be used to process data, Type of operation and so on*/@Test Public voidfun5 () {Map<String,Object> map=NewHashmap<string,object>(); Map.put ("Date",NewDate ()); Map.put ("Dates", Arrays.aslist (NewDate ())); Jsonconfig Config=NewJsonconfig (); Config.registerjsonvalueprocessor (Date.class,Newjsonvalueprocessor () {//custom Date processing formatsSimpleDateFormat sdf=NewSimpleDateFormat ("Yyyy-mm-dd"); /*** Processing a single Date object*/ PublicObject Processobjectvalue (String PropertyName, Object date, Jsonconfig config) {returnSdf.format (date); } PublicObject Processarrayvalue (object date, Jsonconfig config) {returnSdf.format (date); } }); SYSTEM.OUT.PRINTLN (jsonobject.fromobject (map, config)); } }
Some filtering operations of Json-lib