2013-03-03 05:43:44 +00:00
|
|
|
|
2013-03-24 12:45:56 +00:00
|
|
|
-- TOOD: Evan please review texts
|
|
|
|
|
2013-03-06 17:22:59 +00:00
|
|
|
module Json where
|
2013-03-03 05:43:44 +00:00
|
|
|
|
2013-03-17 05:24:18 +00:00
|
|
|
import Dict as Dict
|
2013-03-03 05:43:44 +00:00
|
|
|
import JavaScript as JS
|
|
|
|
import Native.Json as Native
|
|
|
|
|
2013-04-10 05:15:06 +00:00
|
|
|
-- This datatype can represent all valid values that can be held in a JSON
|
2013-05-21 09:04:38 +00:00
|
|
|
-- object. In Elm, a proper JSON object is represented as a (Dict String JsonValue)
|
2013-04-10 05:15:06 +00:00
|
|
|
-- which is a mapping from strings to Json Values.
|
2013-05-21 09:04:38 +00:00
|
|
|
data JsonValue
|
|
|
|
= String String
|
|
|
|
| Number Float
|
|
|
|
| Boolean Bool
|
|
|
|
| Null
|
|
|
|
| Array [JsonValue]
|
|
|
|
| Object (Dict String JsonValue)
|
2013-03-03 05:43:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- String Converters
|
|
|
|
|
2013-05-21 09:04:38 +00:00
|
|
|
-- Convert a `JsonValue` into a prettified string.
|
2013-04-10 05:15:06 +00:00
|
|
|
-- The first argument is a separator token (e.g. \" \", \"\\n\", etc.) that will
|
2013-05-21 09:04:38 +00:00
|
|
|
-- be used for indentation in the prettified string version of the JSON.
|
|
|
|
toString : String -> JsonValue -> String
|
|
|
|
toString sep v = JS.toString (Native.toJSString sep v)
|
2013-03-03 05:43:44 +00:00
|
|
|
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Convert a proper JSON object into a JavaScript string.
|
2013-04-10 05:15:06 +00:00
|
|
|
-- Note that the type JSString seen here is not the same as the type constructor
|
|
|
|
-- JsonString used elsewhere in this module.
|
2013-05-21 09:04:38 +00:00
|
|
|
toJSString : String -> JsonValue -> JSString
|
2013-03-03 05:43:44 +00:00
|
|
|
|
2013-04-10 05:15:06 +00:00
|
|
|
-- Parse a string representation of a proper JSON object into
|
2013-05-11 20:44:28 +00:00
|
|
|
-- its Elm representation.
|
2013-05-21 09:04:38 +00:00
|
|
|
fromString : String -> Maybe JsonValue
|
2013-03-03 05:43:44 +00:00
|
|
|
fromString s = Native.fromJSString (JS.fromString s)
|
|
|
|
|
2013-04-10 05:15:06 +00:00
|
|
|
-- Parse a JavaScript string representation of a proper JSON object into
|
2013-05-11 20:44:28 +00:00
|
|
|
-- its Elm representation.
|
2013-05-21 09:04:38 +00:00
|
|
|
fromJSString : JSString -> Maybe JsonValue
|
|
|
|
|
|
|
|
-- Convert a JS object into a `JsonValue`.
|
|
|
|
fromJSObject : JSObject -> JsonValue
|
|
|
|
|
|
|
|
-- Convert a `JsonValue` into a `JSObject`. Paired with the
|
|
|
|
-- [`JavaScript.Experimental` library](/docs/JavaScript/Experimental.elm),
|
|
|
|
-- This lets you convert strings into Elm records:
|
|
|
|
--
|
|
|
|
-- import JavaScript.Experimental as JS
|
|
|
|
--
|
|
|
|
-- stringToRecord str =
|
|
|
|
-- case fromString str of
|
|
|
|
-- Just jsonValue -> Just (JS.toRecord (toJSObject jsonValue))
|
|
|
|
-- Nothing -> Nothing
|
|
|
|
toJSObject : JsonValue -> JSObject
|
|
|
|
|
|
|
|
{-- Extract Elm values from Json values
|
|
|
|
|
|
|
|
string : JsonValue -> String
|
|
|
|
string v = case v of { String s -> s ; _ -> "" }
|
|
|
|
|
|
|
|
number : JsonValue -> Float
|
|
|
|
number v = case v of { Number n -> n ; _ -> 0 }
|
|
|
|
|
|
|
|
boolean : JsonValue -> Bool
|
|
|
|
boolean v = case v of { Boolean b -> b ; _ -> False }
|
|
|
|
|
|
|
|
array : JsonValue -> [JsonValue]
|
|
|
|
array v = case v of { Array a -> a ; _ -> [] }
|
|
|
|
|
|
|
|
object : JsonValue -> Dict String JsonValue
|
|
|
|
object v = case v of { Object o -> o ; _ -> Dict.empty }
|
|
|
|
|
|
|
|
|
|
|
|
-- Extract Elm values from dictionaries of Json values
|
|
|
|
|
|
|
|
-- Find a value in a Json Object using the passed get function. If the key is
|
|
|
|
-- not found, this returns the given default/base value.
|
|
|
|
find get base =
|
|
|
|
let f key dict =
|
|
|
|
case Dict.lookup key dict of
|
|
|
|
Nothing -> base
|
|
|
|
Just v -> get v
|
|
|
|
in f
|
|
|
|
|
|
|
|
-- Find a string value in an Elm Json object. If the key is not found or the
|
|
|
|
-- value found is not a string, this returns the empty string.
|
|
|
|
findString : String -> Object -> String
|
|
|
|
findString = find string ""
|
|
|
|
|
|
|
|
-- Find a number value in an Elm Json object. If the key is not found or the
|
|
|
|
-- value found is not a number, this returns 0
|
|
|
|
findNumber : String -> Object -> Float
|
|
|
|
findNumber = find number 0
|
|
|
|
|
|
|
|
-- Find a boolean value in an Elm Json object. If the key is not found or the
|
|
|
|
-- value found is not a boolean, this returns the False.
|
|
|
|
findBoolean : String -> Object -> Bool
|
|
|
|
findBoolean = find boolean False
|
|
|
|
|
|
|
|
-- Find an array value in an Elm Json object. If the key is not found or the
|
|
|
|
-- value found is not an array, this returns an empty list.
|
|
|
|
findArray : String -> Object -> [JsonValue]
|
|
|
|
findArray = find array []
|
|
|
|
|
|
|
|
-- Find an object value in an Elm Json object. If the key is not found or the
|
|
|
|
-- value found is not an object, this returns an empty object.
|
|
|
|
findObject : String -> Object -> Object
|
|
|
|
findObject = find object Dict.empty
|
|
|
|
--}
|