Skip to content

Commit ea216c4

Browse files
committed
Initial commit
0 parents  commit ea216c4

File tree

6 files changed

+803
-0
lines changed

6 files changed

+803
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.gradle
2+
/build/
3+
4+
# Ignore Gradle GUI config
5+
gradle-app.setting
6+
7+
# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
8+
!gradle-wrapper.jar
9+
10+
# Cache of project
11+
.gradletasknamecache
12+
13+
# # Work around https://youtrack.jetbrains.com/issue/IDEA-116898
14+
# gradle/wrapper/gradle-wrapper.properties

LICENSE

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# executor-json-library
2+
Simple json library for KayJam Executor

build.gradle

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
plugins {
2+
id 'java'
3+
}
4+
5+
group 'com.github.kayjamlang'
6+
version '0.1'
7+
8+
repositories {
9+
mavenCentral()
10+
maven {
11+
url "https://oss.sonatype.org/service/local/repositories/releases/content/"
12+
}
13+
}
14+
15+
jar {
16+
manifest {
17+
attributes(
18+
'Main-Class': 'com.github.kayjamlang.executor.cli.Main'
19+
)
20+
}
21+
from {
22+
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
23+
}
24+
}
25+
26+
dependencies {
27+
compile group: 'org.json', name: 'json', version: '20210307'
28+
compileOnly group: 'com.github.kayjamlang', name: 'executor', version: '0.1.3.17-fix4'
29+
testCompile group: 'junit', name: 'junit', version: '4.12'
30+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)