Skip to content

Commit

Permalink
🐺🛠️
Browse files Browse the repository at this point in the history
  • Loading branch information
AmarokIce committed Sep 15, 2024
1 parent 4111303 commit 82490e5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
4 changes: 1 addition & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ plugins {
}

group 'club.someoneice.json'
version '1.7.1'
version '1.7.2'

repositories {
mavenLocal()
mavenCentral()
}

dependencies {
// annotationProcessor("org.jetbrains:annotations:24.1.0")

testImplementation("com.google.guava:guava:33.0.0-jre")
testImplementation("com.google.code.gson:gson:2.11.0")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

import club.someoneice.json.node.JsonNode;

public interface JsonLike {
public interface NodeLike {


/**
* 数据验证。
* @return {@code asJsonNode} 之后获取的数据。
*/
JsonNode.NodeType getType();

/**
* 保证数据最终能变为 JsonNode。
* @return 为目标类创建的 JsonNode 返回值。
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/club/someoneice/json/node/JsonNode.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package club.someoneice.json.node;

import club.someoneice.json.api.JsonLike;
import club.someoneice.json.api.NodeLike;
import club.someoneice.json.api.exception.NodeCastException;

import java.util.ArrayList;
Expand All @@ -10,7 +10,7 @@
import java.util.function.Supplier;

@SuppressWarnings({"unused", "rawtypes", "unchecked"})
public class JsonNode<T> implements JsonLike {
public class JsonNode<T> implements NodeLike {
protected final T obj;

public JsonNode(T obj) {
Expand Down Expand Up @@ -155,6 +155,10 @@ public boolean isNull() {
return this.typeOf(NodeType.Null) || this == NullNode.INSTANCE;
}

public boolean nonNull() {
return !isNull();
}

/**
* @return JsonNode 持有的真实数据。
*/
Expand All @@ -178,7 +182,16 @@ public int hashCode() {
}

@Override
public JsonNode<?> asJsonNode() {
public JsonNode asJsonNode() {
return this.asTypeNode();
}

public static JsonNode asJsonNodeOrEmpty(Object obj) {
if (obj instanceof NodeLike) {
return ((NodeLike) obj).asJsonNode();
}

JsonNode node = new JsonNode(obj);
return node.getType() == NodeType.Other ? NullNode.INSTANCE : node;
}
}
28 changes: 23 additions & 5 deletions src/main/java/club/someoneice/json/node/MapNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import club.someoneice.json.PairList;
import club.someoneice.json.api.TreeNode;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.*;
import java.util.stream.Stream;

@SuppressWarnings({"rawtypes", "unchecked"})
Expand All @@ -17,7 +14,7 @@ public MapNode(Map<String, JsonNode<?>> obj) {
}

public MapNode() {
super(new HashMap());
super(new LinkedHashMap<>());
}

@Override
Expand Down Expand Up @@ -96,4 +93,25 @@ public MapNode copy(Map<String, JsonNode<?>> map) {
map.putAll(this.obj);
return new MapNode(map);
}

public static MapNode fromNodeMap(Map<String, JsonNode<?>> map) {
return new MapNode(map);
}

public static MapNode fromMapNonnull(Map<String, Object> map) {
MapNode node = new MapNode();
map.entrySet().stream()
.map(it -> new Pair<>(it.getKey(), JsonNode.asJsonNodeOrEmpty(it.getValue())))
.filter(it -> it.getValue().nonNull())
.forEach(it -> node.put(it.getKey(), it.getValue()));
return node;
}

public static MapNode fromMap(Map<String, Object> map) {
MapNode node = new MapNode();
map.entrySet().stream()
.map(it -> new Pair<>(it.getKey(), JsonNode.asJsonNodeOrEmpty(it.getValue())))
.forEach(it -> node.put(it.getKey(), it.getValue()));
return node;
}
}

0 comments on commit 82490e5

Please sign in to comment.