Pinpointing the nuances JSON format beautify

I. Background

Today @ paragraphs raised a question in the technology group in: "How to achieve JSON format beautification."

Specific meaning is to achieve a compact JSON string becomes a more attractive form of the back-end code.

The following JSON string

{\"age\":0,\"name\":\"张三\",\"sex\":\"女\"

Into effect in the following figure:

The question of who might know about the API can answer it, people may not know the first reaction is Baidu.

So is there a better way to solve it?

This article focuses on how to think and solve this problem, the focus is not a problem in itself, hoping to inspire you a bit.

 

Second, step

There are many ways to solve this problem, here are the steps I suggested.

The core idea is: first find, can not find themselves create the wheel.

Special special special emphasis on, think first, then act.

 

2.1 First Thoughts

JSON string landscaping features in many web tools in common, and normally is not JSON library should provide this function?

So how to find whether there is such a function? ?

First thought is key, because the pages used to provide similar functionality on a Web page, features described are "Format" and "beautification."

And we think that, if there is such a function should be called what name it? Is not it also called "formatting / beautification" mean?

We go fastjson Jar package or source code search along this line of thought: beautify / format keyword.

 

Then find the code of suspected related functions.

Find a single test run to see results:

We found exactly what we want.

2.2 From Class

If fastjson provides a format string or beautify the method most likely to turn ordinary strings and function in a class, it is also very easy to find this function.

   public static String toJSONString(Object object, boolean prettyFormat) {
        if (!prettyFormat) {
            return toJSONString(object);
        }

        return toJSONString(object, SerializerFeature.PrettyFormat);
    }

2.3 see the official documents

Fastjson code comments speak really small, serious Tucao! ! And the official documentation is very incomplete, severely Tucao! !

For us little help.

 

2.4 With search engine

Search fastjson formatted or landscaping, or in English search from google or sof where you can find many clues.

https://github.com/alibaba/fastjson/issues/150

2.5 ask others

Through the first few steps, this step is almost not used anymore.

 

 

Third, think

Recent thinking about how the new advanced faster and better, a lot of people anxious to learn, but never learn how to think, I think the most important thing should change the thinking consciousness.

 

1 From the specific learning and memory specific knowledge-based, problem-solving methods to learn to think and turn-based .

We do not just care about learning when 2 commonly used functions, should always look into the source code, open the function list to see some very useful features.

3 should be thinking from a designer's perspective, think about what features it should have, if I write how I would design, source code, and then compare, in order to understand the profound, in order to constantly correct their thinking, like good code to move closer.

If direct learning, looking directly at the blog analysis, like watching do direct answer questions, and consequently will be looked at, in fact, I did not grasp, watching the easy to forget, not to remember with .

4 before opening a lot of questions and then ask others to wait a bit, check the common steps if they have used up? Look as source code, see the official document, see the single measure, such as search engines.

 

However, many people underestimate method, a lot of people do not think for a long time to learn how to learn, a lot of people learned a long time and never see the source code, a lot of people learned a long time and never see the official documents, many people do not think before asking questions, a lot of before people did not even find someone to help express a clear problem.

 

Published 379 original articles · won praise 862 · Views 1.32 million +

Guess you like

Origin blog.csdn.net/w605283073/article/details/102751077