mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 00:34:20 +03:00
Конвертация в объект/значение вынесена в отдельный класс
This commit is contained in:
parent
07fd22914b
commit
148b6eff32
83
src/main/java/com/annimon/ownlang/lib/ValueUtils.java
Normal file
83
src/main/java/com/annimon/ownlang/lib/ValueUtils.java
Normal file
@ -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<Value, Value> 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<String> 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;
|
||||
}
|
||||
}
|
@ -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<String> 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;
|
||||
}
|
||||
}
|
@ -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<Value, Value> 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user