Skip to content

Commit 21d5232

Browse files
committed
🐺📰
1 parent 53e0af4 commit 21d5232

File tree

7 files changed

+108
-27
lines changed

7 files changed

+108
-27
lines changed

build.gradle

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ publishing {
5353
url = layout.buildDirectory.dir('staging-deploy')
5454
}
5555

56-
// maven {
57-
// name = "GithubPackage"
58-
// url = uri("https://maven.pkg.github.com/amarokice/amarokjsonforjava")
59-
// credentials {
60-
// username = "AmarokIce"
61-
// password = System.getenv("GITHUB_TOKEN")
62-
// }
63-
// }
56+
maven {
57+
name = "GithubPackage"
58+
url = uri("https://maven.pkg.github.com/amarokice/amarokjsonforjava")
59+
credentials {
60+
username = "AmarokIce"
61+
password = System.getenv("GITHUB_TOKEN")
62+
}
63+
}
6464
}
6565

6666

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
groupId=club.snowlyicewolf
2-
version=1.7.7
2+
version=1.7.8
33
artifactId=amarok-json-for-java

src/main/java/club/someoneice/json/JSON.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package club.someoneice.json;
22

3-
import club.someoneice.json.node.ArrayNode;
4-
import club.someoneice.json.node.JsonNode;
5-
import club.someoneice.json.node.MapNode;
3+
import club.someoneice.json.api.JsonVar;
4+
import club.someoneice.json.node.*;
65

76
import java.io.File;
87
import java.io.InputStream;
98
import java.lang.reflect.Field;
9+
import java.lang.reflect.Modifier;
1010
import java.util.ArrayList;
1111
import java.util.Collections;
1212
import java.util.HashMap;
@@ -112,7 +112,7 @@ public <T> T tryPullAsClass(Class<? extends T> clazz, File file) throws Instanti
112112
return tryPullAsClass(clazz, jsonMap);
113113
}
114114

