@@ -4,7 +4,7 @@ module Json where
44{-| Library for working with [JSON](https://en.wikipedia.org/wiki/JSON) values.
55
66# Json Values
7−@docs JsonValue
7+@docs Json
88
99# Strings
1010@docs fromString, toString
@@ -25,46 +25,46 @@ import Native.Json
2525import JavaScript (JSObject, JSString)
2626
2727{-| 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)
2929which is a mapping from strings to Json Values. -}
30−data JsonValue
30+data Json
3131 = String String
3232 | Number Float
3333 | Boolean Bool
3434 | Null
35− | Array [JsonValue]
36− | Object (Dict.Dict String JsonValue)
35+ | Array [Json]
36+ | Object (Dict.Dict String Json)
3737
3838
3939-- String Converters
4040
41−{-| Convert a `JsonValue` into a prettified string.
41+{-| Convert a `Json` into a prettified string.
4242The first argument is a separator token (e.g. \" \", \"\\n\", etc.) that will
4343be used for indentation in the prettified string version of the JSON. -}
44−toString : String -> JsonValue -> String
44+toString : String -> Json -> String
4545toString sep v = JS.toString (Native.Json.toJSString sep v)
4646
4747{-| Convert a proper JSON object into a JavaScript string.
4848Note that the type JSString seen here is not the same as the type constructor
4949JsonString used elsewhere in this module. -}
50−toJSString : String -> JsonValue -> JSString
50+toJSString : String -> Json -> JSString
5151toJSString = Native.Json.toJSString
5252
5353{-| Parse a string representation of a proper JSON object into
5454its Elm representation. -}
55−fromString : String -> Maybe JsonValue
55+fromString : String -> Maybe Json
5656fromString s = Native.Json.fromJSString (JS.fromString s)
5757
5858{-| Parse a JavaScript string representation of a proper JSON object into
5959its Elm representation. -}
60−fromJSString : JSString -> Maybe JsonValue
60+fromJSString : JSString -> Maybe Json
6161fromJSString = Native.Json.fromJSString
6262
63−{-| Convert a JS object into a `JsonValue`. -}
64−fromJSObject : JSObject -> JsonValue
63+{-| Convert a JS object into a `Json`. -}
64+fromJSObject : JSObject -> Json
6565fromJSObject = Native.Json.fromJSObject
6666
67−{-| Convert a `JsonValue` into a `JSObject`. Paired with the
67+{-| Convert a `Json` into a `JSObject`. Paired with the
6868[`JavaScript.Experimental` library](/docs/JavaScript/Experimental.elm),
6969This lets you convert strings into Elm records:
7070
@@ -72,63 +72,8 @@ This lets you convert strings into Elm records:
7272
7373 stringToRecord str =
7474 case fromString str of
75− Just jsonValue -> Just (JS.toRecord (toJSObject jsonValue))
75+ Just json -> Just (JS.toRecord (toJSObject json))
7676 Nothing -> Nothing
7777-}
78−toJSObject : JsonValue -> JSObject
78+toJSObject : Json -> JSObject
7979toJSObject = Native.Json.toJSObject
80−
81− {-- Extract Elm values from Json values
82−
83− string : JsonValue -> String
84− string v = case v of { String s -> s ; _ -> "" }
85−
86− number : JsonValue -> Float
87− number v = case v of { Number n -> n ; _ -> 0 }
88−
89− boolean : JsonValue -> Bool
90− boolean v = case v of { Boolean b -> b ; _ -> False }
91−
92− array : JsonValue -> [JsonValue]
93− array v = case v of { Array a -> a ; _ -> [] }
94−
95− object : JsonValue -> Dict String JsonValue
96− object v = case v of { Object o -> o ; _ -> Dict.empty }
97−
98−
99− -- Extract Elm values from dictionaries of Json values
100−
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. -}
103− find get base =
104− let f key dict =
105− case Dict.lookup key dict of
106− Nothing -> base
107− Just v -> get v
108− in f
109−
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 ""
114−
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
119−
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
124−
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 []
129−
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
134− --}