We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
使用JSONPath.set对未知属性赋值失败
在以下的测试用例中,期望使用JSONPath.set(p, "$.age", 13)修改属性age的值为13,但读到age值仍然为12
JSONPath.set(p, "$.age", 13)
age
13
12
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONPath; import com.alibaba.fastjson.annotation.JSONField; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.util.HashMap; import java.util.Map; public class MapperTest { static class People { private String name; private Map<String, Object> extra = new HashMap<>(); public String getName() { return name; } public void setName(String name) { this.name = name; } public Map<String, Object> getExtra() { return extra; } public void setExtra(Map<String, Object> extra) { this.extra = extra; } @JSONField(unwrapped = true) public void set(String key, Object value) { this.extra.put(key, value); } @JSONField(unwrapped = true) public Object get(String key) { return this.extra.get(key); } } @Test public void test() { String json = "{\"name\": \"lisi\", \"age\": 12}"; People p = JSON.parseObject(json, People.class); Assertions.assertEquals("lisi", p.name); // 成功 Assertions.assertEquals(12, p.extra.get("age")); // 成功, 使用JSON.parseObject对未知属性赋值成功 JSONPath.set(p, "$.age", 13); Assertions.assertEquals(13, p.extra.get("age")); // 失败: 使用JSONPath.set对未知属性赋值失败, p.extra.get("age") == 12 } }
p.extra.get("age") == 13
The text was updated successfully, but these errors were encountered:
.bi
Sorry, something went wrong.
https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.18-SNAPSHOT/ 问题已修复,请帮忙用2.0.18-SNAPSHOT版本验证,2.0.18版本预计在月底发布
2.0.18-SNAPSHOT版本测试失败,这个版本的JSON.parseObject对未知属性的赋值也不生效,以下是测试结果描述
@Test public void test() { String json = "{\"name\": \"lisi\", \"age\": 12}"; People p = JSON.parseObject(json, People.class); Assertions.assertEquals("lisi", p.name); // 成功 Assertions.assertEquals(12, p.extra.get("age")); // 失败: p.extra.get("age") == null JSONPath.set(p, "$.age", 13); Assertions.assertEquals(13, p.extra.get("age")); // 失败: p.extra.get("age") == null }
完整的测试代码见重现步骤
add testcase for issue #897
919745a
https://github.com/alibaba/fastjson2/releases/tag/2.0.18 问题已修复,请用新版本
No branches or pull requests
问题描述
使用JSONPath.set对未知属性赋值失败
环境信息
重现步骤
在以下的测试用例中,期望使用
JSONPath.set(p, "$.age", 13)
修改属性age
的值为13
,但读到age
值仍然为12
期待的正确结果
p.extra.get("age") == 13
The text was updated successfully, but these errors were encountered: