@@ -8,95 +8,105 @@ import JavaScript as JS
8import Native.Json as Native
8import Native.Json as Native
10-- This datatype can represent all valid values that can be held in a JSON
10-- This datatype can represent all valid values that can be held in a JSON
11−-- object. In Elm, a proper JSON object is represented as a (Dict String Value)
11+-- object. In Elm, a proper JSON object is represented as a (Dict String JsonValue)
12-- which is a mapping from strings to Json Values.
12-- which is a mapping from strings to Json Values.
13−data Value
13+data JsonValue
14− = String String
14+ = String String
15− | Number Float
15+ | Number Float
16− | Boolean Bool
16+ | Boolean Bool
18− | Array [Value]
18+ | Array [JsonValue]
19− | Object (Dict String Value)
19+ | Object (Dict String JsonValue)
22-- String Converters
22-- String Converters
24−-- Convert a proper JSON object into a string.
25−toString : Value -> String
26−toString v = JS.toString (Native.toPrettyJSString "" v)
28−-- Convert a proper JSON object into a prettified string.
24+-- Convert a `JsonValue` into a prettified string.
29-- The first argument is a separator token (e.g. \" \", \"\\n\", etc.) that will
25-- The first argument is a separator token (e.g. \" \", \"\\n\", etc.) that will
30−-- be used for indentation in the prettified string version of the JSON object.
26+-- be used for indentation in the prettified string version of the JSON.
31−toPrettyString : String -> Value -> String
27+toString : String -> JsonValue -> String
32−toPrettyString sep v = JS.toString (Native.toPrettyJSString sep v)
28+toString sep v = JS.toString (Native.toJSString sep v)
34-- Convert a proper JSON object into a JavaScript string.
30-- Convert a proper JSON object into a JavaScript string.
35-- Note that the type JSString seen here is not the same as the type constructor
31-- Note that the type JSString seen here is not the same as the type constructor
36-- JsonString used elsewhere in this module.
32-- JsonString used elsewhere in this module.
37−toJSString : Value -> JSString
33+toJSString : String -> JsonValue -> JSString
38−toJSString v = Native.toPrettyJSString "" v
40-- Parse a string representation of a proper JSON object into
35-- Parse a string representation of a proper JSON object into
41-- its Elm representation.
36-- its Elm representation.
42−fromString : String -> Maybe Value
37+fromString : String -> Maybe JsonValue
43fromString s = Native.fromJSString (JS.fromString s)
38fromString s = Native.fromJSString (JS.fromString s)
45-- Parse a JavaScript string representation of a proper JSON object into
40-- Parse a JavaScript string representation of a proper JSON object into
46-- its Elm representation.
41-- its Elm representation.
47−fromJSString : JSString -> Maybe Value
42+fromJSString : JSString -> Maybe JsonValue
49−
44+-- Convert a JS object into a `JsonValue`.
45+fromJSObject : JSObject -> JsonValue
47+-- Convert a `JsonValue` into a `JSObject`. Paired with the
48+-- [`JavaScript.Experimental` library](/docs/JavaScript/Experimental.elm),
49+-- This lets you convert strings into Elm records:
51+-- import JavaScript.Experimental as JS
53+-- stringToRecord str =
54+-- case fromString str of
55+-- Just jsonValue -> Just (JS.toRecord (toJSObject jsonValue))
57+toJSObject : JsonValue -> JSObject
50−-- Extract Elm values from Json values
59+ {-- Extract Elm values from Json values
52−string : Value -> String
61+ string : JsonValue -> String
53−string v = case v of { String s -> s ; _ -> "" }
62+ string v = case v of { String s -> s ; _ -> "" }
55−number : Value -> Float
64+ number : JsonValue -> Float
56−number v = case v of { Number n -> n ; _ -> 0 }
65+ number v = case v of { Number n -> n ; _ -> 0 }
58−boolean : Value -> Bool
67+ boolean : JsonValue -> Bool
59−boolean v = case v of { Boolean b -> b ; _ -> False }
68+ boolean v = case v of { Boolean b -> b ; _ -> False }
61−array : Value -> [Value]
70+ array : JsonValue -> [JsonValue]
62−array v = case v of { Array a -> a ; _ -> [] }
71+ array v = case v of { Array a -> a ; _ -> [] }
64−object : Value -> Dict String Value
73+ object : JsonValue -> Dict String JsonValue
65−object v = case v of { Object o -> o ; _ -> Dict.empty }
74+ object v = case v of { Object o -> o ; _ -> Dict.empty }
68−-- Extract Elm values from dictionaries of Json values
77+ -- Extract Elm values from dictionaries of Json values
70−-- Find a value in a Json Object using the passed get function. If the key is
79+ -- Find a value in a Json Object using the passed get function. If the key is
71−-- not found, this returns the given default/base value.
80+ -- not found, this returns the given default/base value.
72−find get base =
81+ find get base =
73− let f key dict =
82+ let f key dict =
74− case Dict.lookup key dict of
83+ case Dict.lookup key dict of
75− Nothing -> base
84+ Nothing -> base
76− Just v -> get v
85+ Just v -> get v
79−-- Find a string value in an Elm Json object. If the key is not found or the
88+ -- Find a string value in an Elm Json object. If the key is not found or the
80−-- value found is not a string, this returns the empty string.
89+ -- value found is not a string, this returns the empty string.
81−findString : String -> Object -> String
90+ findString : String -> Object -> String
82−findString = find string ""
91+ findString = find string ""
84−-- Find a number value in an Elm Json object. If the key is not found or the
93+ -- Find a number value in an Elm Json object. If the key is not found or the
85−-- value found is not a number, this returns 0
94+ -- value found is not a number, this returns 0
86−findNumber : String -> Object -> Float
95+ findNumber : String -> Object -> Float
87−findNumber = find number 0
96+ findNumber = find number 0
89−-- Find a boolean value in an Elm Json object. If the key is not found or the
98+ -- Find a boolean value in an Elm Json object. If the key is not found or the
90−-- value found is not a boolean, this returns the False.
99+ -- value found is not a boolean, this returns the False.
91−findBoolean : String -> Object -> Bool
100+ findBoolean : String -> Object -> Bool
92−findBoolean = find boolean False
101+ findBoolean = find boolean False
94−-- Find an array value in an Elm Json object. If the key is not found or the
103+ -- Find an array value in an Elm Json object. If the key is not found or the
95−-- value found is not an array, this returns an empty list.
104+ -- value found is not an array, this returns an empty list.
96−findArray : String -> Object -> [Value]
105+ findArray : String -> Object -> [JsonValue]
97−findArray = find array []
106+ findArray = find array []
99−-- Find an object value in an Elm Json object. If the key is not found or the
108+ -- Find an object value in an Elm Json object. If the key is not found or the
100−-- value found is not an object, this returns an empty object.
109+ -- value found is not an object, this returns an empty object.
101−findObject : String -> Object -> Object
110+ findObject : String -> Object -> Object
102−findObject = find object Dict.empty
111+ findObject = find object Dict.empty