Google JSON technology-converting to JSON ignores certain attributes

Source: Internet
Author: User
Tags stub

The company's small project just started. I wrote some util methods.

The company uses Google's JSON technology to process JSON, which is transformed into entities by receiving httpservletrequest input streams.

Pom.xml as follows

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.7</version>
</dependency>

Here's how:

public static <T> T Convertjson (httpservletrequest request,type typeoft,string[] ignorefields) throws IOException , jsonsyntaxexception{
HttpServletRequest temp_request = Request;
String json = commonutil.getrequeststring (temp_request); Using the Request.getreader () method to get the string method is not posted here.
if (Json.equals ("")) {
throw new Nojsondataexception ();
}

A collection of fields that need to be ignored
list<string> list = new arraylist<> ();
for (String str:ignorefields) {
List.add (str);
}

Set the time format
Gson Gson = new Gsonbuilder (). Setdateformat ("Yyyy-mm-dd HH:mm:ss")
. Setexclusionstrategies (New Exclusionstrategy () {

Field names that need to be ignored

@Override
public boolean Shouldskipfield (FieldAttributes f) {
TODO auto-generated Method Stub
Return List.contains (F.getname ());
}

field types that need to be emptied

@Override
public boolean shouldskipclass (class<?> clazz) {
TODO auto-generated Method Stub
Return Clazz.getname (). Equals (Date.class.getName ());
}
}). Create ();


Return (T) Gson.fromjson (JSON, typeoft);
}

Google JSON technology-converting to JSON ignores certain attributes

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.