From 148b6eff32e06c8c7a4d1583e7298a685cdb4ad1 Mon Sep 17 00:00:00 2001 From: Victor Date: Tue, 2 Aug 2016 23:06:21 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=BE=D0=BD=D0=B2=D0=B5=D1=80=D1=82?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F=20=D0=B2=20=D0=BE=D0=B1=D1=8A=D0=B5?= =?UTF-8?q?=D0=BA=D1=82/=D0=B7=D0=BD=D0=B0=D1=87=D0=B5=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=20=D0=B2=D1=8B=D0=BD=D0=B5=D1=81=D0=B5=D0=BD=D0=B0=20=D0=B2=20?= =?UTF-8?q?=D0=BE=D1=82=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD=D1=8B=D0=B9=20=D0=BA?= =?UTF-8?q?=D0=BB=D0=B0=D1=81=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/annimon/ownlang/lib/ValueUtils.java | 83 +++++++++++++++++++ .../lib/modules/functions/json_decode.java | 53 ++---------- .../lib/modules/functions/json_encode.java | 46 ++-------- 3 files changed, 100 insertions(+), 82 deletions(-) create mode 100644 src/main/java/com/annimon/ownlang/lib/ValueUtils.java diff --git a/src/main/java/com/annimon/ownlang/lib/ValueUtils.java b/src/main/java/com/annimon/ownlang/lib/ValueUtils.java new file mode 100644 index 0000000..90feac5 --- /dev/null +++ b/src/main/java/com/annimon/ownlang/lib/ValueUtils.java @@ -0,0 +1,83 @@ +package com.annimon.ownlang.lib; + +import java.util.Iterator; +import java.util.Map; +import org.json.JSONArray; +import org.json.JSONObject; + +public final class ValueUtils { + + public static Object toObject(Value val) { + switch (val.type()) { + case Types.ARRAY: + return toObject((ArrayValue) val); + case Types.MAP: + return toObject((MapValue) val); + case Types.NUMBER: + return val.raw(); + case Types.STRING: + return val.asString(); + default: + return JSONObject.NULL; + } + } + + public static Object toObject(MapValue map) { + final JSONObject result = new JSONObject(); + for (Map.Entry entry : map) { + final String key = entry.getKey().asString(); + final Object value = toObject(entry.getValue()); + result.put(key, value); + } + return result; + } + + public static Object toObject(ArrayValue array) { + final JSONArray result = new JSONArray(); + for (Value value : array) { + result.put(toObject(value)); + } + return result; + } + + public static Value toValue(Object obj) { + if (obj instanceof JSONObject) { + return toValue((JSONObject) obj); + } + if (obj instanceof JSONArray) { + return toValue((JSONArray) obj); + } + if (obj instanceof String) { + return new StringValue((String) obj); + } + if (obj instanceof Number) { + return NumberValue.of(((Number) obj)); + } + if (obj instanceof Boolean) { + return NumberValue.fromBoolean((Boolean) obj); + } + // NULL or other + return NumberValue.ZERO; + } + + public static MapValue toValue(JSONObject json) { + final MapValue result = new MapValue(json.length()); + final Iterator it = json.keys(); + while(it.hasNext()) { + final String key = it.next(); + final Value value = toValue(json.get(key)); + result.set(new StringValue(key), value); + } + return result; + } + + public static ArrayValue toValue(JSONArray json) { + final int length = json.length(); + final ArrayValue result = new ArrayValue(length); + for (int i = 0; i < length; i++) { + final Value value = toValue(json.get(i)); + result.set(i, value); + } + return result; + } +} diff --git a/src/main/java/com/annimon/ownlang/lib/modules/functions/json_decode.java b/src/main/java/com/annimon/ownlang/lib/modules/functions/json_decode.java index 5130a42..8bd415a 100644 --- a/src/main/java/com/annimon/ownlang/lib/modules/functions/json_decode.java +++ b/src/main/java/com/annimon/ownlang/lib/modules/functions/json_decode.java @@ -1,8 +1,12 @@ package com.annimon.ownlang.lib.modules.functions; -import com.annimon.ownlang.lib.*; -import java.util.Iterator; -import org.json.*; +import com.annimon.ownlang.lib.Arguments; +import com.annimon.ownlang.lib.Function; +import com.annimon.ownlang.lib.Value; +import com.annimon.ownlang.lib.ValueUtils; +import org.json.JSONException; +import org.json.JSONTokener; + public final class json_decode implements Function { @@ -12,50 +16,9 @@ public final class json_decode implements Function { try { final String jsonRaw = args[0].asString(); final Object root = new JSONTokener(jsonRaw).nextValue(); - return process(root); + return ValueUtils.toValue(root); } catch (JSONException ex) { throw new RuntimeException("Error while parsing json", ex); } } - - private Value process(Object obj) { - if (obj instanceof JSONObject) { - return process((JSONObject) obj); - } - if (obj instanceof JSONArray) { - return process((JSONArray) obj); - } - if (obj instanceof String) { - return new StringValue((String) obj); - } - if (obj instanceof Number) { - return NumberValue.of(((Number) obj)); - } - if (obj instanceof Boolean) { - return NumberValue.fromBoolean((Boolean) obj); - } - // NULL or other - return NumberValue.ZERO; - } - - private MapValue process(JSONObject json) { - final MapValue result = new MapValue(json.length()); - final Iterator it = json.keys(); - while(it.hasNext()) { - final String key = it.next(); - final Value value = process(json.get(key)); - result.set(new StringValue(key), value); - } - return result; - } - - private ArrayValue process(JSONArray json) { - final int length = json.length(); - final ArrayValue result = new ArrayValue(length); - for (int i = 0; i < length; i++) { - final Value value = process(json.get(i)); - result.set(i, value); - } - return result; - } } \ No newline at end of file diff --git a/src/main/java/com/annimon/ownlang/lib/modules/functions/json_encode.java b/src/main/java/com/annimon/ownlang/lib/modules/functions/json_encode.java index 33951c4..7907b14 100644 --- a/src/main/java/com/annimon/ownlang/lib/modules/functions/json_encode.java +++ b/src/main/java/com/annimon/ownlang/lib/modules/functions/json_encode.java @@ -1,8 +1,13 @@ package com.annimon.ownlang.lib.modules.functions; -import com.annimon.ownlang.lib.*; -import java.util.Map; -import org.json.*; +import com.annimon.ownlang.lib.Arguments; +import com.annimon.ownlang.lib.Function; +import com.annimon.ownlang.lib.StringValue; +import com.annimon.ownlang.lib.Value; +import com.annimon.ownlang.lib.ValueUtils; +import org.json.JSONException; +import org.json.JSONObject; + public final class json_encode implements Function { @@ -10,44 +15,11 @@ public final class json_encode implements Function { public Value execute(Value... args) { Arguments.check(1, args.length); try { - final Object root = process(args[0]); + final Object root = ValueUtils.toObject(args[0]); final String jsonRaw = JSONObject.valueToString(root); return new StringValue(jsonRaw); } catch (JSONException ex) { throw new RuntimeException("Error while creating json", ex); } } - - private Object process(Value val) { - switch (val.type()) { - case Types.ARRAY: - return process((ArrayValue) val); - case Types.MAP: - return process((MapValue) val); - case Types.NUMBER: - return val.raw(); - case Types.STRING: - return val.asString(); - default: - return JSONObject.NULL; - } - } - - private Object process(MapValue map) { - final JSONObject result = new JSONObject(); - for (Map.Entry entry : map) { - final String key = entry.getKey().asString(); - final Object value = process(entry.getValue()); - result.put(key, value); - } - return result; - } - - private Object process(ArrayValue array) { - final JSONArray result = new JSONArray(); - for (Value value : array) { - result.put(process(value)); - } - return result; - } } \ No newline at end of file