JSON serialization Java Tool class

Source: Internet
Author: User
Tags parse string serialization

JSON serialization Common tool Class (Java)

> to support the normal use of this tool class, first import the following jar packages
The 1.lombok package is a jar package that simplifies Java redundancy code and is very handy in real-world development.

Import Lombok coordinates in MAVEN Pom file
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>

2.json Serialized JAR Package
Import Lombok coordinates in MAVEN Pom file
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.12</version>
</dependency>

3.String Processing of JAR packages
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
The tool classes are as follows:
Import lombok.extern.slf4j.Slf4j;
Import Org.apache.commons.lang.StringUtils;
Import Org.codehaus.jackson.map.DeserializationConfig;
Import Org.codehaus.jackson.map.ObjectMapper;
Import Org.codehaus.jackson.map.SerializationConfig;
Import org.codehaus.jackson.map.annotate.JsonSerialize;
Import Org.codehaus.jackson.type.JavaType;
Import org.codehaus.jackson.type.TypeReference;

Import Java.text.SimpleDateFormat;

@Slf4j
public class Jsonutil {

private static Objectmapper Objectmapper = new Objectmapper ();
private static final String Standard_format = "Yyyy-mm-dd HH:mm:ss";

static {
All fields of the object are listed in the
Objectmapper.setserializationinclusion (JsonSerialize.Inclusion.ALWAYS);
Cancels the default conversion timestamps form
Objectmapper.configure (SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
Error ignoring empty bean to JSON
Objectmapper.configure (SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
All date formats are unified into the following styles, i.e. Yyyy-mm-dd HH:mm:ss
Objectmapper.setdateformat (New SimpleDateFormat (Standard_format));
Ignoring the existence of a JSON string, but there is no corresponding attribute in the Java object. Prevent errors
Objectmapper.configure (DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}

public static <T> String obj2string (T obj) {
if (obj = = null) {
return null;
}
try {
return obj instanceof String? (String) obj:objectMapper.writeValueAsString (obj);
} catch (Exception e) {
Log.warn ("Parse Object to String error", E);
return null;
}
}

public static <T> String obj2stringpretty (T obj) {
if (obj = = null) {
return null;
}
try {
return obj instanceof String? (String) Obj:objectMapper.writerWithDefaultPrettyPrinter (). writevalueasstring (obj);
} catch (Exception e) {
Log.warn ("Parse Object to String error", E);
return null;
}
}


public static <T> T string2obj (String str, class<t> clazz) {
if (Stringutils.isempty (str) | | | clazz = = NULL) {
return null;
}

try {
Return Clazz.equals (string.class)? (T) Str:objectMapper.readValue (str, clazz);
} catch (Exception e) {
Log.warn ("Parse String to Object error", E);
return null;
}
}


public static <T> T string2obj (String str, typereference<t> typereference) {
if (Stringutils.isempty (str) | | | typereference = = NULL) {
return null;
}
try {
Return (T) (Typereference.gettype () equals (String.class) str:objectMapper.readValue (str, typereference));
} catch (Exception e) {
Log.warn ("Parse String to Object error", E);
return null;
}
}


public static <T> T string2obj (String str, class<?> collectionclass, Class<?> .... elementclasses) {
Javatype Javatype = Objectmapper.gettypefactory (). Constructparametrictype (Collectionclass, elementClasses);
try {
return Objectmapper.readvalue (str, javatype);
} catch (Exception e) {
Log.warn ("Parse String to Object error", E);
return null;
}
}

This tool class is very general, recommended, the parameters can be adjusted according to their own needs.

JSON serialization Java Tool 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.