11package com .github .kayjamlang .executor .json ;
22
3- import com .github .kayjamlang .core .Argument ;
3+ import com .github .kayjamlang .core .expressions . data . Argument ;
44import com .github .kayjamlang .core .Type ;
5+ import com .github .kayjamlang .executor .Context ;
56import com .github .kayjamlang .executor .Executor ;
67import com .github .kayjamlang .executor .libs .Library ;
78import com .github .kayjamlang .executor .libs .main .ArrayClass ;
@@ -18,17 +19,6 @@ public class JSONLibrary extends Library {
1819
1920 public JSONLibrary () throws Exception {
2021 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" )));
3222 }
3323
3424 public Object decode (Executor executor , Object value ) throws Exception {
@@ -42,6 +32,41 @@ else if(value instanceof Number)
4232 return value ;
4333 }
4434
35+ public Object encodeMap (Executor executor , MapClass object ) throws Exception {
36+ Map <Object , Object > map = object .getMap ((Context ) object .data .get ("ctx" ));
37+
38+ JSONObject jsonObject = new JSONObject ();
39+ for (Map .Entry <Object , Object > entry : map .entrySet ()) {
40+ Object value = entry .getValue ();
41+ if (value instanceof MapClass )
42+ value = encodeMap (executor , (MapClass ) value );
43+ else if (value instanceof ArrayClass )
44+ value = encodeArray (executor , (ArrayClass ) value );
45+
46+ jsonObject .put (entry .getKey ().toString (), value );
47+ }
48+
49+
50+ return jsonObject ;
51+ }
52+
53+ public Object encodeArray (Executor executor , ArrayClass object ) throws Exception {
54+ List <Object > array = object .getVariable ((Context ) object .data .get ("ctx" ), "array" );
55+
56+ JSONArray jsonArray = new JSONArray ();
57+ for (Object value : array ) {
58+ if (value instanceof MapClass )
59+ value = encodeMap (executor , (MapClass ) value );
60+ else if (value instanceof ArrayClass )
61+ value = encodeArray (executor , (ArrayClass ) value );
62+
63+ jsonArray .put (value );
64+ }
65+
66+
67+ return jsonArray ;
68+ }
69+
4570 public Object decodeObject (Executor executor , JSONObject object ) throws Exception {
4671 Map <Object , Object > map = new HashMap <>();
4772 for (String key : object .keySet ())
@@ -64,8 +89,18 @@ public class JSONClass extends LibClass{
6489 public JSONClass () throws Exception {
6590 super ("JSON" , null );
6691 setCompanion (new LibObject ((object -> {
67- object .addFunction (new LibFunction ("decode" , (mainContext , context ) -> {
68- String jsonEncoded = (String ) context .variables .get ("jsonEncoded" );
92+ object .addFunction (new LibFunction ("encode" , Type .STRING , (mainContext , context ) -> {
93+ Object value = context .getVariable ("jsonEncoded" );
94+ if (value instanceof ArrayClass )
95+ return encodeArray (mainContext .executor , (ArrayClass ) value );
96+ else if (value instanceof MapClass )
97+ return encodeMap (mainContext .executor , (MapClass ) value );
98+
99+ return false ;
100+ }, new Argument (Type .ANY , "jsonEncoded" )));
101+
102+ object .addFunction (new LibFunction ("decode" , Type .ANY , (mainContext , context ) -> {
103+ String jsonEncoded = context .getVariable ("jsonEncoded" );
69104 try {
70105 if (jsonEncoded .startsWith ("{" ))
71106 return decodeObject (mainContext .executor , new JSONObject (jsonEncoded ));
0 commit comments