-
Notifications
You must be signed in to change notification settings - Fork 511
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
[FEATURE] 建议增加 java 的 Properties 能转成有深度的 json 对象! #2442
Labels
Milestone
Comments
还有一个:希望能支持把 json 对象,转为 Properties 对象(就是上面特性的反向操作) |
properties 里的数组,需要注意下顺序:
|
wenshao
added a commit
that referenced
this issue
Apr 15, 2024
https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.50-SNAPSHOT/ @Test
public void test() {
Properties properties = new Properties();
properties.put("id", "123");
properties.put("name", "xyz");
Bean bean = PropertiesUtils.toJavaObject(properties, Bean.class);
assertEquals(123, bean.id);
assertEquals("xyz", bean.name);
Properties properties1 = PropertiesUtils.toProperties(bean);
assertEquals(2, properties1.size());
assertEquals("123", properties1.getProperty("id"));
assertEquals("xyz", properties1.getProperty("name"));
}
public static class Bean {
public int id;
public String name;
} |
@wenshao 测试用例得略复杂点儿。添加数组的模式 @Test
public void test() {
Properties properties = new Properties();
properties.put("id", "123");
properties.put("name", "xyz");
properties.put("list[0].id", "000");
properties.put("list[1].id", "111");
Bean bean = PropertiesUtils.toJavaObject(properties, Bean.class);
assertEquals(123, bean.id);
assertEquals("xyz", bean.name);
Properties properties1 = PropertiesUtils.toProperties(bean);
assertEquals(2, properties1.size());
assertEquals("123", properties1.getProperty("id"));
assertEquals("xyz", properties1.getProperty("name"));
}
public static class Bean {
public int id;
public String name;
public List<Item> list;
}
public static class Item {
public int id;
} |
https://github.com/alibaba/fastjson2/releases/tag/2.0.50 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
请描述您的需求或者改进建议
比如 Properties:
user.name=noear
能转成 Json 对象:{"user":{"name":"noear"}}
请描述你建议的实现方案
无
描述您考虑过的替代方案
目前 snack3 支持这个特性:
ONode.loadObj(new Properties()).toJson()
附加信息(应用场景)
应用开发框架(比如我的 solon),就比较需要这个特性。用它读取属性配置文件,并配置转为 bean。如果有这个特性,就可以用 fastjson2 强大的反序列化能力,去做 properties 到 bean 的转换。
The text was updated successfully, but these errors were encountered: