-
Notifications
You must be signed in to change notification settings - Fork 496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] JSONPath使用ofJSONB读取部分byte[]的数据功能不完善 #2138
Comments
wenshao
added a commit
that referenced
this issue
Dec 29, 2023
https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.45-SNAPSHOT/ |
你好,我验证了一下,只修复了其中一个(READ_BYTES_03);另一个READ_BYTES_01返回的还是null,只支持了JavaBean的JSONB,通过字符串生成的JSONB还是返回的null,在JSONReaderJSONB的nextIfObjectStart判断中bytes[0] != -90, 所以不会执行后续的解析。 // JSONPathSingleName
...
public Object extract(JSONReader jsonReader) {
long nameHashCode;
boolean match;
if (jsonReader.jsonb) {
// 这里调用JSONReaderJSONB的nextIfObjectStart返回false
if (jsonReader.nextIfObjectStart()) {
while(true) {
do {
if (jsonReader.nextIfObjectEnd()) {
return (this.features & Feature.AlwaysReturnList.mask) != 0L ? new JSONArray() : null;
}
nameHashCode = jsonReader.readFieldNameHashCode();
} while(nameHashCode == 0L);
match = nameHashCode == this.nameHashCode;
if (match || jsonReader.isObject() || jsonReader.isArray()) {
return jsonReader.readAny();
}
jsonReader.skipValue();
}
} else {
return (this.features & Feature.AlwaysReturnList.mask) != 0L ? new JSONArray() : null;
}
}
...
}
// 源码 JSONReaderJSONB
public boolean nextIfObjectStart() {
if (this.bytes[this.offset] != -90) {
return false;
} else {
++this.offset;
return true;
}
} |
字符串对象使用JSONReader.of就能正常使用。 |
READ_BYTES_01: {
byte[] bytes = json.getBytes();
JSONPath path = JSONPath.of("$.name"); // 缓存起来重复使用能提升性能
JSONReader reader = JSONReader.ofJSONB(bytes);
System.out.println(path.extract(reader));
} 这个不应该这样用的,应该做一个转换,比如: byte[] bytes = JSON.parseObject(json).toJSONBBytes(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
问题描述
使用官网文档中的样例JSONPath ofJSONB读取部分byte[]数据失败。byte数组必须是JavaBean对象,通过字符串转的byte数组不可用;只能获取Number类型的数据。
环境信息
重现步骤
期待的正确结果
READ_BYTES_01、READ_BYTES_03返回charmander
相关日志输出
The text was updated successfully, but these errors were encountered: