Skip to content

Commit f885475

Browse files
committed
Добавлен модуль json
1 parent ca2462d commit f885475

File tree

9 files changed

+235
-9
lines changed

9 files changed

+235
-9
lines changed

examples/network/github_timeline.own

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use "std"
2+
use "http"
3+
use "json"
4+
use "functional"
5+
6+
header = "* Prints current GitHub timeline *"
7+
println "*" * length(header)
8+
println header
9+
println "*" * length(header)
10+
11+
// Executes in main thread
12+
//http("https://api.github.com/events", def(r) {
13+
// foreach(jsondecode(r), ::show_github_events)
14+
//})
15+
16+
// Executes in new thread
17+
thread(::http, "https://api.github.com/events", def(r) {
18+
foreach(jsondecode(r), ::show_github_events)
19+
})
20+
21+
def show_github_events(event) {
22+
println event["created_at"]
23+
actor = event["actor"]
24+
println "User: https://github.com/" + actor["login"]
25+
println github_event_type(event)
26+
println "-" * 50
27+
}
28+
29+
def github_event_type(event) {
30+
type = event["type"]
31+
repo = event["repo"]
32+
repo = "https://github.com/" + repo["name"]
33+
payload = event["payload"]
34+
35+
if (type == "CommitCommentEvent") {
36+
comment = payload["comment"]
37+
return "commented commit in " + repo + "\n" + comment["body"]
38+
}
39+
if (type == "CreateEvent") {
40+
return "created " + payload["ref_type"] + " on " + repo
41+
}
42+
if (type == "DeleteEvent") {
43+
return "deleted " + payload["ref_type"] + " on " + repo
44+
}
45+
if (type == "ForkEvent") {
46+
return "forked repository " + repo
47+
}
48+
if (type == "IssueCommentEvent") {
49+
comment = payload["comment"]
50+
issue = payload["issue"]
51+
return "commented issue " + issue["title"] + " on " + repo + "\n" + comment["body"]
52+
}
53+
if (type == "IssuesEvent") {
54+
issue = payload["issue"]
55+
return payload["action"] + " issue '" + issue["title"] + "' on " + repo
56+
}
57+
if (type == "PullRequestEvent") {
58+
pr = payload["pull_request"]
59+
return payload["action"] + " pull request #" + payload["number"] + " '" + pr["title"] + "' on " + repo
60+
}
61+
if (type == "PushEvent") {
62+
return "pushed " + length(payload["commits"]) + " commits to " + repo
63+
}
64+
if (type == "WatchEvent") {
65+
return "start watching repository " + repo
66+
}
67+
return type + " on " + repo
68+
}

libs/json-20151123.jar

47.1 KB
Binary file not shown.

nbproject/build-impl.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ is divided into following sections:
222222
<condition else="" property="testng.debug.mode" value="-mixed">
223223
<istrue value="${junit+testng.available}"/>
224224
</condition>
225+
<property name="java.failonerror" value="true"/>
225226
</target>
226227
<target name="-post-init">
227228
<!-- Empty placeholder for easier customization. -->
@@ -698,7 +699,7 @@ is divided into following sections:
698699
<sequential>
699700
<property environment="env"/>
700701
<resolve name="profiler.current.path" value="${profiler.info.pathvar}"/>
701-
<java classname="@{classname}" dir="${profiler.info.dir}" fork="true" jvm="${profiler.info.jvm}">
702+
<java classname="@{classname}" dir="${profiler.info.dir}" failonerror="${java.failonerror}" fork="true" jvm="${profiler.info.jvm}">
702703
<jvmarg line="${endorsed.classpath.cmd.line.arg}"/>
703704
<jvmarg value="${profiler.info.jvmargs.agent}"/>
704705
<jvmarg line="${profiler.info.jvmargs}"/>
@@ -773,7 +774,7 @@ is divided into following sections:
773774
<attribute default="${debug.classpath}" name="classpath"/>
774775
<element name="customize" optional="true"/>
775776
<sequential>
776-
<java classname="@{classname}" dir="${work.dir}" fork="true">
777+
<java classname="@{classname}" dir="${work.dir}" failonerror="${java.failonerror}" fork="true">
777778
<jvmarg line="${endorsed.classpath.cmd.line.arg}"/>
778779
<jvmarg line="${debug-args-line}"/>
779780
<jvmarg value="-Xrunjdwp:transport=${debug-transport},address=${jpda.address}"/>
@@ -800,7 +801,7 @@ is divided into following sections:
800801
<attribute default="jvm" name="jvm"/>
801802
<element name="customize" optional="true"/>
802803
<sequential>
803-
<java classname="@{classname}" dir="${work.dir}" fork="true">
804+
<java classname="@{classname}" dir="${work.dir}" failonerror="${java.failonerror}" fork="true">
804805
<jvmarg line="${endorsed.classpath.cmd.line.arg}"/>
805806
<jvmarg value="-Dfile.encoding=${runtime.encoding}"/>
806807
<redirector errorencoding="${runtime.encoding}" inputencoding="${runtime.encoding}" outputencoding="${runtime.encoding}"/>

nbproject/genfiles.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ [email protected]
44
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
55
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
66
nbproject/build-impl.xml.data.CRC32=48c1b1a1
7-
nbproject/build-impl.xml.script.CRC32=e3eeb3f1
8-
nbproject/build-impl.xml.stylesheet.CRC32=2d327b5d@1.78.0.48
7+
nbproject/build-impl.xml.script.CRC32=b8cd7788
8+
nbproject/build-impl.xml.stylesheet.CRC32=05530350@1.79.0.48

nbproject/project.properties

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
annotation.processing.enabled=true
22
annotation.processing.enabled.in.editor=false
3-
annotation.processing.processor.options=
43
annotation.processing.processors.list=
54
annotation.processing.run.all.processors=true
65
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
6+
application.title=OwnLang
7+
application.vendor=aNNiMON
78
build.classes.dir=${build.dir}/classes
89
build.classes.excludes=**/*.java,**/*.form
910
# This directory is removed when the project is cleaned:
@@ -26,10 +27,21 @@ dist.archive.excludes=
2627
dist.dir=dist
2728
dist.jar=${dist.dir}/OwnLang.jar
2829
dist.javadoc.dir=${dist.dir}/javadoc
30+
endorsed.classpath=
2931
excludes=
32+
file.reference.commons-codec-1.6.jar=libs/commons-codec-1.6.jar
33+
file.reference.commons-logging-1.1.3.jar=libs/commons-logging-1.1.3.jar
34+
file.reference.httpclient-4.3.5.jar=libs/httpclient-4.3.5.jar
35+
file.reference.httpcore-4.3.2.jar=libs/httpcore-4.3.2.jar
36+
file.reference.json-20151123.jar=libs/json-20151123.jar
3037
includes=**
3138
jar.compress=false
32-
javac.classpath=
39+
javac.classpath=\
40+
${file.reference.commons-codec-1.6.jar}:\
41+
${file.reference.commons-logging-1.1.3.jar}:\
42+
${file.reference.httpclient-4.3.5.jar}:\
43+
${file.reference.httpcore-4.3.2.jar}:\
44+
${file.reference.json-20151123.jar}
3345
# Space-separated list of extra javac options
3446
javac.compilerargs=
3547
javac.deprecation=false

program.own

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,25 @@ println "Sum: " + reduce(squares, 0, def(x, y) = x + y)
128128

129129
use "http"
130130

131-
http("http://jsonplaceholder.typicode.com/users", "POST", {"name": "OwnLang", "versionCode": 10}, def(v) {
131+
/*http("http://jsonplaceholder.typicode.com/users", "POST", {"name": "OwnLang", "versionCode": 10}, def(v) {
132132
println "Added: " + v
133133
http("http://jsonplaceholder.typicode.com/users/2", "PATCH", {"name": "Patched Name"}, ::http_get_demo)
134134
})
135135
def http_get_demo(v) {
136136
println "Updated: " + v
137137
http("http://jsonplaceholder.typicode.com/users", ::echo)
138-
}
138+
}*/
139+
140+
use "json"
141+
println "json"
142+
println jsonencode({
143+
"name": "JSON Example",
144+
"version": 1,
145+
"arrayData": [
146+
1, 2, 3, 4
147+
],
148+
"objectData": {
149+
"key": "value",
150+
10: "1000"
151+
}
152+
})
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.annimon.ownlang.lib.modules.functions;
2+
3+
import com.annimon.ownlang.lib.*;
4+
import java.util.Iterator;
5+
import org.json.*;
6+
7+
public final class json_decode implements Function {
8+
9+
@Override
10+
public Value execute(Value... args) {
11+
if (args.length != 1) throw new RuntimeException("One argument expected");
12+
try {
13+
final String jsonRaw = args[0].asString();
14+
final Object root = new JSONTokener(jsonRaw).nextValue();
15+
return process(root);
16+
} catch (JSONException ex) {
17+
throw new RuntimeException("Error while parsing json", ex);
18+
}
19+
}
20+
21+
private Value process(Object obj) {
22+
if (obj instanceof JSONObject) {
23+
return process((JSONObject) obj);
24+
}
25+
if (obj instanceof JSONArray) {
26+
return process((JSONArray) obj);
27+
}
28+
if (obj instanceof String) {
29+
return new StringValue((String) obj);
30+
}
31+
if (obj instanceof Number) {
32+
return new NumberValue(((Number) obj).doubleValue());
33+
}
34+
if (obj instanceof Boolean) {
35+
return NumberValue.fromBoolean((Boolean) obj);
36+
}
37+
// NULL or other
38+
return NumberValue.ZERO;
39+
}
40+
41+
private MapValue process(JSONObject json) {
42+
final MapValue result = new MapValue(json.length());
43+
final Iterator<String> it = json.keys();
44+
while(it.hasNext()) {
45+
final String key = it.next();
46+
final Value value = process(json.get(key));
47+
result.set(new StringValue(key), value);
48+
}
49+
return result;
50+
}
51+
52+
private ArrayValue process(JSONArray json) {
53+
final int length = json.length();
54+
final ArrayValue result = new ArrayValue(length);
55+
for (int i = 0; i < length; i++) {
56+
final Value value = process(json.get(i));
57+
result.set(i, value);
58+
}
59+
return result;
60+
}
61+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.annimon.ownlang.lib.modules.functions;
2+
3+
import com.annimon.ownlang.lib.*;
4+
import java.util.Map;
5+
import org.json.*;
6+
7+
public final class json_encode implements Function {
8+
9+
@Override
10+
public Value execute(Value... args) {
11+
if (args.length != 1) throw new RuntimeException("One argument expected");
12+
try {
13+
final Object root = process(args[0]);
14+
final String jsonRaw = JSONObject.valueToString(root);
15+
return new StringValue(jsonRaw);
16+
} catch (JSONException ex) {
17+
throw new RuntimeException("Error while creating json", ex);
18+
}
19+
}
20+
21+
private Object process(Value val) {
22+
switch (val.type()) {
23+
case Types.ARRAY:
24+
return process((ArrayValue) val);
25+
case Types.MAP:
26+
return process((MapValue) val);
27+
case Types.NUMBER:
28+
return val.asNumber();
29+
case Types.STRING:
30+
return val.asString();
31+
default:
32+
return JSONObject.NULL;
33+
}
34+
}
35+
36+
private Object process(MapValue map) {
37+
final JSONObject result = new JSONObject();
38+
for (Map.Entry<Value, Value> entry : map) {
39+
final String key = entry.getKey().asString();
40+
final Object value = process(entry.getValue());
41+
result.put(key, value);
42+
}
43+
return result;
44+
}
45+
46+
private Object process(ArrayValue array) {
47+
final JSONArray result = new JSONArray();
48+
for (Value value : array) {
49+
result.put(process(value));
50+
}
51+
return result;
52+
}
53+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.annimon.ownlang.lib.modules;
2+
3+
import com.annimon.ownlang.lib.*;
4+
import com.annimon.ownlang.lib.modules.functions.*;
5+
6+
/**
7+
*
8+
* @author aNNiMON
9+
*/
10+
public final class json implements Module {
11+
12+
@Override
13+
public void init() {
14+
Functions.set("jsonencode", new json_encode());
15+
Functions.set("jsondecode", new json_decode());
16+
}
17+
}

0 commit comments

Comments
 (0)