on master help sign in
Change the `Json` API and change the name of `WebSocket.open` to not clash with `open` as a keyword.
96e7194evancz
libraries/Json.elm+85−75
@@ -8,95 +8,105 @@ import JavaScript as JS
8import Native.Json as Native
8import Native.Json as Native
9
9
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
17− | Null
17+ | Null
18− | Array [Value]
18+ | Array [JsonValue]
19− | Object (Dict String Value)
19+ | Object (Dict String JsonValue)
20
20
21
21
22-- String Converters
22-- String Converters
23
23
24−-- Convert a proper JSON object into a string.
25−toString : Value -> String
26−toString v = JS.toString (Native.toPrettyJSString "" v)
27−
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)
33
29
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
39
34
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)
44
39
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
48−
43+
49−
44+-- Convert a JS object into a `JsonValue`.
45+fromJSObject : JSObject -> JsonValue
46+
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:
50+--
51+-- import JavaScript.Experimental as JS
52+--
53+-- stringToRecord str =
54+-- case fromString str of
55+-- Just jsonValue -> Just (JS.toRecord (toJSObject jsonValue))
56+-- Nothing -> Nothing
57+toJSObject : JsonValue -> JSObject
58+
50−-- Extract Elm values from Json values
59+ {-- Extract Elm values from Json values
51−
60+
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 ; _ -> "" }
54−
63+
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 }
57−
66+
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 }
60−
69+
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 ; _ -> [] }
63−
72+
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 }
66−
75+
67−
76+
68−-- Extract Elm values from dictionaries of Json values
77+ -- Extract Elm values from dictionaries of Json values
69−
78+
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
77− in f
86+ in f
78−
87+
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 ""
83−
92+
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
88−
97+
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
93−
102+
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 []
98−
107+
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
112+ --}
libraries/Native/Json.js+3−14
@@ -59,22 +59,11 @@ Elm.Native.Json = function(elm) {
59 }
59 }
60 }
60 }
61
61
62− function recordFromJSString(str) {
63− try {
64− return Maybe.Just(JS.toRecord(JSON.parse(str)));
65− } catch (e) {
66− return Maybe.Nothing;
67− }
68− }
69− function recordToPrettyJSString(sep, rec) {
70− return JSON.stringify(JS.fromRecord(rec), null, JS.fromString(sep));
71− }
72−
73 return elm.Native.Json = {
62 return elm.Native.Json = {
74− toPrettyJSString : F2(toPrettyJSString),
63+ toJSString : F2(toPrettyJSString),
75 fromJSString : fromJSString,
64 fromJSString : fromJSString,
76− recordToPrettyJSString : F2(recordToPrettyJSString),
65+ toJSObject : fromValue,
77− recordFromJSString : recordFromJSString
66+ fromJSObject : toValue
78 };
67 };
79
68
80};
69};
libraries/Native/Signal/WebSocket.js+1−1
@@ -34,5 +34,5 @@ Elm.Native.WebSocket = function(elm) {
34 return A3(Signal.lift2, F2(take1), incoming, A2(Signal.lift, send, outgoing));
34 return A3(Signal.lift2, F2(take1), incoming, A2(Signal.lift, send, outgoing));
35 }
35 }
36
36
37− return elm.Native.WebSocket = { open: F2(open) };
37+ return elm.Native.WebSocket = { connect: F2(open) };
38};
38};
libraries/WebSocket.elm+1−1
@@ -7,7 +7,7 @@ import Native.WebSocket as WS
7-- Create a web-socket. The first argument is the URL of the desired
7-- Create a web-socket. The first argument is the URL of the desired
8-- web-socket server. The input signal holds the outgoing messages,
8-- web-socket server. The input signal holds the outgoing messages,
9-- and the resulting signal contains the incoming ones.
9-- and the resulting signal contains the incoming ones.
10−open : String -> Signal String -> Signal String
10+connect : String -> Signal String -> Signal String
11
11
12-- data Action = Open String | Close String | Send String String
12-- data Action = Open String | Close String | Send String String
13-- connections : Signal Action -> Signal String
13-- connections : Signal Action -> Signal String