on master help sign in
Change the name of JsonValue to Json and trim dead code
0304f0bEvan Czaplicki
libraries/Json.elm+15−70
@@ -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.
5
5
6# Json Values
6# Json Values
7−@docs JsonValue
7+@docs Json
8
8
9# Strings
9# Strings
10@docs fromString, toString
10@docs fromString, toString
@@ -25,46 +25,46 @@ import Native.Json
25import JavaScript (JSObject, JSString)
25import JavaScript (JSObject, JSString)
26
26
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
34 | Null
34 | Null
35− | Array [JsonValue]
35+ | Array [Json]
36− | Object (Dict.Dict String JsonValue)
36+ | Object (Dict.Dict String Json)
37
37
38
38
39-- String Converters
39-- String Converters
40
40
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)
46
46
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
52
52
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)
57
57
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
62
62
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
66
66
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:
70
70
@@ -72,63 +72,8 @@ This lets you convert strings into Elm records:
72
72
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
77-}
77-}
78−toJSObject : JsonValue -> JSObject
78+toJSObject : Json -> JSObject
79toJSObject = Native.Json.toJSObject
79toJSObject = 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− --}