115-
public <T> T tryPullAsClass(Class<? extends T> clazz, MapNode jsonMap) throws InstantiationException, IllegalAccessException {
115+
public <T> T tryPullAsClass(Class<T> clazz, MapNode jsonMap) throws InstantiationException, IllegalAccessException {
116116
T targetClass = clazz.newInstance();
117117
Field[] fields = targetClass.getClass().getDeclaredFields();
118118

@@ -125,6 +125,36 @@ public <T> T tryPullAsClass(Class<? extends T> clazz, MapNode jsonMap) throws In
125125
return targetClass;
126126
}
127127

128+
public <T> MapNode tryPushFromClass(T clazz) throws IllegalAccessException {
129+
final MapNode root = new MapNode();
130+
final Field[] fields = clazz.getClass().getDeclaredFields();
131+
for (Field field : fields) {
132+
field.setAccessible(true);
133+
boolean flag = field.isAnnotationPresent(JsonVar.class);
134+
if (!flag && Modifier.isStatic(field.getModifiers())) {
135+
continue;
136+
}
137+
138+
final String name = flag ? field.getAnnotation(JsonVar.class).value() : field.getName();
139+
final Object obj = field.get(clazz);
140+
final String clazzName = obj.getClass().getSimpleName();
141+
final JsonNode<?> node;
142+
if (obj instanceof String) {
143+
node = new StringNode((String) obj);
144+
} else if (obj instanceof Integer) {
145+
node = new IntegerNode((int) obj);
146+
} else if (obj instanceof Number) {
147+
node = new DoubleNode((double) obj);
148+
} else if (obj instanceof Boolean) {
149+
node = new BooleanNode((boolean) obj);
150+
} else continue;
151+
152+
root.put(name, node);
153+
}
154+
155+
return root;
156+
}
157+
128158
public <T> List<T> tryPullAsClassList(Class<? extends T> clazz, String str) throws InstantiationException, IllegalAccessException {
129159
JsonNode<?> jsonNode = this.parse(str);
130160
return createClassList(clazz, jsonNode);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package club.someoneice.json.api;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.FIELD)
10+
public @interface JsonVar {
11+
String value();
12+
}
Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,41 @@
11
package club.someoneice.json.api;
22

3+
import club.someoneice.json.JSON;
34
import club.someoneice.json.node.JsonNode;
5+
import club.someoneice.json.node.MapNode;
46

57
public interface NodeLike {
6-
7-
88
/**
99
* 数据验证。
1010
* @return {@code asJsonNode} 之后获取的数据。
1111
*/
12-
JsonNode.NodeType getType();
12+
default JsonNode.NodeType getType() {
13+
return JsonNode.NodeType.Map;
14+
}
1315

1416
/**
1517
* 保证数据最终能变为 JsonNode。
1618
* @return 为目标类创建的 JsonNode 返回值。
1719
*/
18-
JsonNode<?> asJsonNode();
20+
default JsonNode<?> asJsonNode() {
21+
try {
22+
return JSON.json.tryPushFromClass(this);
23+
} catch (IllegalAccessException e) {
24+
throw new RuntimeException(e);
25+
}
26+
}
27+
28+
/**
29+
* 从 Json 回到 Class
30+
* @return 通过 JsonNode 创建的实例对象。
31+
*/
32+
default <T extends NodeLike> T fromJsonNode(JsonNode<?> node) {
33+
MapNode root = node.asMapNodeOrEmpty();
34+
if (root.isEmpty()) return null;
35+
try {
36+
return (T) this.getClass().cast(JSON.json.tryPullAsClass(this.getClass(), root));
37+
} catch (InstantiationException | IllegalAccessException e) {
38+
throw new RuntimeException(e);
39+
}
40+
}
1941
}

src/main/java/club/someoneice/json/node/ArrayNode.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22

33
import club.someoneice.json.api.TreeNode;
44

5-
import java.util.ArrayList;
6-
import java.util.Arrays;
7-
import java.util.Iterator;
8-
import java.util.List;
5+
import java.util.*;
96
import java.util.stream.Stream;
107

11-
@SuppressWarnings({"rawtypes", "unchecked"})
8+
@SuppressWarnings({"rawtypes", "unchecked", "unused"})
129
public class ArrayNode extends JsonNode<List<JsonNode<?>>> implements Iterable<JsonNode<?>>, TreeNode<JsonNode<?>> {
1310
public ArrayNode(List<JsonNode<?>> obj) {
1411
super(obj);
@@ -20,7 +17,7 @@ public ArrayNode() {
2017

2118
@Override
2219
public List<JsonNode<?>> getObj() {
23-
return (List<JsonNode<?>>) super.getObj();
20+
return super.getObj();
2421
}
2522

2623
@Override
@@ -43,7 +40,7 @@ public void addAll(List<JsonNode<?>> list) {
4340
}
4441

4542
public JsonNode<?> get(int i) {
46-
return (JsonNode<?>) this.obj.get(i);
43+
return this.obj.get(i);
4744
}
4845

4946
public void clear() {
@@ -116,4 +113,14 @@ public ArrayNode add(boolean b) {
116113
this.add(new BooleanNode(b));
117114
return this;
118115
}
116+
117+
public ArrayNode add(List<JsonNode<?>> list) {
118+
this.add(new ArrayNode(list));
119+
return this;
120+
}
121+
122+
public ArrayNode add(Map<String, JsonNode<?>> map) {
123+
this.add(new MapNode(map));
124+
return this;
125+
}
119126
}

src/main/java/club/someoneice/json/node/MapNode.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.util.*;
88
import java.util.stream.Stream;
99

10-
@SuppressWarnings({"rawtypes", "unchecked"})
10+
@SuppressWarnings({"rawtypes", "unchecked", "unused"})
1111
public class MapNode extends JsonNode<Map<String, JsonNode<?>>> implements Iterable<Pair<String, JsonNode<?>>>, TreeNode<Pair<String, JsonNode<?>>> {
1212
public MapNode(Map<String, JsonNode<?>> obj) {
1313
super(obj);
@@ -24,7 +24,7 @@ public NodeType getType() {
2424

2525
@Override
2626
public Map<String, JsonNode<?>> getObj() {
27-
return (Map<String, JsonNode<?>>) super.getObj();
27+
return super.getObj();
2828
}
2929

3030
@Override
@@ -37,7 +37,7 @@ public void addAll(MapNode mapNode) {
3737
}
3838

3939
public JsonNode<?> get(String key) {
40-
return (JsonNode<?>) this.obj.get(key);
40+
return this.obj.get(key);
4141
}
4242

4343
public JsonNode<?> put(String key, JsonNode<?> value) {
@@ -144,4 +144,14 @@ public MapNode put(String name, boolean b) {
144144
this.put(name, new BooleanNode(b));
145145
return this;
146146
}
147+
148+
public MapNode put(String name, List<JsonNode<?>> list) {
149+
this.put(name, new ArrayNode(list));
150+
return this;
151+
}
152+
153+
public MapNode put(String name, Map<String, JsonNode<?>> map) {
154+
this.put(name, new MapNode(map));
155+
return this;
156+
}
147157
}

0 commit comments

Comments
 (0)