@@ -3,77 +3,47 @@ module Json where
33
44{-| Library for working with [JSON](https://en.wikipedia.org/wiki/JSON) values.
55
6−# Json Values
7−@docs Json
8−
9−# Strings
10−@docs fromString, toString
11−
12−# JS Strings
13−@docs fromJSString, toJSString
14−
15−# JS Objects
16−@docs fromJSObject, toJSObject
6+@docs Value, fromString, toString
177
188-}
199
20−import Basics (..)
2110import Dict
2211import Maybe (Maybe)
23−import JavaScript as JS
2412import Native.Json
25−import JavaScript (JSObject, JSString)
2613
27−{-| This datatype can represent all valid values that can be held in a JSON
28−object. In Elm, a proper JSON object is represented as a (Dict String Json)
29−which is a mapping from strings to Json Values. -}
30−data Json
14+{-| This datatype can represent all valid JSON values. A `Value` can be sent
15+through ports, and the runtime will do a conversion like this:
16+
17+ [2,'abc',null] == Array [Number 2, String "abc", Null]
18+
19+JSON is an intentional subset of JavaScript values which excludes functions.
20+This ensures that impure functions cannot sneak into Elm through ports.
21+-}
22+data Value
3123 = String String
3224 | Number Float
3325 | Boolean Bool
3426 | Null
35− | Array [Json]
36− | Object (Dict.Dict String Json)
27+ | Array [Value]
28+ | Object (Dict.Dict String Value)
3729
3830
39−-- String Converters
40−
41−{-| Convert a `Json` into a prettified string.
31+{-| Convert a `Value` into a prettified string.
4232The first argument is a separator token (e.g. \" \", \"\\n\", etc.) that will
43−be used for indentation in the prettified string version of the JSON. -}
44−toString : String -> Json -> String
45−toString sep v = JS.toString (Native.Json.toJSString sep v)
33+be used for indentation in the prettified string version of the JSON.
4634
47−{-| Convert a proper JSON object into a JavaScript string.
48−Note that the type JSString seen here is not the same as the type constructor
49−JsonString used elsewhere in this module. -}
50−toJSString : String -> Json -> JSString
51−toJSString = Native.Json.toJSString
35+ toString "" Null == "null"
36+ toString "" (Number 4.0) == "4.0"
37+-}
38+toString : String -> Value -> String
39+toString sep value = Native.Json.toString sep value
5240
5341{-| Parse a string representation of a proper JSON object into
54−its Elm representation. -}
55−fromString : String -> Maybe Json
56−fromString s = Native.Json.fromJSString (JS.fromString s)
57−
58−{-| Parse a JavaScript string representation of a proper JSON object into
59−its Elm representation. -}
60−fromJSString : JSString -> Maybe Json
61−fromJSString = Native.Json.fromJSString
62−
63−{-| Convert a JS object into a `Json`. -}
64−fromJSObject : JSObject -> Json
65−fromJSObject = Native.Json.fromJSObject
66−
67−{-| Convert a `Json` into a `JSObject`. Paired with the
68−[`JavaScript.Experimental` library](/docs/JavaScript/Experimental.elm),
69−This lets you convert strings into Elm records:
70−
71− import JavaScript.Experimental as JS
42+its Elm representation.
7243
73− stringToRecord str =
74− case fromString str of
75− Just json -> Just (JS.toRecord (toJSObject json))
76− Nothing -> Nothing
44+ fromString "null" == Just Null
45+ fromString "[true,3]" == Just (Array [Boolean True, Number 3])
46+ fromString "{'abc':4}" == Just (Object (Dict.fromList [("abc", Number 4)]))
7747-}
78−toJSObject : Json -> JSObject
79−toJSObject = Native.Json.toJSObject
48+fromString : String -> Maybe Value
49+fromString str = Native.Json.fromString str