Skip to content

Commit

Permalink
- Kleenen initial #36
Browse files Browse the repository at this point in the history
- Add count of object/phrase in the json
- Add json size of current element
  • Loading branch information
cooffeeRequired committed Mar 10, 2023
1 parent 81f2084 commit 42669bf
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 98 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,4 @@ replay_pid*
*.lock
*.bin
build.___gradle
build.___gradle
*.lang
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ apply plugin: 'com.github.johnrengelman.shadow'

group = 'cz.coffee'
version = '2.8.0'
def testVersion = 'latest'


repositories {
Expand Down
65 changes: 34 additions & 31 deletions src/main/java/cz/coffee/core/ColoredJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,52 @@
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;

@SuppressWarnings("unused")
public class ColoredJson {
final static String GOLD = "§6";
final static String YELLOW = "§e";
final static String GRAY = "§7";
final static String DARK_GRAY = "§8";
final static String BLACK = "§0";
final static String RED = "§c";
final static String DARK_RED = "§4";
final static String GREEN = "§a";
final static String DARK_GREEN = "§2";
final static String AQUA = "§b";
final static String DARK_AQUA = "§3";
final static String BLUE = "§9";
final static String DARK_BLUE = "§1";
final static String LIGHT_PURPLE = "§d";
final static String DARK_PURPLE = "§5";
final static String WHITE = "§f";
final static String RESET = "§r";
/**
* This file is part of skJson.
* <p>
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* <p>
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <<a href="http://www.gnu.org/licenses/">...</a>>.
* <p>
* Copyright coffeeRequired nd contributors
* <p>
* Created: Saturday (3/10/2023)
*/

public class ColoredJson {
final static Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().serializeNulls().create();

private String finalOutput;

public ColoredJson(JsonElement input) {
process(input);
}

private void process(JsonElement input) {
if (input == null) return;
String jsonString = gson.toJson(input);
finalOutput = Util.color(
jsonString
.replaceAll("([+]?([0-9]*[.])?[0-9]+)", AQUA+"$1"+RESET)
.replaceAll("true", GREEN+"$0"+RESET)
.replaceAll("false", RED+"$0"+RESET)
.replaceAll("null", LIGHT_PURPLE+"$0"+RESET)
.replaceAll("[{}]", DARK_GREEN+"$0"+RESET)
.replaceAll("[\\[\\]]", GOLD+"$0"+RESET)
.replaceAll("(\")(.)", DARK_GRAY+"$1"+RESET+GRAY+"$2"+RESET)
if (input == null) {
return;
}
StringBuilder jsonString = new StringBuilder(gson.toJson(input));
finalOutput = Util.color("&f"+
new String(jsonString)
.replaceAll("(?i:true)", "&2$0&f")
.replaceAll("(?i:false)", "&c$0&f")
.replaceAll("(?i:null)", "&d$0&f")
.replaceAll("(?<=\\W)([+]?([0-9]*[.])?[0-9]+)", "&b$1&f")
.replaceAll("(\")(.)", "&8$1&f$2&f")
.replaceAll("([{}])|([\\[\\]])", "&7$1&r&e$2&f")
);
}


public String getOutput() {
return finalOutput;
}
Expand Down
50 changes: 46 additions & 4 deletions src/main/java/cz/coffee/core/JsonUtils.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package cz.coffee.core;

import com.google.gson.*;
import org.jetbrains.annotations.NotNull;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.ExecutionException;

import static cz.coffee.core.AdapterUtils.parseItem;
import static cz.coffee.core.NumberUtils.parsedNumber;
Expand Down Expand Up @@ -57,7 +58,48 @@ public static JsonElement convert(Object object) {
return null;
}

public static void changeKey(JsonElement obj, LinkedList<String> keys, String newKey) throws ExecutionException, InterruptedException {
public static int countKeys(@NotNull String key, @NotNull JsonElement json) {
int count = 0;
JsonElement value;
Deque<JsonElement> elements = new ConcurrentLinkedDeque<>();
elements.add(json);

while ((value = elements.pollFirst()) != null) {
if (value instanceof JsonArray) {
for (JsonElement l : value.getAsJsonArray()) elements.offerLast(l);
} else if (value instanceof JsonObject) {
for (Map.Entry<String, JsonElement> entry : value.getAsJsonObject().entrySet()) {
if (entry.getKey().equals(key)) count++;
if (!entry.getValue().isJsonPrimitive()) elements.offerLast(entry.getValue());
}
}
}
return count;
}
public static int countValues(@NotNull JsonElement value, @NotNull JsonElement json) {
int count = 0;
JsonElement jsonElement;
Deque<JsonElement> elements = new ConcurrentLinkedDeque<>();
elements.add(json);

while ((jsonElement = elements.pollFirst()) != null) {
if (jsonElement instanceof JsonArray) {
for (JsonElement l : jsonElement.getAsJsonArray()) elements.offerLast(l);
} else if (jsonElement instanceof JsonObject) {
for (Map.Entry<String, JsonElement> entry : jsonElement.getAsJsonObject().entrySet()) {
if (entry.getValue().equals(value)) count++;
if (!entry.getValue().isJsonPrimitive()) elements.offerLast(entry.getValue());
}
}
}
return count;
}





public static void changeKey(JsonElement obj, LinkedList<String> keys, String newKey) {
Deque<JsonElement> currentElements = new ConcurrentLinkedDeque<>();
currentElements.offerLast(obj);

Expand Down Expand Up @@ -95,7 +137,7 @@ public static void changeKey(JsonElement obj, LinkedList<String> keys, String ne
}
}

public static void addValue(JsonElement obj, LinkedList<String> keys, Object value) throws ExecutionException, InterruptedException {
public static void addValue(JsonElement obj, LinkedList<String> keys, Object value) {
Deque<JsonElement> currentElements = new ConcurrentLinkedDeque<>();
currentElements.offerLast(obj);

Expand Down Expand Up @@ -217,7 +259,7 @@ public static void removeByKey(JsonElement obj, LinkedList<String> keys) {



public static void changeValue(JsonElement obj, LinkedList<String> keys, Object value) throws ExecutionException, InterruptedException {
public static void changeValue(JsonElement obj, LinkedList<String> keys, Object value) {
Deque<JsonElement> currentElements = new ConcurrentLinkedDeque<>();
currentElements.offerLast(obj);

Expand Down
89 changes: 89 additions & 0 deletions src/main/java/cz/coffee/skript/expressions/JsonCountOf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package cz.coffee.skript.expressions;

import ch.njol.skript.Skript;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.util.LiteralUtils;
import ch.njol.util.Kleenean;
import com.google.gson.JsonElement;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.NotNull;

import static cz.coffee.core.AdapterUtils.parseItem;
import static cz.coffee.core.JsonUtils.countKeys;
import static cz.coffee.core.JsonUtils.countValues;

/**
* This file is part of skJson.
* <p>
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* <p>
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <<a href="http://www.gnu.org/licenses/">...</a>>.
* <p>
* Copyright coffeeRequired nd contributors
* <p>
* Created: Friday (3/10/2023)
*/
public class JsonCountOf extends SimpleExpression<Integer> {

static {
Skript.registerExpression(JsonCountOf.class, Integer.class, ExpressionType.SIMPLE, "(count|number) of (:key|[value]) %object% in %json%");
}

private boolean isValue;
private Expression<?> valueExpression;
private Expression<JsonElement> jsonElementExpression;

@Override
protected @Nullable Integer @NotNull [] get(@NotNull Event e) {
JsonElement json = jsonElementExpression.getSingle(e);
Object unparsedValue = valueExpression.getSingle(e);
assert json != null;
if (isValue) {
JsonElement parsed = parseItem(unparsedValue, valueExpression, e);
assert parsed != null;
return new Integer[]{countValues(parsed, json)};
} else {
if (unparsedValue instanceof String) {
return new Integer[]{countKeys((String) unparsedValue, json)};
}
}
return new Integer[0];
}

@Override
public boolean isSingle() {
return true;
}

@Override
public @NotNull Class<? extends Integer> getReturnType() {
return Integer.class;
}

@Override
public @NotNull String toString(@Nullable Event e, boolean debug) {
return "count of " + (isValue ? valueExpression.toString(e, debug) : "key " + valueExpression.toString(e, debug)) + " in " + jsonElementExpression.toString(e, debug);
}

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?> @NotNull [] exprs, int matchedPattern, @NotNull Kleenean isDelayed, @NotNull ParseResult parseResult) {
isValue = !parseResult.hasTag("key");
jsonElementExpression = (Expression<JsonElement>) exprs[1];
valueExpression = LiteralUtils.defendExpression(exprs[0]);
return LiteralUtils.canInitSafely(valueExpression);
}
}
49 changes: 49 additions & 0 deletions src/main/java/cz/coffee/skript/expressions/JsonSize.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cz.coffee.skript.expressions;

import ch.njol.skript.expressions.base.SimplePropertyExpression;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.NotNull;

/**
* This file is part of skJson.
* <p>
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* <p>
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <<a href="http://www.gnu.org/licenses/">...</a>>.
* <p>
* Copyright coffeeRequired nd contributors
* <p>
* Created: Friday (3/10/2023)
*/
public class JsonSize extends SimplePropertyExpression<JsonElement, Integer> {

static {
register(JsonSize.class, Integer.class, "size", "jsons");
}
@Override
protected @NotNull String getPropertyName() {
return "json size";
}

@Override
public @Nullable Integer convert(JsonElement jsonElement) {
return jsonElement.isJsonArray() ? ((JsonArray) jsonElement).size() : ((JsonObject) jsonElement).size();
}

@Override
public @NotNull Class<? extends Integer> getReturnType() {
return Integer.class;
}
}
Loading

0 comments on commit 42669bf

Please sign in to comment.