From 323d68d67818a42838a25fee27f1e8c8bbfd852d Mon Sep 17 00:00:00 2001 From: zeroflag Date: Tue, 29 Jun 2021 16:46:01 +0200 Subject: [PATCH] json response --- .../java/com/vectron/fcl/types/JvmObj.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/main/java/com/vectron/fcl/types/JvmObj.java b/src/main/java/com/vectron/fcl/types/JvmObj.java index aeafe8b..5bc7159 100644 --- a/src/main/java/com/vectron/fcl/types/JvmObj.java +++ b/src/main/java/com/vectron/fcl/types/JvmObj.java @@ -2,11 +2,38 @@ package com.vectron.fcl.types; import com.vectron.fcl.exceptions.TypeMismatched; +import java.util.Collection; +import java.util.Map; + import static com.vectron.fcl.Fcl.STRICT; public class JvmObj implements Obj { private final Object object; + public static Obj toFcl(Object object) { + if (object == null) + return Nil.INSTANCE; + if (object instanceof Number) + return new Num((Number)object); + if (object instanceof CharSequence) + return new Str(((CharSequence)object).toString()); + if (object instanceof Boolean) + return (Boolean)object ? Bool.TRUE : Bool.FALSE; + if (object instanceof Map) { + Dic result = Dic.empty(); + for (Map.Entry each : ((Map) object).entrySet()) + result.put(toFcl(each.getKey()), toFcl(each.getValue())); + return result; + } + if (object instanceof Collection) { + Lst result = Lst.empty(); + for (Object each : (Collection) object) + result.append(toFcl(each)); + return result; + } + return new JvmObj(object); + } + public JvmObj(Object object) { this.object = object; }