-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
aabssmc
committed
Jun 28, 2024
1 parent
7f8a7d9
commit e1fd552
Showing
1 changed file
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package lol.aabss.skhttp.objects; | ||
|
||
import ch.njol.skript.registrations.Classes; | ||
import com.google.gson.*; | ||
|
||
public class Json { | ||
|
||
public Json(JsonElement element){ | ||
this.element = element; | ||
} | ||
|
||
public Json(){ | ||
this(new JsonObject()); | ||
} | ||
|
||
public Json(String key, Object value){ | ||
this(new JsonObject()); | ||
add(key, value); | ||
} | ||
|
||
public Gson gson = new GsonBuilder().setPrettyPrinting().create(); | ||
public JsonElement element; | ||
|
||
public JsonElement add(String key, Object value){ | ||
return add(key, value, true); | ||
} | ||
|
||
public JsonElement add(String key, Object value, boolean skript){ | ||
if (value instanceof Boolean || value instanceof Number || value instanceof String){ | ||
return addProperty(key, value); | ||
} else if (value == null){ | ||
return addProperty(key, null); | ||
} else { | ||
if (Classes.getExactClassInfo(value.getClass()) != null && skript){ | ||
if (element instanceof JsonObject) { | ||
((JsonObject) element).addProperty(key, Classes.toString(value)); | ||
} else if (element instanceof JsonArray) { | ||
((JsonArray) element).add(Classes.toString(value)); | ||
} | ||
} else { | ||
if (element instanceof JsonObject) { | ||
((JsonObject) element).add(key, gson.toJsonTree(value)); | ||
} else if (element instanceof JsonArray) { | ||
((JsonArray) element).add(gson.toJsonTree(value)); | ||
} | ||
} | ||
} | ||
return element; | ||
} | ||
|
||
public JsonElement addProperty(String key, Object value){ | ||
if (value instanceof String){ | ||
if (element instanceof JsonObject){ | ||
((JsonObject) element).addProperty(key, (String) value); | ||
} else if (element instanceof JsonArray){ | ||
((JsonArray) element).add((String) value); | ||
} | ||
} else if (value instanceof Number){ | ||
if (element instanceof JsonObject){ | ||
((JsonObject) element).addProperty(key, (Number) value); | ||
} else if (element instanceof JsonArray){ | ||
((JsonArray) element).add((Number) value); | ||
} | ||
} else if (value instanceof Boolean){ | ||
if (element instanceof JsonObject){ | ||
((JsonObject) element).addProperty(key, (Boolean) value); | ||
} else if (element instanceof JsonArray){ | ||
((JsonArray) element).add((Boolean) value); | ||
} | ||
} else if (value instanceof JsonElement){ | ||
if (element instanceof JsonObject){ | ||
((JsonObject) element).add(key, (JsonElement) value); | ||
} else if (element instanceof JsonArray){ | ||
((JsonArray) element).add((JsonElement) value); | ||
} | ||
} | ||
return element; | ||
} | ||
|
||
public String toString() { | ||
return gson.toJson(element); | ||
} | ||
|
||
} |