|
| 1 | +package com.github.kayjamlang.executor.json; |
| 2 | + |
| 3 | +import com.github.kayjamlang.core.Argument; |
| 4 | +import com.github.kayjamlang.core.Type; |
| 5 | +import com.github.kayjamlang.executor.Executor; |
| 6 | +import com.github.kayjamlang.executor.libs.Library; |
| 7 | +import com.github.kayjamlang.executor.libs.main.ArrayClass; |
| 8 | +import com.github.kayjamlang.executor.libs.main.MapClass; |
| 9 | +import org.json.JSONArray; |
| 10 | +import org.json.JSONObject; |
| 11 | + |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.LinkedList; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | + |
| 17 | +public class JSONLibrary extends Library { |
| 18 | + |
| 19 | + public JSONLibrary() throws Exception { |
| 20 | + classes.put("JSON", new JSONClass()); |
| 21 | + addFunction(new LibFunction("jsonDecode", (mainContext, context) -> { |
| 22 | + String jsonEncoded = (String) context.variables.get("jsonEncoded"); |
| 23 | + try { |
| 24 | + if(jsonEncoded.startsWith("{")) |
| 25 | + return decodeObject(mainContext.executor, new JSONObject(jsonEncoded)); |
| 26 | + return decodeArray(mainContext.executor, new JSONArray(jsonEncoded)); |
| 27 | + }catch (Exception e){ |
| 28 | + e.printStackTrace(); |
| 29 | + return false; |
| 30 | + } |
| 31 | + }, new Argument(Type.STRING, "jsonEncoded"))); |
| 32 | + } |
| 33 | + |
| 34 | + public Object decode(Executor executor, Object value) throws Exception { |
| 35 | + if(value instanceof JSONArray) |
| 36 | + return decodeArray(executor, (JSONArray) value); |
| 37 | + else if(value instanceof JSONObject) |
| 38 | + return decodeObject(executor, (JSONObject) value); |
| 39 | + else if(value instanceof Number) |
| 40 | + return ((Number) value).longValue(); |
| 41 | + |
| 42 | + return value; |
| 43 | + } |
| 44 | + |
| 45 | + public Object decodeObject(Executor executor, JSONObject object) throws Exception { |
| 46 | + Map<Object, Object> map = new HashMap<>(); |
| 47 | + for(String key: object.keySet()) |
| 48 | + map.put(key, decode(executor, object.get(key))); |
| 49 | + |
| 50 | + return MapClass.create(executor, map); |
| 51 | + } |
| 52 | + |
| 53 | + public ArrayClass decodeArray(Executor executor, JSONArray array) throws Exception { |
| 54 | + List<Object> list = new LinkedList<>(); |
| 55 | + for (int i = 0; i < array.length(); i++) { |
| 56 | + list.add(decode(executor, array.get(i))); |
| 57 | + } |
| 58 | + |
| 59 | + return ArrayClass.create(executor, list); |
| 60 | + } |
| 61 | + |
| 62 | + public class JSONClass extends LibClass{ |
| 63 | + |
| 64 | + public JSONClass() throws Exception { |
| 65 | + super("JSON", null); |
| 66 | + setCompanion(new LibObject((object -> { |
| 67 | + object.addFunction(new LibFunction("decode", (mainContext, context) -> { |
| 68 | + String jsonEncoded = (String) context.variables.get("jsonEncoded"); |
| 69 | + try { |
| 70 | + if(jsonEncoded.startsWith("{")) |
| 71 | + return decodeObject(mainContext.executor, new JSONObject(jsonEncoded)); |
| 72 | + return decodeArray(mainContext.executor, new JSONArray(jsonEncoded)); |
| 73 | + }catch (Exception e){ |
| 74 | + e.printStackTrace(); |
| 75 | + return false; |
| 76 | + } |
| 77 | + }, new Argument(Type.STRING, "jsonEncoded"))); |
| 78 | + }))); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments