@@ -3,132 +3,47 @@ module Json where
4{-| Library for working with [JSON](https://en.wikipedia.org/wiki/JSON) values.
4{-| Library for working with [JSON](https://en.wikipedia.org/wiki/JSON) values.
10−@docs fromString, toString
6+@docs Value, fromString, toString
13−@docs fromJSString, toJSString
16−@docs fromJSObject, toJSObject
21import Dict
10import Dict
22import Maybe (Maybe)
11import Maybe (Maybe)
23−import JavaScript as JS
24import Native.Json
12import Native.Json
25−import JavaScript (JSObject, JSString)
27−{-| This datatype can represent all valid values that can be held in a JSON
14+{-| This datatype can represent all valid JSON values. A `Value` can be sent
28−object. In Elm, a proper JSON object is represented as a (Dict String JsonValue)
15+through ports, and the runtime will do a conversion like this:
29−which is a mapping from strings to Json Values. -}
16+
30−data JsonValue
17+ [2,'abc',null] == Array [Number 2, String "abc", Null]
19+JSON is an intentional subset of JavaScript values which excludes functions.
20+This ensures that impure functions cannot sneak into Elm through ports.
31 = String String
23 = String String
32 | Number Float
24 | Number Float
33 | Boolean Bool
25 | Boolean Bool
35− | Array [JsonValue]
27+ | Array [Value]
36− | Object (Dict.Dict String JsonValue)
28+ | Object (Dict.Dict String Value)
41−{-| Convert a `JsonValue` into a prettified string.
31+{-| Convert a `Value` into a prettified string.
42The first argument is a separator token (e.g. \" \", \"\\n\", etc.) that will
32The 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. -}
33+be used for indentation in the prettified string version of the JSON.
44−toString : String -> JsonValue -> String
45−toString sep v = JS.toString (Native.Json.toJSString sep v)
47−{-| Convert a proper JSON object into a JavaScript string.
35+ toString "" Null == "null"
48−Note that the type JSString seen here is not the same as the type constructor
36+ toString "" (Number 4.0) == "4.0"
49−JsonString used elsewhere in this module. -}
37+-}
50−toJSString : String -> JsonValue -> JSString
38+toString : String -> Value -> String
51−toJSString = Native.Json.toJSString
39+toString sep value = Native.Json.toString sep value
53{-| Parse a string representation of a proper JSON object into
41{-| Parse a string representation of a proper JSON object into
54−its Elm representation. -}
42+its Elm representation.
55−fromString : String -> Maybe JsonValue
56−fromString s = Native.Json.fromJSString (JS.fromString s)
58−{-| Parse a JavaScript string representation of a proper JSON object into
59−its Elm representation. -}
60−fromJSString : JSString -> Maybe JsonValue
61−fromJSString = Native.Json.fromJSString
63−{-| Convert a JS object into a `JsonValue`. -}
64−fromJSObject : JSObject -> JsonValue
65−fromJSObject = Native.Json.fromJSObject
67−{-| Convert a `JsonValue` into a `JSObject`. Paired with the
44+ fromString "null" == Just Null
68−[`JavaScript.Experimental` library](/docs/JavaScript/Experimental.elm),
45+ fromString "[true,3]" == Just (Array [Boolean True, Number 3])
69−This lets you convert strings into Elm records:
46+ fromString "{'abc':4}" == Just (Object (Dict.fromList [("abc", Number 4)]))
71− import JavaScript.Experimental as JS
74− case fromString str of
75− Just jsonValue -> Just (JS.toRecord (toJSObject jsonValue))
78−toJSObject : JsonValue -> JSObject
48+fromString : String -> Maybe Value
79−toJSObject = Native.Json.toJSObject
49+fromString str = Native.Json.fromString str
81− {-- Extract Elm values from Json values
83− string : JsonValue -> String
84− string v = case v of { String s -> s ; _ -> "" }
86− number : JsonValue -> Float
87− number v = case v of { Number n -> n ; _ -> 0 }
89− boolean : JsonValue -> Bool
90− boolean v = case v of { Boolean b -> b ; _ -> False }
92− array : JsonValue -> [JsonValue]
93− array v = case v of { Array a -> a ; _ -> [] }
95− object : JsonValue -> Dict String JsonValue
96− object v = case v of { Object o -> o ; _ -> Dict.empty }
99− -- Extract Elm values from dictionaries of Json values
101− {-| Find a value in a Json Object using the passed get function. If the key is
102− not found, this returns the given default/base value. -}
105− case Dict.lookup key dict of
110− {-| Find a string value in an Elm Json object. If the key is not found or the
111− value found is not a string, this returns the empty string. -}
112− findString : String -> Object -> String
113− findString = find string ""
115− {-| Find a number value in an Elm Json object. If the key is not found or the
116− value found is not a number, this returns 0 -}
117− findNumber : String -> Object -> Float
118− findNumber = find number 0
120− {-| Find a boolean value in an Elm Json object. If the key is not found or the
121− value found is not a boolean, this returns the False. -}
122− findBoolean : String -> Object -> Bool
123− findBoolean = find boolean False
125− {-| Find an array value in an Elm Json object. If the key is not found or the
126− value found is not an array, this returns an empty list. -}
127− findArray : String -> Object -> [JsonValue]
128− findArray = find array []
130− {-| Find an object value in an Elm Json object. If the key is not found or the
131− value found is not an object, this returns an empty object. -}
132− findObject : String -> Object -> Object
133− findObject = find object Dict.empty