json字符串格式化输出fastjson、gson、jackson实现
import org.junit.jupiter.api.Test;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.extern.slf4j.Slf4j;
/**
* Json字符串格式化
*/
@Slf4j
public class JsonPrettyFormat
{
private String jsonString =
"{\"_index\":\"book_shop\",\"_type\":\"it_book\",\"_id\":\"1\",\"_score\":1.0, \"_source\":{\"name\": \"Java编程思想(第4版)\",\"author\": \"[美] Bruce Eckel\",\"category\": \"编程语言\", \"price\": 109.0,\"publisher\": \"机械工业出版社\",\"date\": \"2007-06-01\",\"tags\": [ \"Java\", \"编程语言\" ]}}";
@Test
public void fastjsonTest()
{
JSONObject object = JSONObject.parseObject(jsonString);
String pretty = JSON.toJSONString(object, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat);
log.info("{} ", pretty);
}
@Test
public void gsonTest()
{
JsonObject jsonObject = JsonParser.parseString(jsonString).getAsJsonObject();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String pretty = gson.toJson(jsonObject);
log.info("{} ", pretty);
}
@Test
public void jacksonTest()
throws JsonMappingException, JsonProcessingException
{
ObjectMapper mapper = new ObjectMapper();
Object obj = mapper.readValue(jsonString, Object.class);
String pretty = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
log.info("{} ", pretty);
JsonNode jsonNode = mapper.readTree(jsonString);
pretty = jsonNode.toPrettyString();
log.info("{} ", pretty);
}
}
2023-12-19 17:26:19.704 INFO 21640 --- [ main] c.f.t.r.j.JsonPrettyFormat : {
"_index":"book_shop",
"_type":"it_book",
"_source":{
"date":"2007-06-01",
"author":"[美] Bruce Eckel",
"price":109.0,
"name":"Java编程思想(第4版)",
"publisher":"机械工业出版社",
"category":"编程语言",
"tags":[
"Java",
"编程语言"
]
},
"_id":"1",
"_score":1.0
}
2023-12-19 17:26:19.744 INFO 21640 --- [ main] c.f.t.r.j.JsonPrettyFormat : {
"_index": "book_shop",
"_type": "it_book",
"_id": "1",
"_score": 1.0,
"_source": {
"name": "Java编程思想(第4版)",
"author": "[美] Bruce Eckel",
"category": "编程语言",
"price": 109.0,
"publisher": "机械工业出版社",
"date": "2007-06-01",
"tags": [
"Java",
"编程语言"
]
}
}
2023-12-19 17:26:19.991 INFO 21640 --- [ main] c.f.t.r.j.JsonPrettyFormat : {
"_index" : "book_shop",
"_type" : "it_book",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "Java编程思想(第4版)",
"author" : "[美] Bruce Eckel",
"category" : "编程语言",
"price" : 109.0,
"publisher" : "机械工业出版社",
"date" : "2007-06-01",
"tags" : [ "Java", "编程语言" ]
}
}
2023-12-19 17:26:19.999 INFO 21640 --- [ main] c.f.t.r.j.JsonPrettyFormat : {
"_index" : "book_shop",
"_type" : "it_book",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "Java编程思想(第4版)",
"author" : "[美] Bruce Eckel",
"category" : "编程语言",
"price" : 109.0,
"publisher" : "机械工业出版社",
"date" : "2007-06-01",
"tags" : [ "Java", "编程语言" ]
}
}