gpt4 book ai didi

java - 不通过 ObjectMapper 时如何在 JsonParser 上设置 ObjectCodec?

转载 作者:行者123 更新时间:2023-12-04 17:17:51 25 4
gpt4 key购买 nike

注意:这是使用 jackson 2.3.2

为了我的一个项目的需要,我正在编写一个自定义 JsonParser其中记录了 Map其中键是 JsonPointer s 和值是 Integer s(指针指向的行号)。

该类名为 LineRecorderJsonParser .它由 LineRecorderJsonFactory 制作这是一个简单的委托(delegate):

public final class LineRecorderJsonFactory
extends JsonFactory
{
@Override
protected JsonParser _createParser(final InputStream in,
final IOContext ctxt)
throws IOException, JsonParseException
{
final JsonParser parser = super._createParser(in, ctxt);
return new LineRecorderJsonParser(parser);
}

@Override
protected JsonParser _createParser(final Reader r, final IOContext ctxt)
throws IOException, JsonParseException
{
final JsonParser parser = super._createParser(r, ctxt);
return new LineRecorderJsonParser(parser);
}

@Override
protected JsonParser _createParser(final byte[] data, final int offset,
final int len, final IOContext ctxt)
throws IOException, JsonParseException
{
final JsonParser parser = super._createParser(data, offset, len, ctxt);
return new LineRecorderJsonParser(parser);
}
}

我已经使用以下代码测试了该代码是否有效:
public static void main(final String... args)
throws IOException
{
final JsonFactory factory = new LineRecorderJsonFactory();
final ObjectMapper mapper = new ObjectMapper(factory);
final Closer closer = Closer.create();
final InputStream in;

try {
in = closer.register(LineTesting.class.getResourceAsStream
("/testfile.json"));
mapper.readTree(in);
} finally {
closer.close();
}
}

但现在我想测试它;所以我写了以下测试代码:
public final class LineRecorderJsonParserTest
{
private static final String INCORRECT_LINE_INFO
= "generated line info is incorrect; expected: %s, actual: %s";

private JsonFactory factory;
private ObjectMapper mapper;

@BeforeMethod
public void initFactory()
{
factory = new LineRecorderJsonFactory();
mapper = new ObjectMapper();
}

@DataProvider
public Iterator<Object[]> getLineData()
{
final List<Object[]> list = Lists.newArrayList();

list.add(new Object[] { "1" });
return list.iterator();
}

@Test(dataProvider = "getLineData")
public void lineNumbersAreCorrectlyReported(final String subdir)
throws IOException
{
final String basePath = "/parser/" + subdir + '/';
final Closer closer = Closer.create();
final TypeReference<Map<JsonPointer, Integer>> typeRef
= new TypeReference<Map<JsonPointer, Integer>>() {};

final InputStream input, lines;
final Map<JsonPointer, Integer> actual, expected;
final LineRecorderJsonParser parser;

try {
input = closer.register(inputFrom(basePath + "input.json"));
lines = closer.register(inputFrom(basePath + "lines.json"));

expected = mapper.readValue(lines, typeRef);

parser = (LineRecorderJsonParser) factory.createParser(input);
//parser.setCodec(factory.getCodec()); // Doesn't change anything...
parser.readValueAsTree(); // FAILS HERE
actual = parser.getLineInfo();

assertEquals(actual, expected,
String.format(INCORRECT_LINE_INFO, expected, actual));
} catch (IOException e) {
throw closer.rethrow(e);
} finally {
closer.close();
}
}

private static InputStream inputFrom(final String path)
{
return LineRecorderJsonParserTest.class.getResourceAsStream(path);
}
}

不幸的是,这不起作用。我得到以下异常:
java.lang.IllegalStateException: No ObjectCodec defined for the parser, can not deserialize JSON into JsonNode tree
at com.fasterxml.jackson.core.JsonParser.readValueAsTree(JsonParser.java:1507)
at com.fasterxml.jackson.core.util.JsonParserDelegate.readValueAsTree(JsonParserDelegate.java:371)
at com.github.fge.jackson.parse.LineRecorderJsonParserTest.lineNumbersAreCorrectlyReported(LineRecorderJsonParserTest.java:84)

即使我(尝试)设置 ObjectCodec我自己(见代码),我得到这个异常......

我究竟做错了什么?我如何测试这门课,因为当你阅读时使用 ObjectMapper直接,您没有机会获取底层解析器吗?

最佳答案

或者,JsonFactory.setCodec(mapper) .

关于java - 不通过 ObjectMapper 时如何在 JsonParser 上设置 ObjectCodec?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22870488/

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