Skip to content

Commit 1845f06

Browse files
committed
feat: json
1 parent 3b16782 commit 1845f06

6 files changed

Lines changed: 60 additions & 2 deletions

File tree

‎pom.xml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
<artifactId>schematic4j</artifactId>
5252
<version>1.1.1</version>
5353
</dependency>
54+
<dependency>
55+
<groupId>org.json</groupId>
56+
<artifactId>json</artifactId>
57+
<version>20260522</version>
58+
</dependency>
5459
<dependency>
5560
<groupId>me.clip</groupId>
5661
<artifactId>placeholderapi</artifactId>
@@ -171,6 +176,10 @@
171176
<pattern>net.sandrohc.schematic4j</pattern>
172177
<shadedPattern>${plugin.pckg}.lib.schematic4j</shadedPattern>
173178
</relocation>
179+
<relocation>
180+
<pattern>org.json</pattern>
181+
<shadedPattern>${plugin.pckg}.lib.json</shadedPattern>
182+
</relocation>
174183
</relocations>
175184
<filters>
176185
<filter>

‎src/main/java/xyz/webmc/wlib/WLIBBukkitPlugin.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package xyz.webmc.wlib;
22

33
import xyz.webmc.wlib.api.WLIB;
4+
import xyz.webmc.wlib.api.misc.CaptureSender;
45
import xyz.webmc.wlib.api.util.CommandUtil;
56
import xyz.webmc.wlib.api.util.EventUtil;
67
import xyz.webmc.wlib.api.util.PermissionUtil;
@@ -17,6 +18,7 @@
1718
import net.sandrohc.schematic4j.SchematicLoader;
1819
import org.apache.logging.log4j.Level;
1920
import org.apache.logging.log4j.core.config.Configurator;
21+
import org.bukkit.ChatColor;
2022
import org.bukkit.command.CommandSender;
2123
import org.bukkit.command.ConsoleCommandSender;
2224
import org.bukkit.event.EventHandler;
@@ -102,15 +104,15 @@ private static boolean handleCommandEvent(final CommandSender sender, final Stri
102104
MirrorSafe.invokeMethod(TextUtil.class, "sendStringListMessageType" + type, sender, name, plugins);
103105
});
104106
}
105-
} /* else {
107+
} else {
106108
final CaptureSender capture = new CaptureSender(sender);
107109
CommandUtil.dispatch(capture, commandStr);
108110
final String[] msg = capture.getMessages().get(0).split(": ");
109111
sender.sendMessage(ChatColor.GOLD + "Bukkit " + msg[0] + ":");
110112
sender.sendMessage(ChatColor.DARK_GRAY + " - " + msg[1]);
111113
TextUtil.sendStringListMessageType3(sender, name, plugins);
112114
return true;
113-
} */
115+
}
114116
}
115117
}
116118

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package xyz.webmc.wlib.api.interfaces;
2+
3+
public interface Deserializable<T> {
4+
T deserialize(String data);
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package xyz.webmc.wlib.api.interfaces;
2+
3+
public interface Serializable {
4+
String serialize();
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package xyz.webmc.wlib.api.interfaces;
2+
3+
public interface SerializableDeserializable<T> extends Serializable, Deserializable<T> {
4+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package xyz.webmc.wlib.api.util;
2+
3+
import org.json.JSONArray;
4+
import org.json.JSONObject;
5+
import org.json.JSONTokener;
6+
7+
public final class JSONUtil {
8+
public static final String serialize(final Object obj) {
9+
validateObject(obj);
10+
return JSONObject.valueToString(obj);
11+
}
12+
13+
public static final Object deserialize(final String json) {
14+
final Object ret = new JSONTokener(json).nextValue();
15+
validateObject(ret);
16+
return ret;
17+
}
18+
19+
public static final boolean isJSON(final String json) {
20+
try {
21+
deserialize(json);
22+
return true;
23+
} catch (final Exception ex) {
24+
return false;
25+
}
26+
}
27+
28+
private static void validateObject(final Object obj) throws IllegalArgumentException {
29+
if (!(obj instanceof JSONObject || obj instanceof JSONArray)) {
30+
throw new IllegalArgumentException();
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)