Gson is used to handle situations where html escape is not required

Source: Internet
Author: User

Introduction:

Although it is a small problem, I still need to record it. We know that Google provides a Gson framework to make it more efficient. Common objects are generally VO better.) and the conversion between json strings. Because most conversions work with browsers, the default Gson will escape html escape, but some special fields, for example, we have an authToken which is base64 encoding, at this time, we need to keep its original value followed by 0 or two equal signs =, rather than url escape.


Solution:

In this case, we cannot use the default Gson. Instead, we need to create a new GsonBuilder, disable the html escape function of this GsonBuilder, and then use the Gson created by it for conversion, we can achieve our goal.


Example:


Suppose we have a VO:

650) this. width = 650; "src =" http://img1.51cto.com/attachment/201309/095342478.png "title =" 37.png" alt = "095342478.png"/>

It contains a field and cannot be escaped. We must keep the original content (the content after base64 encoding ).


We write a program to test:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1446214110-1.png "title =" 38.png" alt = "095540689.png"/>


The test results are as follows:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/144621N29-2.png "title =" 40.png" alt = "095732217.png"/>

Conclusion:

It can be seen from this that the default Gson will perform html escape, And it will convert the "=" sign to \ u003d. If we use GsonBuilder and disableHtmlEscaping, let it create a Gson, and then use this Gson conversion, the result will not be html escape.


This article from "parallel line cohesion" blog, please be sure to keep this source http://supercharles888.blog.51cto.com/609344/1302450

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.