on master help sign in
Change the `Json` API and change the name of `WebSocket.open` to not clash with `open` as a keyword.
commit96e719401dbddb81b920989ef2dbe984642b420d authorevancz <info@elm-lang.org> dateTue 21 May 2013, 11:04 (13 years ago) parentd31acc5
4 files changed+90−91
libraries/Json.elm+85−75 libraries/Native/Json.js+3−14 libraries/Native/Signal/WebSocket.js+1−1 libraries/WebSocket.elm+1−1
libraries/Json.elm+85−75
@@ -8,95 +8,105 @@ import JavaScript as JS
88import Native.Json as Native
99
1010-- 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)
1212-- which is a mapping from strings to Json Values.
13−data Value
14− = String String
15− | Number Float
16− | Boolean Bool
17− | Null
18− | Array [Value]
19− | Object (Dict String Value)
13+data JsonValue
14+ = String String
15+ | Number Float
16+ | Boolean Bool
17+ | Null
18+ | Array [JsonValue]
19+ | Object (Dict String JsonValue)
2020
2121
2222-- String Converters
2323
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.
2925-- 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.
31−toPrettyString : String -> Value -> String
32−toPrettyString sep v = JS.toString (Native.toPrettyJSString sep v)
26+-- be used for indentation in the prettified string version of the JSON.
27+toString : String -> JsonValue -> String
28+toString sep v = JS.toString (Native.toJSString sep v)
3329
3430-- Convert a proper JSON object into a JavaScript string.
3531-- Note that the type JSString seen here is not the same as the type constructor
3632-- JsonString used elsewhere in this module.
37−toJSString : Value -> JSString
38−toJSString v = Native.toPrettyJSString "" v
33+toJSString : String -> JsonValue -> JSString
3934
4035-- Parse a string representation of a proper JSON object into
4136-- its Elm representation.
42−fromString : String -> Maybe Value
37+fromString : String -> Maybe JsonValue
4338fromString s = Native.fromJSString (JS.fromString s)
4439
4540-- Parse a JavaScript string representation of a proper JSON object into
4641-- its Elm representation.
47−fromJSString : JSString -> Maybe Value
48−
49−
50−-- Extract Elm values from Json values
51−
52−string : Value -> String
53−string v = case v of { String s -> s ; _ -> "" }
54−
55−number : Value -> Float
56−number v = case v of { Number n -> n ; _ -> 0 }
57−
58−boolean : Value -> Bool
59−boolean v = case v of { Boolean b -> b ; _ -> False }
60−
61−array : Value -> [Value]
62−array v = case v of { Array a -> a ; _ -> [] }
63−
64−object : Value -> Dict String Value
65−object v = case v of { Object o -> o ; _ -> Dict.empty }
66−
67−
68−-- Extract Elm values from dictionaries of Json values
69−
70−-- 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.
72−find get base =
73− let f key dict =
74− case Dict.lookup key dict of
75− Nothing -> base
76− Just v -> get v
77− in f
78−
79−-- 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.
81−findString : String -> Object -> String
82−findString = find string ""
83−
84−-- 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
86−findNumber : String -> Object -> Float
87−findNumber = find number 0
88−
89−-- 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.
91−findBoolean : String -> Object -> Bool
92−findBoolean = find boolean False
93−
94−-- 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.
96−findArray : String -> Object -> [Value]
97−findArray = find array []
98−
99−-- 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.
101−findObject : String -> Object -> Object
102−findObject = find object Dict.empty
42+fromJSString : JSString -> Maybe JsonValue
43+
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+
59+ {-- Extract Elm values from Json values
60+
61+ string : JsonValue -> String
62+ string v = case v of { String s -> s ; _ -> "" }
63+
64+ number : JsonValue -> Float
65+ number v = case v of { Number n -> n ; _ -> 0 }
66+
67+ boolean : JsonValue -> Bool
68+ boolean v = case v of { Boolean b -> b ; _ -> False }
69+
70+ array : JsonValue -> [JsonValue]
71+ array v = case v of { Array a -> a ; _ -> [] }
72+
73+ object : JsonValue -> Dict String JsonValue
74+ object v = case v of { Object o -> o ; _ -> Dict.empty }
75+
76+
77+ -- Extract Elm values from dictionaries of Json values
78+
79+ -- Find a value in a Json Object using the passed get function. If the key is
80+ -- not found, this returns the given default/base value.
81+ find get base =
82+ let f key dict =
83+ case Dict.lookup key dict of
84+ Nothing -> base
85+ Just v -> get v
86+ in f
87+
88+ -- Find a string value in an Elm Json object. If the key is not found or the
89+ -- value found is not a string, this returns the empty string.
90+ findString : String -> Object -> String
91+ findString = find string ""
92+
93+ -- Find a number value in an Elm Json object. If the key is not found or the
94+ -- value found is not a number, this returns 0
95+ findNumber : String -> Object -> Float
96+ findNumber = find number 0
97+
98+ -- Find a boolean value in an Elm Json object. If the key is not found or the
99+ -- value found is not a boolean, this returns the False.
100+ findBoolean : String -> Object -> Bool
101+ findBoolean = find boolean False
102+
103+ -- Find an array value in an Elm Json object. If the key is not found or the
104+ -- value found is not an array, this returns an empty list.
105+ findArray : String -> Object -> [JsonValue]
106+ findArray = find array []
107+
108+ -- Find an object value in an Elm Json object. If the key is not found or the
109+ -- value found is not an object, this returns an empty object.
110+ findObject : String -> Object -> Object
111+ findObject = find object Dict.empty
112+ --}
libraries/Native/Json.js+3−14
@@ -59,22 +59,11 @@ Elm.Native.Json = function(elm) {
5959 }
6060 }
6161
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−
7362 return elm.Native.Json = {
74− toPrettyJSString : F2(toPrettyJSString),
63+ toJSString : F2(toPrettyJSString),
7564 fromJSString : fromJSString,
76− recordToPrettyJSString : F2(recordToPrettyJSString),
77− recordFromJSString : recordFromJSString
65+ toJSObject : fromValue,
66+ fromJSObject : toValue
7867 };
7968
8069};
libraries/Native/Signal/WebSocket.js+1−1
@@ -34,5 +34,5 @@ Elm.Native.WebSocket = function(elm) {
3434 return A3(Signal.lift2, F2(take1), incoming, A2(Signal.lift, send, outgoing));
3535 }
3636
37− return elm.Native.WebSocket = { open: F2(open) };
37+ return elm.Native.WebSocket = { connect: F2(open) };
3838};
libraries/WebSocket.elm+1−1
@@ -7,7 +7,7 @@ import Native.WebSocket as WS
77-- Create a web-socket. The first argument is the URL of the desired
88-- web-socket server. The input signal holds the outgoing messages,
99-- and the resulting signal contains the incoming ones.
10−open : String -> Signal String -> Signal String
10+connect : String -> Signal String -> Signal String
1111
1212-- data Action = Open String | Close String | Send String String
1313-- connections : Signal Action -> Signal String