Skip to content

Commit da36a34

Browse files
committed
Merge socket-module into latest
2 parents c9249e8 + fcd509e commit da36a34

File tree

6 files changed

+341
-83
lines changed

6 files changed

+341
-83
lines changed

build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ task proguard(dependsOn: dist, type: proguard.gradle.ProGuardTask) {
5353
}
5454

5555
dependencies {
56-
compile 'com.squareup.okhttp3:okhttp:3.4.1'
56+
compile ('io.socket:socket.io-client:0.7.0') {
57+
exclude group: 'org.json', module: 'json'
58+
}
5759
compile 'org.json:json:20160212'
5860

5961
testCompile 'junit:junit:4.12'

src/main/java/com/annimon/ownlang/lib/MapValue.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.Iterator;
66
import java.util.Map;
77
import java.util.Objects;
8+
import java.util.function.Consumer;
89

910
/**
1011
*
@@ -31,6 +32,18 @@ public MapValue(Map<Value, Value> map) {
3132
this.map = map;
3233
}
3334

35+
public boolean ifPresent(String key, Consumer<Value> consumer) {
36+
return ifPresent(new StringValue(key), consumer);
37+
}
38+
39+
public boolean ifPresent(Value key, Consumer<Value> consumer) {
40+
if (map.containsKey(key)) {
41+
consumer.accept(map.get(key));
42+
return true;
43+
}
44+
return false;
45+
}
46+
3447
public ArrayValue toPairs() {
3548
final int size = map.size();
3649
final ArrayValue result = new ArrayValue(size);
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.annimon.ownlang.lib;
2+
3+
import java.util.Iterator;
4+
import java.util.Map;
5+
import org.json.JSONArray;
6+
import org.json.JSONObject;
7+
8+
public final class ValueUtils {
9+
10+
public static Object toObject(Value val) {
11+
switch (val.type()) {
12+
case Types.ARRAY:
13+
return toObject((ArrayValue) val);
14+
case Types.MAP:
15+
return toObject((MapValue) val);
16+
case Types.NUMBER:
17+
return val.raw();
18+
case Types.STRING:
19+
return val.asString();
20+
default:
21+
return JSONObject.NULL;
22+
}
23+
}
24+
25+
public static Object toObject(MapValue map) {
26+
final JSONObject result = new JSONObject();
27+
for (Map.Entry<Value, Value> entry : map) {
28+
final String key = entry.getKey().asString();
29+
final Object value = toObject(entry.getValue());
30+
result.put(key, value);
31+
}
32+
return result;
33+
}
34+
35+
public static Object toObject(ArrayValue array) {
36+
final JSONArray result = new JSONArray();
37+
for (Value value : array) {
38+
result.put(toObject(value));
39+
}
40+
return result;
41+
}
42+
43+
public static Value toValue(Object obj) {
44+
if (obj instanceof JSONObject) {
45+
return toValue((JSONObject) obj);
46+
}
47+
if (obj instanceof JSONArray) {
48+
return toValue((JSONArray) obj);
49+
}
50+
if (obj instanceof String) {
51+
return new StringValue((String) obj);
52+
}
53+
if (obj instanceof Number) {
54+
return NumberValue.of(((Number) obj));
55+
}
56+
if (obj instanceof Boolean) {
57+
return NumberValue.fromBoolean((Boolean) obj);
58+
}
59+
// NULL or other
60+
return NumberValue.ZERO;
61+
}
62+
63+
public static MapValue toValue(JSONObject json) {
64+
final MapValue result = new MapValue(json.length());
65+
final Iterator<String> it = json.keys();
66+
while(it.hasNext()) {
67+
final String key = it.next();
68+
final Value value = toValue(json.get(key));
69+
result.set(new StringValue(key), value);
70+
}
71+
return result;
72+
}
73+
74+
public static ArrayValue toValue(JSONArray json) {
75+
final int length = json.length();
76+
final ArrayValue result = new ArrayValue(length);
77+
for (int i = 0; i < length; i++) {
78+
final Value value = toValue(json.get(i));
79+
result.set(i, value);
80+
}
81+
return result;
82+
}
83+
}
Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.annimon.ownlang.lib.modules.functions;
22

3-
import com.annimon.ownlang.lib.*;
4-
import java.util.Iterator;
5-
import org.json.*;
3+
import com.annimon.ownlang.lib.Arguments;
4+
import com.annimon.ownlang.lib.Function;
5+
import com.annimon.ownlang.lib.Value;
6+
import com.annimon.ownlang.lib.ValueUtils;
7+
import org.json.JSONException;
8+
import org.json.JSONTokener;
9+
610

711
public final class json_decode implements Function {
812

@@ -12,50 +16,9 @@ public Value execute(Value... args) {
1216
try {
1317
final String jsonRaw = args[0].asString();
1418
final Object root = new JSONTokener(jsonRaw).nextValue();
15-
return process(root);
19+
return ValueUtils.toValue(root);
1620
} catch (JSONException ex) {
1721
throw new RuntimeException("Error while parsing json", ex);
1822
}
1923
}
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 NumberValue.of(((Number) obj));
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-
}
6124
}
Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,25 @@
11
package com.annimon.ownlang.lib.modules.functions;
22

3-
import com.annimon.ownlang.lib.*;
4-
import java.util.Map;
5-
import org.json.*;
3+
import com.annimon.ownlang.lib.Arguments;
4+
import com.annimon.ownlang.lib.Function;
5+
import com.annimon.ownlang.lib.StringValue;
6+
import com.annimon.ownlang.lib.Value;
7+
import com.annimon.ownlang.lib.ValueUtils;
8+
import org.json.JSONException;
9+
import org.json.JSONObject;
10+
611

712
public final class json_encode implements Function {
813

914
@Override
1015
public Value execute(Value... args) {
1116
Arguments.check(1, args.length);
1217
try {
13-
final Object root = process(args[0]);
18+
final Object root = ValueUtils.toObject(args[0]);
1419
final String jsonRaw = JSONObject.valueToString(root);
1520
return new StringValue(jsonRaw);
1621
} catch (JSONException ex) {
1722
throw new RuntimeException("Error while creating json", ex);
1823
}
1924
}
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.raw();
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-
}
5325
}

0 commit comments

Comments
 (0)