gpt4 book ai didi

java - 考虑到性能,我应该如何获得一个 ObjectWriter 将 POJO 序列化为 JSON?

转载 作者:行者123 更新时间:2023-11-30 05:44:14 24 4
gpt4 key购买 nike

com.fasterxml.jackson.databind.ObjectMapper JavaDocs说:

Mapper instances are fully thread-safe provided that ALL configuration of the instance occurs before ANY read or write calls. If configuration of a mapper instance is modified after first usage, changes may or may not take effect, and configuration calls themselves may fail. If you need to use different configuration, you have two main possibilities:

Construct and use ObjectReader for reading, ObjectWriter for writing. Both types are fully immutable and you can freely create new instances with different configuration using either factory methods of ObjectMapper, or readers/writers themselves. Construction of new ObjectReaders and ObjectWriters is a very light-weight operation so it is usually appropriate to create these on per-call basis, as needed, for configuring things like optional indentation of JSON.

每次我需要新的 ObjectWriter 时都可以进行此调用吗?

jsonString = new MyObjectWriter().objectWriter().writeValueAsString(myPojo);

MyObjectWriter 看起来像这样:

public class MyObjectWriter {
public ObjectWriter objectWriter()
{
return new ObjectMapper()
.writer()
.with(SerializationFeature.INDENT_OUTPUT)
.with(JsonGenerator.Feature.IGNORE_UNKNOWN);
}
}

我应该保留 ObjectMapper 的副本吗?对象编写器?

最佳答案

就像文档所说,这是非常便宜的操作,您可以“在每次调用的基础上”执行此操作。让我们看看每个方法背后都有什么。

  • ObjectMapper.writer - 使用 ObjectMapper 中的 SerializationConfig 创建新的 ObjectWriter
  • ObjectWriter.with - 创建新的 ObjectWriter,它基于 caller 实例以及必须启用的新功能。如果给定的功能已启用,则返回相同的实例。如果功能更改配置 - 将创建并返回新的 ObjectWriter

让我们看一下显示给定场景的示例应用程序:

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.util.Collections;
import java.util.Map;

public class JsonApp {

public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer0 = mapper.writer();
ObjectWriter writer1 = writer0.with(SerializationFeature.INDENT_OUTPUT);
ObjectWriter writer2 = writer1.with(SerializationFeature.INDENT_OUTPUT);
ObjectWriter writer3 = writer2.with(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS);

Map<String, Long> map = Collections.singletonMap("key", 123L);
System.out.println(writer0 + " = " + writer0.writeValueAsString(map));
System.out.println(writer1 + " = " + writer1.writeValueAsString(map));
System.out.println(writer2 + " = " + writer2.writeValueAsString(map));
System.out.println(writer3 + " = " + writer3.writeValueAsString(map));


ObjectMapper mapper1 = new ObjectMapper();
mapper1.enable(SerializationFeature.INDENT_OUTPUT);
mapper1.enable(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS);

ObjectWriter writer4 = mapper1.writer();
System.out.println(writer4 + " = " + writer4.writeValueAsString(map));
}
}

以上应用程序打印:

com.fasterxml.jackson.databind.ObjectWriter@2ed94a8b = {"key":123}
com.fasterxml.jackson.databind.ObjectWriter@2a5ca609 = {
"key" : 123
}
com.fasterxml.jackson.databind.ObjectWriter@2a5ca609 = {
"key" : 123
}
com.fasterxml.jackson.databind.ObjectWriter@20e2cbe0 = {
"key" : "123"
}

com.fasterxml.jackson.databind.ObjectWriter@68be2bc2 = {
"key" : "123"
}

注意,第二个 (writer1) 和第三个 (writer2) 实例 (com.fasterxml.jackson.databind.ObjectWriter@2a5ca609)是相同的。它们还生成相同的 JSON 有效负载。

因此,使用 ObjectMapper 的第一个实例,我们创建并配置了 ObjectWriter。但主要只使用最后一项。中间的所有内容都已经消失并等待 GC 进行收集。这样做没有意义。最好创建 ObjectMapper 实例,对其进行配置,然后通过调用 writer() 方法创建已配置的 ObjectWriter 。您可以为已配置的 ObjectMapper 实例创建类似于 Factory 的类,并且可以使用这些实例生成 ObjectWriter-s。

关于java - 考虑到性能,我应该如何获得一个 ObjectWriter 将 POJO 序列化为 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55127728/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com