AnyValue.toJsonObject
This commit is contained in:
@@ -15,6 +15,9 @@ import org.redkale.util.*;
|
|||||||
*
|
*
|
||||||
* <p>详情见: https://redkale.org
|
* <p>详情见: https://redkale.org
|
||||||
*
|
*
|
||||||
|
* @see org.redkale.convert.json.JsonElement
|
||||||
|
* @see org.redkale.convert.json.JsonObject
|
||||||
|
* @see org.redkale.convert.json.JsonString
|
||||||
* @author zhangjx
|
* @author zhangjx
|
||||||
* @since 2.8.0
|
* @since 2.8.0
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -10,6 +10,9 @@ import org.redkale.util.Utility;
|
|||||||
*
|
*
|
||||||
* <p>详情见: https://redkale.org
|
* <p>详情见: https://redkale.org
|
||||||
*
|
*
|
||||||
|
* @see org.redkale.convert.json.JsonObject
|
||||||
|
* @see org.redkale.convert.json.JsonString
|
||||||
|
* @see org.redkale.convert.json.JsonArray
|
||||||
* @author zhangjx
|
* @author zhangjx
|
||||||
* @since 2.8.0
|
* @since 2.8.0
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -15,6 +15,9 @@ import org.redkale.util.*;
|
|||||||
*
|
*
|
||||||
* <p>详情见: https://redkale.org
|
* <p>详情见: https://redkale.org
|
||||||
*
|
*
|
||||||
|
* @see org.redkale.convert.json.JsonElement
|
||||||
|
* @see org.redkale.convert.json.JsonString
|
||||||
|
* @see org.redkale.convert.json.JsonArray
|
||||||
* @author zhangjx
|
* @author zhangjx
|
||||||
* @since 2.8.0
|
* @since 2.8.0
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -10,6 +10,9 @@ import org.redkale.convert.ConvertDisabled;
|
|||||||
*
|
*
|
||||||
* <p>详情见: https://redkale.org
|
* <p>详情见: https://redkale.org
|
||||||
*
|
*
|
||||||
|
* @see org.redkale.convert.json.JsonElement
|
||||||
|
* @see org.redkale.convert.json.JsonObject
|
||||||
|
* @see org.redkale.convert.json.JsonArray
|
||||||
* @author zhangjx
|
* @author zhangjx
|
||||||
* @since 2.8.0
|
* @since 2.8.0
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ import java.util.*;
|
|||||||
import java.util.function.*;
|
import java.util.function.*;
|
||||||
import org.redkale.annotation.ConstructorParameters;
|
import org.redkale.annotation.ConstructorParameters;
|
||||||
import org.redkale.convert.ConvertColumn;
|
import org.redkale.convert.ConvertColumn;
|
||||||
|
import org.redkale.convert.json.JsonArray;
|
||||||
|
import org.redkale.convert.json.JsonObject;
|
||||||
import static org.redkale.util.Utility.isEmpty;
|
import static org.redkale.util.Utility.isEmpty;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1168,34 +1170,89 @@ public abstract class AnyValue {
|
|||||||
|
|
||||||
public Properties toProperties() {
|
public Properties toProperties() {
|
||||||
Properties props = new Properties();
|
Properties props = new Properties();
|
||||||
toProperties(props, new HashMap<>(), "", this);
|
toProperties(props, "", this);
|
||||||
return props;
|
return props;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void toProperties(Properties props, Map<String, Integer> keyPrefixs, String parent, AnyValue conf) {
|
protected static void toProperties(Properties props, String parent, AnyValue conf) {
|
||||||
conf.forEach(
|
Map<String, List> tmp = new HashMap<>();
|
||||||
(k, v) -> {
|
Entry<String>[] strs = conf.getStringEntrys();
|
||||||
final String prefix = parent + k;
|
if (strs != null && strs.length > 0) {
|
||||||
if (keyPrefixs.containsKey(prefix)) {
|
for (Entry<String> en : strs) {
|
||||||
String key = prefix;
|
tmp.computeIfAbsent(en.getName(), k -> new ArrayList<>()).add(en.getValue());
|
||||||
int index = keyPrefixs.get(key);
|
|
||||||
if (index == -1) {
|
|
||||||
key = prefix + "[" + (++index) + "]";
|
|
||||||
props.put(key, props.remove(prefix));
|
|
||||||
}
|
}
|
||||||
key = prefix + "[" + (++index) + "]";
|
tmp.forEach((k, list) -> {
|
||||||
while (props.containsKey(key)) {
|
if (list.size() == 1) {
|
||||||
key = prefix + "[" + (++index) + "]";
|
props.put(parent + k, list.get(0));
|
||||||
}
|
|
||||||
props.put(key, v);
|
|
||||||
keyPrefixs.put(prefix, index);
|
|
||||||
} else {
|
} else {
|
||||||
props.put(prefix, v);
|
int i = -1;
|
||||||
keyPrefixs.put(prefix, -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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
package org.redkale.test.util;
|
package org.redkale.test.util;
|
||||||
|
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
import java.util.TreeMap;
|
||||||
import org.junit.jupiter.api.*;
|
import org.junit.jupiter.api.*;
|
||||||
import org.redkale.util.AnyValue;
|
import org.redkale.util.AnyValue;
|
||||||
import org.redkale.util.AnyValueWriter;
|
import org.redkale.util.AnyValueWriter;
|
||||||
@@ -152,22 +153,31 @@ public class AnyValuePropertiesTest {
|
|||||||
@Test
|
@Test
|
||||||
public void run5() {
|
public void run5() {
|
||||||
AnyValueWriter conf = AnyValue.create();
|
AnyValueWriter conf = AnyValue.create();
|
||||||
conf.addValue("name", "haha");
|
conf.addValue("ns", "haha");
|
||||||
conf.addValue("name", "hehe");
|
conf.addValue("ns", "hehe");
|
||||||
conf.addValue("status", 45);
|
conf.addValue("status", 45);
|
||||||
conf.addValue("name", AnyValueWriter.create("id", 123).addValue("desc", "test"));
|
conf.addValue("name", AnyValueWriter.create("id", 123).addValue("desc", "test"));
|
||||||
conf.addValue("nodes", AnyValueWriter.create("time", 123).addValue("time", 456));
|
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();
|
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();
|
Properties props2 = new Properties();
|
||||||
props2.put("name[0]", "haha");
|
props2.put("ns[0]", "haha");
|
||||||
props2.put("name[1]", "hehe");
|
props2.put("ns[1]", "hehe");
|
||||||
props2.put("status", "45");
|
props2.put("status", "45");
|
||||||
props2.put("name.desc", "test");
|
props2.put("name.desc", "test");
|
||||||
props2.put("name.id", "123");
|
props2.put("name.id", "123");
|
||||||
props2.put("nodes.time[0]", "123");
|
props2.put("nodes.time[0]", "123");
|
||||||
props2.put("nodes.time[1]", "456");
|
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());
|
Assertions.assertEquals(props1.toString(), props2.toString());
|
||||||
System.out.println("------------------------ 05 ------------------------");
|
System.out.println("------------------------ 05 ------------------------");
|
||||||
|
|||||||
Reference in New Issue
Block a user