on master help sign in
Trim out parts of API that are no longer useful
a7bb27dEvan Czaplicki
libraries/Json.elm+25−55
@@ -3,77 +3,47 @@ module Json where
3
3
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
7−@docs Json
8−
9−# Strings
10−@docs fromString, toString
6+@docs Value, fromString, toString
11−
12−# JS Strings
13−@docs fromJSString, toJSString
14−
15−# JS Objects
16−@docs fromJSObject, toJSObject
17
7
18-}
8-}
19
9
20−import Basics (..)
21import Dict
10import Dict
22import Maybe (Maybe)
11import Maybe (Maybe)
23−import JavaScript as JS
24import Native.Json
12import Native.Json
25−import JavaScript (JSObject, JSString)
26
13
27−{-| This datatype can represent all valid values that can be held in a JSON
14+{-| This datatype can represent all valid JSON values. A `Value` can be sent
28−object. In Elm, a proper JSON object is represented as a (Dict String Json)
15+through ports, and the runtime will do a conversion like this:
29−which is a mapping from strings to Json Values. -}
16+
17+ [2,'abc',null] == Array [Number 2, String "abc", Null]
18+
19+JSON is an intentional subset of JavaScript values which excludes functions.
20+This ensures that impure functions cannot sneak into Elm through ports.
21+-}
30−data Json
22+data Value
31 = String String
23 = String String
32 | Number Float
24 | Number Float
33 | Boolean Bool
25 | Boolean Bool
34 | Null
26 | Null
35− | Array [Json]
27+ | Array [Value]
36− | Object (Dict.Dict String Json)
28+ | Object (Dict.Dict String Value)
37
29
38
30
39−-- String Converters
40−
41−{-| Convert a `Json` into a prettified string.
31+{-| Convert a `Value` into a prettified string.
42The first argument is a separator token (e.g. \" \", \"\\n\", etc.) that will
32The first argument is a separator token (e.g. \" \", \"\\n\", etc.) that will
43−be used for indentation in the prettified string version of the JSON. -}
33+be used for indentation in the prettified string version of the JSON.
44−toString : String -> Json -> String
45−toString sep v = JS.toString (Native.Json.toJSString sep v)
46
34
47−{-| Convert a proper JSON object into a JavaScript string.
35+ toString "" Null == "null"
48−Note that the type JSString seen here is not the same as the type constructor
36+ toString "" (Number 4.0) == "4.0"
49−JsonString used elsewhere in this module. -}
37+-}
50−toJSString : String -> Json -> JSString
38+toString : String -> Value -> String
51−toJSString = Native.Json.toJSString
39+toString sep value = Native.Json.toString sep value
52
40
53{-| Parse a string representation of a proper JSON object into
41{-| Parse a string representation of a proper JSON object into
54−its Elm representation. -}
42+its Elm representation.
55−fromString : String -> Maybe Json
56−fromString s = Native.Json.fromJSString (JS.fromString s)
57−
58−{-| Parse a JavaScript string representation of a proper JSON object into
59−its Elm representation. -}
60−fromJSString : JSString -> Maybe Json
61−fromJSString = Native.Json.fromJSString
62−
63−{-| Convert a JS object into a `Json`. -}
64−fromJSObject : JSObject -> Json
65−fromJSObject = Native.Json.fromJSObject
66−
67−{-| Convert a `Json` into a `JSObject`. Paired with the
68−[`JavaScript.Experimental` library](/docs/JavaScript/Experimental.elm),
69−This lets you convert strings into Elm records:
70−
71− import JavaScript.Experimental as JS
72
43
73− stringToRecord str =
44+ fromString "null" == Just Null
74− case fromString str of
45+ fromString "[true,3]" == Just (Array [Boolean True, Number 3])
75− Just json -> Just (JS.toRecord (toJSObject json))
46+ fromString "{'abc':4}" == Just (Object (Dict.fromList [("abc", Number 4)]))
76− Nothing -> Nothing
77-}
47-}
78−toJSObject : Json -> JSObject
48+fromString : String -> Maybe Value
79−toJSObject = Native.Json.toJSObject
49+fromString str = Native.Json.fromString str
libraries/Native/Json.js+13−14
@@ -15,11 +15,14 @@ Elm.Native.Json.make = function(elm) {
15 switch (v.ctor) {
15 switch (v.ctor) {
16 case 'Null' : return null;
16 case 'Null' : return null;
17 case 'String' : return JS.fromString(v._0);
17 case 'String' : return JS.fromString(v._0);
18+ case 'Number' : return v._0;
19+ case 'Boolean': return v._0;
18 case 'Object' :
20 case 'Object' :
19 var obj = {};
21 var obj = {};
20 var array = JS.fromList(Dict.toList(v._0));
22 var array = JS.fromList(Dict.toList(v._0));
21 for (var i = array.length; i--; ) {
23 for (var i = array.length; i--; ) {
24+ var entry = array[i];
22− obj[JS.fromString(array[i]._0)] = fromValue(array[i]._1);
25+ obj[JS.fromString(entry._0)] = fromValue(entry._1);
23 }
26 }
24 return obj;
27 return obj;
25 case 'Array' :
28 case 'Array' :
@@ -28,20 +31,18 @@ Elm.Native.Json.make = function(elm) {
28 array[i] = fromValue(array[i]);
31 array[i] = fromValue(array[i]);
29 }
32 }
30 return array;
33 return array;
31− default :
32− return v._0;
33 }
34 }
34 }
35 }
35
36
36− function toPrettyJSString(sep, obj) {
37+ function toString(sep, value) {
37− return JSON.stringify(fromValue(obj), null, JS.fromString(sep));
38+ return JSON.stringify(fromValue(value), null, JS.fromString(sep));
38 }
39 }
39
40
40 function toValue(v) {
41 function toValue(v) {
41 switch (typeof v) {
42 switch (typeof v) {
42− case 'string' : return { ctor:"String", _0: JS.toString(v) };
43+ case 'string' : return { ctor:"String" , _0: JS.toString(v) };
43− case 'number' : return { ctor:"Number", _0: JS.toFloat(v) };
44+ case 'number' : return { ctor:"Number" , _0: JS.toFloat(v) };
44− case 'boolean': return { ctor:"Boolean" , _0: JS.toBool(v) };
45+ case 'boolean': return { ctor:"Boolean", _0: JS.toBool(v) };
45 case 'object' :
46 case 'object' :
46 if (v === null) return { ctor:"Null" };
47 if (v === null) return { ctor:"Null" };
47 if (v instanceof Array) {
48 if (v instanceof Array) {
@@ -54,19 +55,17 @@ Elm.Native.Json.make = function(elm) {
54 }
55 }
55 }
56 }
56
57
57− function fromJSString(str) {
58+ function fromString(str) {
58 try {
59 try {
59− return Maybe.Just(toValue(JSON.parse(str)));
60+ return Maybe.Just(toValue(JSON.parse(JS.fromString(str))));
60 } catch (e) {
61 } catch (e) {
61 return Maybe.Nothing;
62 return Maybe.Nothing;
62 }
63 }
63 }
64 }
64
65
65 return elm.Native.Json.values = {
66 return elm.Native.Json.values = {
66− toJSString : F2(toPrettyJSString),
67+ toString : F2(toString),
67− fromJSString : fromJSString,
68+ fromString : fromString,
68− toJSObject : fromValue,
69− fromJSObject : toValue
70 };
69 };
71
70
72};
71};