on 63243ca help sign in
elk/libraries/Json.elm
134 lines 4.2 kB history raw
2module Json where
4{-| Library for working with [JSON](https://en.wikipedia.org/wiki/JSON) values.
6# Json Values
7@docs JsonValue
9# Strings
10@docs fromString, toString
12# JS Strings
13@docs fromJSString, toJSString
15# JS Objects
16@docs fromJSObject, toJSObject
18-}
20import open Basics
21import Dict
22import Maybe (Maybe)
23import JavaScript as JS
24import Native.Json
25import JavaScript (JSObject, JSString)
27{-| This datatype can represent all valid values that can be held in a JSON
28object. In Elm, a proper JSON object is represented as a (Dict String JsonValue)
29which is a mapping from strings to Json Values. -}
30data JsonValue
31 = String String
32 | Number Float
33 | Boolean Bool
34 | Null
35 | Array [JsonValue]
36 | Object (Dict.Dict String JsonValue)
39-- String Converters
41{-| Convert a `JsonValue` into a prettified string.
42The first argument is a separator token (e.g. \" \", \"\\n\", etc.) that will
43be used for indentation in the prettified string version of the JSON. -}
44toString : String -> JsonValue -> String
45toString sep v = JS.toString (Native.Json.toJSString sep v)
47{-| Convert a proper JSON object into a JavaScript string.
48Note that the type JSString seen here is not the same as the type constructor
49JsonString used elsewhere in this module. -}
50toJSString : String -> JsonValue -> JSString
51toJSString = Native.Json.toJSString
53{-| Parse a string representation of a proper JSON object into
54its Elm representation. -}
55fromString : String -> Maybe JsonValue
56fromString s = Native.Json.fromJSString (JS.fromString s)
58{-| Parse a JavaScript string representation of a proper JSON object into
59its Elm representation. -}
60fromJSString : JSString -> Maybe JsonValue
61fromJSString = Native.Json.fromJSString
63{-| Convert a JS object into a `JsonValue`. -}
64fromJSObject : JSObject -> JsonValue
65fromJSObject = Native.Json.fromJSObject
67{-| Convert a `JsonValue` into a `JSObject`. Paired with the
68[`JavaScript.Experimental` library](/docs/JavaScript/Experimental.elm),
69This lets you convert strings into Elm records:
71 import JavaScript.Experimental as JS
73 stringToRecord str =
74 case fromString str of
75 Just jsonValue -> Just (JS.toRecord (toJSObject jsonValue))
76 Nothing -> Nothing
77-}
78toJSObject : JsonValue -> JSObject
79toJSObject = Native.Json.toJSObject
81 {-- Extract Elm values from Json values
83 string : JsonValue -> String
84 string v = case v of { String s -> s ; _ -> "" }
86 number : JsonValue -> Float
87 number v = case v of { Number n -> n ; _ -> 0 }
89 boolean : JsonValue -> Bool
90 boolean v = case v of { Boolean b -> b ; _ -> False }
92 array : JsonValue -> [JsonValue]
93 array v = case v of { Array a -> a ; _ -> [] }
95 object : JsonValue -> Dict String JsonValue
96 object v = case v of { Object o -> o ; _ -> Dict.empty }
99 -- Extract Elm values from dictionaries of Json values
101 {-| Find a value in a Json Object using the passed get function. If the key is
102 not found, this returns the given default/base value. -}
103 find get base =
104 let f key dict =
105 case Dict.lookup key dict of
106 Nothing -> base
107 Just v -> get v
108 in f
110 {-| Find a string value in an Elm Json object. If the key is not found or the
111 value found is not a string, this returns the empty string. -}
112 findString : String -> Object -> String
113 findString = find string ""
115 {-| Find a number value in an Elm Json object. If the key is not found or the
116 value found is not a number, this returns 0 -}
117 findNumber : String -> Object -> Float
118 findNumber = find number 0
120 {-| Find a boolean value in an Elm Json object. If the key is not found or the
121 value found is not a boolean, this returns the False. -}
122 findBoolean : String -> Object -> Bool
123 findBoolean = find boolean False
125 {-| Find an array value in an Elm Json object. If the key is not found or the
126 value found is not an array, this returns an empty list. -}
127 findArray : String -> Object -> [JsonValue]
128 findArray = find array []
130 {-| Find an object value in an Elm Json object. If the key is not found or the
131 value found is not an object, this returns an empty object. -}
132 findObject : String -> Object -> Object
133 findObject = find object Dict.empty
134 --}