The null attribute is stripped when the object is converted to a JSON string, and the use of the enumeration class

Source: Internet
Author: User

Import Java.lang.reflect.Field;

Import Java.util.Date;

Import Java.util.Map;

Import Org.hibernate.Hibernate;

Import abc.dao.Student;

Import Com.alibaba.fastjson.serializer.JSONSerializer;

Import Com.alibaba.fastjson.serializer.PropertyPreFilter;

Import Com.alibaba.fastjson.serializer.SerializeWriter;

Import Com.alibaba.fastjson.serializer.SerializerFeature;

public class Jsonutils {

public static String toJSONString (Object object, Boolean Filternull, serializerfeature[] features) {

Serializewriter out = new Serializewriter ();

Jsonserializer serializer = new Jsonserializer (out);

for (int i = 0; i < features.length; i++) {

Serializerfeature feature = Features[i];

Serializer.config (feature, true);

}

Serializer.setdateformat ("Yyyy-mm-dd HH:mm:ss");

Serializer.config (Serializerfeature.writedateusedateformat, true);

Determining whether to serialize according to PropertyName

Serializer.getpropertyprefilters (). Add (New Propertyprefilter () {

public Boolean apply (Jsonserializer serializer, Object object, String name) {

try {

if ((Object instanceof Map) | | ((Object instanceof iterable))) {

return true;

}

Field f = Object.getclass (). Getdeclaredfield (name);

if (!f.isaccessible ()) {

F.setaccessible (TRUE);

}

Object val = F.get (object);

Return Hibernate.isinitialized (Val);

} catch (Nosuchfieldexception localnosuchfieldexception) {

} catch (Exception e) {

E.printstacktrace ();

}

return false;

}

});

Serializer.write (object);

String s = out.tostring ();

Out.close ();

return s;

}

public static String toJSONString (Object object) {

Return toJSONString (object, False, new serializerfeature[0]);

}

public static void Main (string[] args) {

Student AA = new Student ();

Aa.setname ("Lxg");

Aa.setbirstday (New Date ());

System.out.println (tojsonstring (AA));

}

}


Defines an enumeration class that can also define methods and properties in an enumeration class, so that the enumeration object has corresponding methods and properties.

It is more convenient to use, for example: String name = SearchType.SaveHistory.name;

Enumeration classes can actually be replaced with static constant classes, but they cannot be used as enumeration classes, and properties and methods can be redefined for constants.

public enum SearchType {

SaveHistory ("savehisply cost", "Save History", "1"),

Quote ("============== quote processing", "quote", "2"),

Edrapply ("============== submit a correction request Form", "Submit Application", "3"),

Edrapplyumstask ("Correcting tasks: Edrapplyumstask", "Creating data Audit Tasks", "4");

public String keyword;

public String name;

Public String Code;

Private SearchType (string keyword, string name, string code) {

This.keyword = keyword;

THIS.name = name;

This.code = code;

}

public static SearchType Matchbycode (String c) {

if ("1". Equals (c))

return savehistory;

if ("2". Equals (c))

return Quote;

if ("3". Equals (c)) {

return edrapply;

}

if ("4". Equals (c)) {

return edrapplyumstask;

}

return null;

}

}


http://www.csdn.net/article/2014-09-25/2821866


Here's the information about SPRINGMVC.

http://jinnianshilongnian.iteye.com/blog/1594806

Http://www.admin10000.com/document/6436.html




The null attribute is stripped when the object is converted to a JSON string, and the use of the enumeration class

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.