A peer-to-peer stack for code collaboration
Make to-json work on more inputs. (#578)
James Haydonac6e647e7b62
diff --git a/docs/source/reference.rst b/docs/source/reference.rstindex 14b0dc2..1e464dd 100644--- a/docs/source/reference.rst+++ b/docs/source/reference.rst@@ -445,7 +445,12 @@ right now, formatted according to ISO 8601.``to-json``~~~~~~~~~~~-Returns a JSON formatted string representing the input value.+Returns a JSON formatted string representing the input value. Numbers+are only converted if they have a finite decimal expansion. Strings and+booleans are converted to their JSON counterparts. Keywords are+converted to JSON strings (dropping the initial ':'). Lists and vectors+are converted to JSON arrays. Dicts are converted to JSON objects as+long as all the keys are either strings or keywords.``uuid!``~~~~~~~~~diff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hsindex 7eff072..85eb4d3 100644--- a/src/Radicle/Internal/Core.hs+++ b/src/Radicle/Internal/Core.hs@@ -422,9 +422,9 @@ isAtom _ = Nothing-- >>> encode $ maybeJson $ Dict $ Map.fromList [(String "foo", String "bar")]-- "{\"foo\":\"bar\"}"----- This fails for lambdas, since lambdas capture an entire environment--- (possibly recursively). It also fails for dictionaries with non-string key--- non-string keys.+-- This fails for lambdas, since lambdas capture an entire environment (possibly+-- recursively). It also fails for dictionaries with non-stringy (string or+-- keyword) keys.---- >>> import Data.Aeson (encode)-- >>> encode $ maybeJson $ Dict $ Map.fromList [(Number 3, String "bar")]@@ -433,17 +433,20 @@ maybeJson :: Value -> Maybe A.ValuemaybeJson = \caseNumber n -> A.Number <$> hush (Num.isSci n)String s -> pure $ A.String s+ Keyword (Ident i) -> pure $ A.String iBoolean b -> pure $ A.Bool bList ls -> toJSON <$> traverse maybeJson ls+ Vec xs -> toJSON <$> traverse maybeJson (toList xs)Dict m -> dolet kvs = Map.toList m- ks <- traverse isStr (fst <$> kvs)+ ks <- traverse isStringy (fst <$> kvs)vs <- traverse maybeJson (snd <$> kvs)pure $ A.Object (HashMap.fromList (zip ks vs))_ -> Nothingwhere- isStr (String s) = pure s- isStr _ = Nothing+ isStringy (String s) = pure s+ isStringy (Keyword (Ident i)) = pure i+ isStringy _ = Nothing-- | The environment, which keeps all known bindings.newtype Env s = Env { fromEnv :: Map Ident (Doc.Docd s) }diff --git a/src/Radicle/Internal/PrimFns.hs b/src/Radicle/Internal/PrimFns.hsindex b6a6c1b..35d34b1 100644--- a/src/Radicle/Internal/PrimFns.hs+++ b/src/Radicle/Internal/PrimFns.hs@@ -565,7 +565,12 @@ purePrimFns = fromList $ allDocs $v -> throwErrorHere $ TypeError "seq" 0 TStructure v), ( "to-json"- , "Returns a JSON formatted string representing the input value."+ , "Returns a JSON formatted string representing the input value. Numbers are only\+ \ converted if they have a finite decimal expansion. Strings and booleans are\+ \ converted to their JSON counterparts. Keywords are converted to JSON strings\+ \ (dropping the initial ':'). Lists and vectors are converted to JSON arrays.\+ \ Dicts are converted to JSON objects as long as all the keys are either\+ \ strings or keywords.", oneArg "to-json" $ \v -> String . toS . Aeson.encode <$>maybeJson v ?? toLangError (OtherError "Could not serialise value to JSON"))diff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hsindex 121fe27..92d7657 100644--- a/test/spec/Radicle/Tests.hs+++ b/test/spec/Radicle/Tests.hs@@ -476,7 +476,9 @@ test_eval =, testCase "'to-json' works" $ dorunPureCode "(to-json (dict \"foo\" #t))" @?= Right (String "{\"foo\":true}")+ runPureCode "(to-json {:foo #t \"bar\" #f})" @?= Right (String "{\"foo\":true,\"bar\":false}")runPureCode "(to-json (dict \"key\" (list 1 \"value\")))" @?= Right (String "{\"key\":[1,\"value\"]}")+ runPureCode "(to-json [3/2 #f])" @?= Right (String "[1.5,false]")noStack (runPureCode "(to-json (dict 1 2))") @?= Left (OtherError "Could not serialise value to JSON"), testCase "def-rec can define recursive functions" $ do