AnyValue.toJsonObject

This commit is contained in:
redkale
2024-08-20 18:16:12 +08:00
parent 254ab9a25a
commit f6d20a6324
6 changed files with 109 additions and 30 deletions

View File

@@ -15,6 +15,9 @@ import org.redkale.util.*;
*
* <p>详情见: https://redkale.org
*
* @see org.redkale.convert.json.JsonElement
* @see org.redkale.convert.json.JsonObject
* @see org.redkale.convert.json.JsonString
* @author zhangjx
* @since 2.8.0
*/

View File

@@ -10,6 +10,9 @@ import org.redkale.util.Utility;
*
* <p>详情见: https://redkale.org
*
* @see org.redkale.convert.json.JsonObject
* @see org.redkale.convert.json.JsonString
* @see org.redkale.convert.json.JsonArray
* @author zhangjx
* @since 2.8.0
*/

View File

@@ -15,6 +15,9 @@ import org.redkale.util.*;
*
* <p>详情见: https://redkale.org
*
* @see org.redkale.convert.json.JsonElement
* @see org.redkale.convert.json.JsonString
* @see org.redkale.convert.json.JsonArray
* @author zhangjx
* @since 2.8.0
*/

View File

@@ -10,6 +10,9 @@ import org.redkale.convert.ConvertDisabled;
*
* <p>详情见: https://redkale.org
*
* @see org.redkale.convert.json.JsonElement
* @see org.redkale.convert.json.JsonObject
* @see org.redkale.convert.json.JsonArray
* @author zhangjx
* @since 2.8.0
*/

View File

@@ -11,6 +11,8 @@ import java.util.*;
import java.util.function.*;
import org.redkale.annotation.ConstructorParameters;
import org.redkale.convert.ConvertColumn;
import org.redkale.convert.json.JsonArray;
import org.redkale.convert.json.JsonObject;
import static org.redkale.util.Utility.isEmpty;
/**
@@ -1168,34 +1170,89 @@ public abstract class AnyValue {
public Properties toProperties() {
Properties props = new Properties();
toProperties(props, new HashMap<>(), "", this);
toProperties(props, "", this);
return props;
}
private static void toProperties(Properties props, Map<String, Integer> keyPrefixs, String parent, AnyValue conf) {
conf.forEach(
(k, v) -> {
final String prefix = parent + k;
if (keyPrefixs.containsKey(prefix)) {
String key = prefix;
int index = keyPrefixs.get(key);
if (index == -1) {
key = prefix + "[" + (++index) + "]";
props.put(key, props.remove(prefix));
}
key = prefix + "[" + (++index) + "]";
while (props.containsKey(key)) {
key = prefix + "[" + (++index) + "]";
}
props.put(key, v);
keyPrefixs.put(prefix, index);
} else {
props.put(prefix, v);
keyPrefixs.put(prefix, -1);
protected static void toProperties(Properties props, String parent, AnyValue conf) {
Map<String, List> tmp = new HashMap<>();
Entry<String>[] strs = conf.getStringEntrys();
if (strs != null && strs.length > 0) {
for (Entry<String> en : strs) {
tmp.computeIfAbsent(en.getName(), k -> new ArrayList<>()).add(en.getValue());
}
tmp.forEach((k, list) -> {
if (list.size() == 1) {
props.put(parent + k, list.get(0));
} else {
int i = -1;
for (Object item : list) {
props.put(parent + k + "[" + (++i) + "]", item);
}
},
(k, c) -> {
toProperties(props, keyPrefixs, parent + k + ".", c);
});
}
});
}
Entry<AnyValue>[] entrys = conf.getAnyEntrys();
if (entrys != null && entrys.length > 0) {
tmp.clear();
for (Entry<AnyValue> en : entrys) {
tmp.computeIfAbsent(en.getName(), k -> new ArrayList<>()).add(en.getValue());
}
tmp.forEach((k, list) -> {
if (list.size() == 1) {
toProperties(props, parent + k + ".", (AnyValue) list.get(0));
} else {
int i = -1;
for (Object item : list) {
toProperties(props, parent + k + "[" + (++i) + "].", (AnyValue) item);
}
}
});
}
}
public JsonObject toJsonObject() {
JsonObject json = new JsonObject();
toJsonObject(json, this);
return json;
}
protected static void toJsonObject(JsonObject json, AnyValue conf) {
Map<String, JsonArray> tmp = new HashMap<>();
Entry<String>[] strs = conf.getStringEntrys();
if (strs != null && strs.length > 0) {
for (Entry<String> en : strs) {
tmp.computeIfAbsent(en.getName(), k -> new JsonArray()).add(en.getValue());
}
tmp.forEach((k, list) -> {
if (list.size() == 1) {
json.put(k, list.get(0));
} else {
json.put(k, list);
}
});
}
Entry<AnyValue>[] entrys = conf.getAnyEntrys();
if (entrys != null && entrys.length > 0) {
tmp.clear();
for (Entry<AnyValue> en : entrys) {
tmp.computeIfAbsent(en.getName(), k -> new JsonArray()).add(en.getValue());
}
tmp.forEach((k, list) -> {
if (list.size() == 1) {
JsonObject val = new JsonObject();
toJsonObject(val, (AnyValue) list.get(0));
json.put(k, val);
} else {
JsonArray array = new JsonArray();
for (Object item : list) {
JsonObject val = new JsonObject();
toJsonObject(val, (AnyValue) item);
array.add(val);
}
json.put(k, array);
}
});
}
}
}

View File

@@ -6,6 +6,7 @@
package org.redkale.test.util;
import java.util.Properties;
import java.util.TreeMap;
import org.junit.jupiter.api.*;
import org.redkale.util.AnyValue;
import org.redkale.util.AnyValueWriter;
@@ -152,22 +153,31 @@ public class AnyValuePropertiesTest {
@Test
public void run5() {
AnyValueWriter conf = AnyValue.create();
conf.addValue("name", "haha");
conf.addValue("name", "hehe");
conf.addValue("ns", "haha");
conf.addValue("ns", "hehe");
conf.addValue("status", 45);
conf.addValue("name", AnyValueWriter.create("id", 123).addValue("desc", "test"));
conf.addValue("nodes", AnyValueWriter.create("time", 123).addValue("time", 456));
conf.addValue("area", AnyValueWriter.create("areaid", 123).addValue("name", "aaa"));
conf.addValue("area", AnyValueWriter.create("areaid", 456).addValue("name", "bbb"));
Properties props1 = conf.toProperties();
props1.forEach((k, v) -> System.out.println(k + " = " + v));
System.out.println(conf);
System.out.println(conf.toJsonObject());
System.out.println("----------------------------------------------------");
new TreeMap(props1).forEach((k, v) -> System.out.println(k + " = " + v));
Properties props2 = new Properties();
props2.put("name[0]", "haha");
props2.put("name[1]", "hehe");
props2.put("ns[0]", "haha");
props2.put("ns[1]", "hehe");
props2.put("status", "45");
props2.put("name.desc", "test");
props2.put("name.id", "123");
props2.put("nodes.time[0]", "123");
props2.put("nodes.time[1]", "456");
props2.put("area[0].areaid", "123");
props2.put("area[0].name", "aaa");
props2.put("area[1].areaid", "456");
props2.put("area[1].name", "bbb");
Assertions.assertEquals(props1.toString(), props2.toString());
System.out.println("------------------------ 05 ------------------------");