JS number type accuracy problem
In js number type is 53 bit, if backgroud response 64 bit long type,
js will lose accuracy, number will not be the origin number.
53 bit in decimal system length is about 16(2^52),but in java long type is about 20(2^64).
So backgroud return type in string is a better choice to void accuracy problem.
JsonSerializer JsonDeserializer
Avoiding JS loss of accuracy1
2
3
4
5
6
7
8public class LongJsonSerializer extends JsonSerializer<Long> {
public void serialize(Long value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
String text = (value == null ? null : String.valueOf(value));
if (text != null)
jsonGenerator.writeString(text);
}
}
Deserialize input model1
2
3
4
5
6
7
8
9
10
11
12
13 4j
public class LongJsonDeserializer extends JsonDeserializer<Long> {
public Long deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
String value = jsonParser.getText();
try {
return value == null ? null : Long.parseLong(value);
} catch (NumberFormatException e) {
log.error("Deserialize long type error: {}", value);
return null;
}
}
}
Annotate the target property1
2
3.class) (using = LongJsonSerializer
@JsonDeserialize(using = LongJsonDeserializer.class)
private Long id;
Data Binding [Commonly used]
Maven
1 | <!--jackson--> |
Model
1 |
|
1 |
|
1 |
|
Bean2JsonStr
1 |
|
1 | { |
JsonStr2Bean
string -> object1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void JsonStr2Bean() throws IOException {
ObjectMapper mapper = new ObjectMapper();
File jsonFile = new File("country-demo.json");
// not interrupt unknown properties
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
Country country = mapper.readValue(jsonFile, Country.class);
log.info(country.getCountryName());
List<Province> provinces = country.getProvinces();
for (Province province : provinces) {
for (City city : province.getCities()) {
log.info(city.getId() + " " + city.getCityName());
}
}
}
1 | 09:52:05.570 [main] INFO jackson.JacksonDemoTester - China |
JsonStr2List
string -> list1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void JsonStr2List() throws IOException {
City city1 = new City(1, "hangzhou");
City city2 = new City(2, "taizhou");
List<City> cities = new ArrayList<>();
cities.add(city1);
cities.add(city2);
ObjectMapper mapper = new ObjectMapper();
String listJsonStr = mapper.writeValueAsString(cities);
log.info(listJsonStr);
List<City> list = mapper.readValue(listJsonStr, new TypeReference<List<City>>() {});
for (City city : list) {
log.info("id:" + city.getId() + " cityName:" + city.getCityName());
}
}
1 | 09:52:15.610 [main] INFO jackson.JacksonDemoTester - China |
Streaming API [high performence]
self define paser
JsonSerializer
1 | public class CityJsonSerializer extends JsonSerializer<City> { |
JsonDeserializer
1 | 4j |
StreamJsonStr2List
1 |
|
1 | 10:15:03.874 [main] INFO jackson.JacksonDemoTester - [{"cityName":"hangzhou"},{"id":2,"cityName":"taizhou"}] |
use annotation to avoid module in code1
2
3
4.class) (using=CityJsonSerializer
public class City {
...
}
Tree Model [flexible]
TreeMode2Json
no java class/ pojo, create model by tree model1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public void TreeMode2Json() throws IOException {
// create a factory provide node
JsonNodeFactory factory = new JsonNodeFactory(false);
// create a factory to convert tree model to json
JsonFactory jsonFactory = new JsonFactory();
//create a json generator
JsonGenerator generator = jsonFactory.createGenerator(new FileWriter(new File("country-demo2.json")));
ArrayNode cities = factory.arrayNode();
cities.add(factory.objectNode().put("id", 1).put("cityName", "hangzhou"))
.add(factory.objectNode().put("id", 2).put("cityName", "taizhou"));
ArrayNode provinces = factory.arrayNode();
ObjectNode province = factory.objectNode();
province.put("cities", cities);
province.put("provinceName", "zhejiang");
provinces.add(province);
ObjectNode country = factory.objectNode();
country.put("id", 1).put("countryName", "China");
country.put("provinces", provinces);
// caution! in default mapper not set root node
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
mapper.writeTree(generator, country);
}
TreeModeReadJson
1 |
|
1 | 10:58:44.995 [main] INFO jackson.JacksonDemoTester - node JsonNodeType:OBJECT |
https://www.cnblogs.com/lvgg/p/7475140.html
https://www.cnblogs.com/williamjie/p/9242451.html
https://blog.csdn.net/gjb724332682/article/details/51586701
https://blog.csdn.net/java_huashan/article/details/46375857