- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我写了下面的 JsonSerializer
让 Jackson将整数数组序列化为 JSON:
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
public class TalkIdsSerializer extends JsonSerializer<TalkIds> {
/**
* Serializes a TalkIds object into the following JSON string:
* Example: { "talk_ids" : [ 5931, 5930 ] }
*/
@Override
public void serialize(TalkIds talkIds, JsonGenerator jsonGenerator,
SerializerProvider provider)
throws IOException {
jsonGenerator.writeStartObject();
jsonGenerator.writeArrayFieldStart(TalkIds.API_DICTIONARY_KEY);
for (Integer talkId : talkIds.getTalkIds()) {
jsonGenerator.writeNumber(talkId);
}
jsonGenerator.writeEndArray();
jsonGenerator.writeEndObject();
}
}
这里使用的类:
@JsonSerialize(using = TalkIdsSerializer.class)
public class TalkIds { /* ... */ }
我想测试序列化程序的行为并提出以下内容:
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class TalkIdsSerializerTest {
protected final ArrayList<Integer> TALK_IDS =
new ArrayList<>(Arrays.asList(5931, 5930));
protected TalkIdsSerializer talkIdsSerializer;
@Before
public void setup() throws IOException {
talkIdsSerializer = new TalkIdsSerializer();
}
@Test
public void testSerialize() throws IOException {
StringWriter stringWriter = new StringWriter();
JsonGenerator jsonGenerator =
new JsonFactory().createGenerator(stringWriter);
TalkIds talkIds = new TalkIds();
talkIds.add(TALK_IDS);
talkIdsSerializer.serialize(talkIds, jsonGenerator, null);
String string = stringWriter.toString(); // string is ""
assertNotNull(string);
assertTrue(string.length() > 0);
stringWriter.close();
}
}
但是,没有写入StringWriter
。我做错了什么?
最佳答案
你需要flush()
生成器
Method called to flush any buffered content to the underlying target (output stream, writer), and to flush the target itself as well.
http://fasterxml.github.io/jackson-core/javadoc/2.1.0/com/fasterxml/jackson/core/JsonGenerator.html#flush()
关于java - 如何对自定义 Jackson JsonSerializer 进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33892754/
问题 我有一个 Spring MVC 应用程序,它要求我将某个实体列表的 id 和名称转换为具有特定格式的 JSON 对象数组,并在某个请求上输出。也就是说,我需要一个像这样的 JSON 对象数组:
当我阅读最新 RestSharp 的 readme.txt 时: *** IMPORTANT CHANGE IN RESTSHARP VERSION 103 *** In 103.0, JSON.NE
我有两个类(class)。以二等领域第一。 class A { @JsonSerializer(using = CustomBSerializer.class) private B b; }
有什么方法可以从 JsonSerializer 类中取出设置并在新的 JsonSerializer 中重新实现它们? 似乎没有任何方法可以做那样的事情。我发现最好的是通过反射调用的私有(private
在我的java应用程序中,我分别使用 JsonSerializer 和 JsonSerializer() 来序列化 java.util.Date 和 java.sql.Timestamp 字段。但问题
我想使用 Foundation 的 JSONSerialization.data(withJSONObject obj: AnyObject, options opt:writingOptions =
根据PHP手册 If a Pure Enum is serialized to JSON, an error will be thrown. If a Backed Enum is serialize
我有一个父类,我在其上指定了一个像这样的自定义反序列化器 - @JsonDeserialize(using = CustomDeserializer.class) public class Paren
根据PHP手册 If a Pure Enum is serialized to JSON, an error will be thrown. If a Backed Enum is serialize
我有一个位于 .jar 中的类,但无法修改。 它在类上有一个 @JsonSerialize() 注释。 @JsonSerialize(using = SomeClassSerializer.class
我正在使用 JSON token 开发 iOS,但遇到了 unsupported_grant_type 错误。我正在开发的代码如下 let params = ["grant_type": "passw
我有以下代码将 joda 的 LocalDate 序列化为字符串: public class JodaDateSerializer extends JsonSerializer { priva
我想添加一个包装器,其命名是在运行时确定的,因为它取决于类名(我可以使用@JsonRootName,但我不想这样做,因为我必须在每个子类上使用它,即效率不高)。 我想我应该使用 @JsonSerial
我编写了一个自定义的 JsonSerializer 来将 BigDecimal 转换为 String。我想使用 @JsonSerialize 注释调用此序列化程序,但有条件地,即如果特定 boolea
JSONSerialization 未在服务器发送时序列化数据。它反转数据。我使用后端的数据过滤 API。它发送准确的数据,我还检查了 postman 和 Android 端,但 iOS 代码更改了响
我正在尝试将 SwiftyJSON 与我的 JSON api 数据一起使用,但我无法理解转换的工作原理。 这是 SwiftyJSON Git 的链接:https://github.com/Swifty
我有一个自定义类 public class Balance: NSObject { var details: String var date: Date var amount:
我在解析 json 时遇到错误 "hoursOfOperation" : { "sat" : { "enabled" : true, "schedule"
什么是 option: [] in JSONSerialization in swift let jsonData = try JSONSerialization.data(withJSONObjec
我在将我的模型翻译成字典时遇到了问题。我的模型继承自 Codable,当使用 JSONSerialization 时,我得到了一个字典,其形式为: (...,"sensors": ( { accx =
我是一名优秀的程序员,十分优秀!