on 727d708 help sign in
elk/libraries/Json.elm
112 lines 3.8 kB history raw
2-- TOOD: Evan please review texts
4module Json where
6import Dict as Dict
7import JavaScript as JS
8import Native.Json as Native
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 JsonValue)
12-- which is a mapping from strings to Json Values.
13data JsonValue
14 = String String
15 | Number Float
16 | Boolean Bool
17 | Null
18 | Array [JsonValue]
19 | Object (Dict String JsonValue)
22-- String Converters
24-- Convert a `JsonValue` into a prettified string.
25-- The first argument is a separator token (e.g. \" \", \"\\n\", etc.) that will
26-- be used for indentation in the prettified string version of the JSON.
27toString : String -> JsonValue -> String
28toString sep v = JS.toString (Native.toJSString sep v)
30-- Convert a proper JSON object into a JavaScript string.
31-- Note that the type JSString seen here is not the same as the type constructor
32-- JsonString used elsewhere in this module.
33toJSString : String -> JsonValue -> JSString
35-- Parse a string representation of a proper JSON object into
36-- its Elm representation.
37fromString : String -> Maybe JsonValue
38fromString s = Native.fromJSString (JS.fromString s)
40-- Parse a JavaScript string representation of a proper JSON object into
41-- its Elm representation.
42fromJSString : JSString -> Maybe JsonValue
44-- Convert a JS object into a `JsonValue`.
45fromJSObject : 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:
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
57toJSObject : JsonValue -> JSObject
59 {-- Extract Elm values from Json values
61 string : JsonValue -> String
62 string v = case v of { String s -> s ; _ -> "" }
64 number : JsonValue -> Float
65 number v = case v of { Number n -> n ; _ -> 0 }
67 boolean : JsonValue -> Bool
68 boolean v = case v of { Boolean b -> b ; _ -> False }
70 array : JsonValue -> [JsonValue]
71 array v = case v of { Array a -> a ; _ -> [] }
73 object : JsonValue -> Dict String JsonValue
74 object v = case v of { Object o -> o ; _ -> Dict.empty }
77 -- Extract Elm values from dictionaries of Json values
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
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 ""
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
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
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 []
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 --}