@@ -1,8 +1,19 @@
2−-- TOOD: Evan please review texts
4module Json where
2module Json where
4+{-| Library for working with [JSON](https://en.wikipedia.org/wiki/JSON) values.
7+@docs fromString, toString
10+@docs fromJSString, toJSString
13+@docs fromJSObject, toJSObject
6import open Basics
17import open Basics
7import Dict
18import Dict
8import Maybe (Maybe)
19import Maybe (Maybe)
@@ -10,9 +21,9 @@ import JavaScript as JS
10import Native.Json
21import Native.Json
11import JavaScript (JSObject, JSString)
22import JavaScript (JSObject, JSString)
13−-- This datatype can represent all valid values that can be held in a JSON
24+{-| This datatype can represent all valid values that can be held in a JSON
14−-- object. In Elm, a proper JSON object is represented as a (Dict String JsonValue)
25+object. In Elm, a proper JSON object is represented as a (Dict String JsonValue)
15−-- which is a mapping from strings to Json Values.
26+which is a mapping from strings to Json Values. -}
16data JsonValue
27data JsonValue
17 = String String
28 = String String
18 | Number Float
29 | Number Float
@@ -24,42 +35,43 @@ data JsonValue
25-- String Converters
36-- String Converters
27−-- Convert a `JsonValue` into a prettified string.
38+{-| Convert a `JsonValue` into a prettified string.
28−-- The first argument is a separator token (e.g. \" \", \"\\n\", etc.) that will
39+The first argument is a separator token (e.g. \" \", \"\\n\", etc.) that will
29−-- be used for indentation in the prettified string version of the JSON.
40+be used for indentation in the prettified string version of the JSON. -}
30toString : String -> JsonValue -> String
41toString : String -> JsonValue -> String
31toString sep v = JS.toString (Native.Json.toJSString sep v)
42toString sep v = JS.toString (Native.Json.toJSString sep v)
33−-- Convert a proper JSON object into a JavaScript string.
44+{-| Convert a proper JSON object into a JavaScript string.
34−-- Note that the type JSString seen here is not the same as the type constructor
45+Note that the type JSString seen here is not the same as the type constructor
35−-- JsonString used elsewhere in this module.
46+JsonString used elsewhere in this module. -}
36toJSString : String -> JsonValue -> JSString
47toJSString : String -> JsonValue -> JSString
37toJSString = Native.Json.toJSString
48toJSString = Native.Json.toJSString
39−-- Parse a string representation of a proper JSON object into
50+{-| Parse a string representation of a proper JSON object into
40−-- its Elm representation.
51+its Elm representation. -}
41fromString : String -> Maybe JsonValue
52fromString : String -> Maybe JsonValue
42fromString s = Native.Json.fromJSString (JS.fromString s)
53fromString s = Native.Json.fromJSString (JS.fromString s)
44−-- Parse a JavaScript string representation of a proper JSON object into
55+{-| Parse a JavaScript string representation of a proper JSON object into
45−-- its Elm representation.
56+its Elm representation. -}
46fromJSString : JSString -> Maybe JsonValue
57fromJSString : JSString -> Maybe JsonValue
47fromJSString = Native.Json.fromJSString
58fromJSString = Native.Json.fromJSString
49−-- Convert a JS object into a `JsonValue`.
60+{-| Convert a JS object into a `JsonValue`. -}
50fromJSObject : JSObject -> JsonValue
61fromJSObject : JSObject -> JsonValue
51fromJSObject = Native.Json.fromJSObject
62fromJSObject = Native.Json.fromJSObject
53−-- Convert a `JsonValue` into a `JSObject`. Paired with the
64+{-| Convert a `JsonValue` into a `JSObject`. Paired with the
54−-- [`JavaScript.Experimental` library](/docs/JavaScript/Experimental.elm),
65+[`JavaScript.Experimental` library](/docs/JavaScript/Experimental.elm),
55−-- This lets you convert strings into Elm records:
66+This lets you convert strings into Elm records:
57−-- import JavaScript.Experimental as JS
68+ import JavaScript.Experimental as JS
59−-- stringToRecord str =
70+ stringToRecord str =
60−-- case fromString str of
71+ case fromString str of
61−-- Just jsonValue -> Just (JS.toRecord (toJSObject jsonValue))
72+ Just jsonValue -> Just (JS.toRecord (toJSObject jsonValue))
62−-- Nothing -> Nothing
73+ Nothing -> Nothing
63toJSObject : JsonValue -> JSObject
75toJSObject : JsonValue -> JSObject
64toJSObject = Native.Json.toJSObject
76toJSObject = Native.Json.toJSObject
@@ -83,8 +95,8 @@ toJSObject = Native.Json.toJSObject
84 -- Extract Elm values from dictionaries of Json values
96 -- Extract Elm values from dictionaries of Json values
86− -- Find a value in a Json Object using the passed get function. If the key is
98+ {-| Find a value in a Json Object using the passed get function. If the key is
87− -- not found, this returns the given default/base value.
99+ not found, this returns the given default/base value. -}
88 find get base =
100 find get base =
89 let f key dict =
101 let f key dict =
90 case Dict.lookup key dict of
102 case Dict.lookup key dict of
@@ -92,28 +104,28 @@ toJSObject = Native.Json.toJSObject
92 Just v -> get v
104 Just v -> get v
95− -- Find a string value in an Elm Json object. If the key is not found or the
107+ {-| Find a string value in an Elm Json object. If the key is not found or the
96− -- value found is not a string, this returns the empty string.
108+ value found is not a string, this returns the empty string. -}
97 findString : String -> Object -> String
109 findString : String -> Object -> String
98 findString = find string ""
110 findString = find string ""
100− -- Find a number value in an Elm Json object. If the key is not found or the
112+ {-| Find a number value in an Elm Json object. If the key is not found or the
101− -- value found is not a number, this returns 0
113+ value found is not a number, this returns 0 -}
102 findNumber : String -> Object -> Float
114 findNumber : String -> Object -> Float
103 findNumber = find number 0
115 findNumber = find number 0
105− -- Find a boolean value in an Elm Json object. If the key is not found or the
117+ {-| Find a boolean value in an Elm Json object. If the key is not found or the
106− -- value found is not a boolean, this returns the False.
118+ value found is not a boolean, this returns the False. -}
107 findBoolean : String -> Object -> Bool
119 findBoolean : String -> Object -> Bool
108 findBoolean = find boolean False
120 findBoolean = find boolean False
110− -- Find an array value in an Elm Json object. If the key is not found or the
122+ {-| Find an array value in an Elm Json object. If the key is not found or the
111− -- value found is not an array, this returns an empty list.
123+ value found is not an array, this returns an empty list. -}
112 findArray : String -> Object -> [JsonValue]
124 findArray : String -> Object -> [JsonValue]
113 findArray = find array []
125 findArray = find array []
115− -- Find an object value in an Elm Json object. If the key is not found or the
127+ {-| Find an object value in an Elm Json object. If the key is not found or the
116− -- value found is not an object, this returns an empty object.
128+ value found is not an object, this returns an empty object. -}
117 findObject : String -> Object -> Object
129 findObject : String -> Object -> Object
118 findObject = find object Dict.empty
130 findObject = find object Dict.empty