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
测试用例如下
@Getter @Setter static class User{ private String name; private List<NVString> additionalInfo; } @Getter @Setter @ToString static class NVString{ String name; String value; } public static final String JSON = "{\"name\":\"fastjson2\",\"additionalInfo\":[{\"name\":\"srv6-color\",\"value\":\"test\"},{\"name\":\"test2\",\"value\":\"3\"}]}"; public static String path = "$.additionalInfo[?(@.name=='srv6-color')].value"; @Test public void test_fastjson2_case1(){ User user = JsonUtil.fromJson(JSON,User.class); JSONPath jsonPath = JSONPath.of("$.additionalInfo[?(@.name=='srv6-color')].value"); jsonPath.set(user, "modify"); Assert.assertEquals(user.getAdditionalInfo().get(0).getValue(),"modify"); } @Test public void test_fastjson_case1(){ User user = JsonUtil.fromJson(JSON,User.class); com.alibaba.fastjson.JSONPath.set(user,"$.additionalInfo[?(@.name=='srv6-color')].value","modify"); JSONPath jsonPath = JSONPath.of("$.additionalInfo[?(@.name=='srv6-color')].value"); jsonPath.set(user, "modify"); Assert.assertEquals(user.getAdditionalInfo().get(0).getValue(),"modify"); }
对比fastjson和fastjson2、不支持直接对对象进行设置、会将List中的对象直接设置为null 想请教下: 是故意这么设计不支持List中对象的jpath set对象、还是bug
看源码:再JSONPathSegmentName中的
public void set(JSONPath.Context context, Object value) { Object object = context.parent == null ? context.root : context.parent.value; if (object instanceof Map) { Map map = (Map) object; Object origin = map.put(name, value); if (origin != null) { if ((context.readerFeatures & JSONReader.Feature.DuplicateKeyValueAsArray.mask) != 0) { if (origin instanceof Collection) { ((Collection) origin).add(value); map.put(name, value); } else { JSONArray array = JSONArray.of(origin, value); map.put(name, array); } } } return; } if (object instanceof Collection) { Collection collection = (Collection) object; for (Object item : collection) { if (item == null) { continue; } if (item instanceof Map) { Map map = (Map) item; Object origin = map.put(name, value); if (origin != null) { if ((context.readerFeatures & JSONReader.Feature.DuplicateKeyValueAsArray.mask) != 0) { if (origin instanceof Collection) { ((Collection) origin).add(value); map.put(name, value); } else { JSONArray array = JSONArray.of(origin, value); map.put(name, array); } } } continue; } ObjectReaderProvider provider = context.path.getReaderContext().getProvider(); ObjectReader objectReader = provider.getObjectReader(item.getClass()); FieldReader fieldReader = objectReader.getFieldReader(nameHashCode); if (fieldReader != null) { // **此处设置为了null** fieldReader.accept(item, null); } } return; } ObjectReaderProvider provider = context.path.getReaderContext().getProvider(); ObjectReader objectReader = provider.getObjectReader(object.getClass()); FieldReader fieldReader = objectReader.getFieldReader(nameHashCode); if (fieldReader == null) { return; } if (value != null) { Class<?> valueClass = value.getClass(); Class fieldClass = fieldReader.fieldClass; if (valueClass != fieldClass) { java.util.function.Function typeConvert = provider.getTypeConvert(valueClass, fieldClass); if (typeConvert != null) { value = typeConvert.apply(value); } } } fieldReader.accept(object, value); }
The text was updated successfully, but these errors were encountered:
https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson/2.0.54-SNAPSHOT/ 问题已修复,请帮忙用2.0.54-SNAPSHOT快照版本验证,2.0.54正式版本预计在月底发布
Sorry, something went wrong.
bug fix for jsonpath set, for issue #3125
62a67f2
https://github.com/alibaba/fastjson2/releases/tag/2.0.54 问题已修复,请用新版本
No branches or pull requests
测试用例如下
对比fastjson和fastjson2、不支持直接对对象进行设置、会将List中的对象直接设置为null
想请教下: 是故意这么设计不支持List中对象的jpath set对象、还是bug
看源码:再JSONPathSegmentName中的
The text was updated successfully, but these errors were encountered: