@@ -4,7 +4,7 @@ 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.
6# Json Values
6# Json Values
7−@docs JsonValue
7+@docs Json
10@docs fromString, toString
10@docs fromString, toString
@@ -25,46 +25,46 @@ import Native.Json
25import JavaScript (JSObject, JSString)
25import JavaScript (JSObject, JSString)
27{-| This datatype can represent all valid values that can be held in a JSON
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 JsonValue)
28+object. In Elm, a proper JSON object is represented as a (Dict String Json)
29which is a mapping from strings to Json Values. -}
29which is a mapping from strings to Json Values. -}
30−data JsonValue
30+data Json
31 = String String
31 = String String
32 | Number Float
32 | Number Float
33 | Boolean Bool
33 | Boolean Bool
35− | Array [JsonValue]
35+ | Array [Json]
36− | Object (Dict.Dict String JsonValue)
36+ | Object (Dict.Dict String Json)
39-- String Converters
39-- String Converters
41−{-| Convert a `JsonValue` into a prettified string.
41+{-| Convert a `Json` into a prettified string.
42The first argument is a separator token (e.g. \" \", \"\\n\", etc.) that will
42The first argument is a separator token (e.g. \" \", \"\\n\", etc.) that will
43be used for indentation in the prettified string version of the JSON. -}
43be used for indentation in the prettified string version of the JSON. -}
44−toString : String -> JsonValue -> String
44+toString : String -> Json -> String
45toString sep v = JS.toString (Native.Json.toJSString sep v)
45toString sep v = JS.toString (Native.Json.toJSString sep v)
47{-| Convert a proper JSON object into a JavaScript string.
47{-| Convert a proper JSON object into a JavaScript string.
48Note that the type JSString seen here is not the same as the type constructor
48Note that the type JSString seen here is not the same as the type constructor
49JsonString used elsewhere in this module. -}
49JsonString used elsewhere in this module. -}
50−toJSString : String -> JsonValue -> JSString
50+toJSString : String -> Json -> JSString
51toJSString = Native.Json.toJSString
51toJSString = Native.Json.toJSString
53{-| Parse a string representation of a proper JSON object into
53{-| Parse a string representation of a proper JSON object into
54its Elm representation. -}
54its Elm representation. -}
55−fromString : String -> Maybe JsonValue
55+fromString : String -> Maybe Json
56fromString s = Native.Json.fromJSString (JS.fromString s)
56fromString s = Native.Json.fromJSString (JS.fromString s)
58{-| Parse a JavaScript string representation of a proper JSON object into
58{-| Parse a JavaScript string representation of a proper JSON object into
59its Elm representation. -}
59its Elm representation. -}
60−fromJSString : JSString -> Maybe JsonValue
60+fromJSString : JSString -> Maybe Json
61fromJSString = Native.Json.fromJSString
61fromJSString = Native.Json.fromJSString
63−{-| Convert a JS object into a `JsonValue`. -}
63+{-| Convert a JS object into a `Json`. -}
64−fromJSObject : JSObject -> JsonValue
64+fromJSObject : JSObject -> Json
65fromJSObject = Native.Json.fromJSObject
65fromJSObject = Native.Json.fromJSObject
67−{-| Convert a `JsonValue` into a `JSObject`. Paired with the
67+{-| Convert a `Json` into a `JSObject`. Paired with the
68[`JavaScript.Experimental` library](/docs/JavaScript/Experimental.elm),
68[`JavaScript.Experimental` library](/docs/JavaScript/Experimental.elm),
69This lets you convert strings into Elm records:
69This lets you convert strings into Elm records:
@@ -72,63 +72,8 @@ This lets you convert strings into Elm records:
73 stringToRecord str =
73 stringToRecord str =
74 case fromString str of
74 case fromString str of
75− Just jsonValue -> Just (JS.toRecord (toJSObject jsonValue))
75+ Just json -> Just (JS.toRecord (toJSObject json))
76 Nothing -> Nothing
76 Nothing -> Nothing
78−toJSObject : JsonValue -> JSObject
78+toJSObject : Json -> JSObject
79toJSObject = Native.Json.toJSObject
79toJSObject = Native.Json.toJSObject
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