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-03-24 12:45:56 +00:00
-- This datatype can represent all valid values that can be held in a JSON object.
-- In Elm, a proper JSON object is represented as a (Dict String Value) which is a mapping from strings to Json Values.
2013-03-03 05:43:44 +00:00
data Value
= String String
| Number Float
| Boolean Bool
| Null
| Array [ Value ]
| Object ( Dict String Value )
-- String Converters
2013-03-24 12:45:56 +00:00
-- Convert a proper JSON object into a string.
2013-03-03 05:43:44 +00:00
toString : Value -> String
toString v = JS . toString ( Native . toPrettyJSString " " v )
2013-03-24 12:45:56 +00:00
-- Convert a proper JSON object into a prettified string.
-- The first argument is a separator token (e.g. \" \", \"\\n\", etc.) that will be used for indentation in the prettified string version of the JSON object.
2013-03-03 05:43:44 +00:00
toPrettyString : String -> Value -> String
toPrettyString sep v = JS . toString ( Native . toPrettyJSString sep v )
2013-03-24 12:45:56 +00:00
-- Convert a proper JSON object into a JavaScript string.
-- Note that the type JSString seen here is not the same as the type constructor JsonString used elsewhere in this module.
2013-03-03 05:43:44 +00:00
toJSString : Value -> JSString
toJSString v = Native . toPrettyJSString " " v
2013-03-24 12:45:56 +00:00
-- Parse a string representation of a proper JSON object into its Elm's representation.
2013-03-03 05:43:44 +00:00
fromString : String -> Maybe Value
fromString s = Native . fromJSString ( JS . fromString s )
2013-03-24 12:45:56 +00:00
-- Parse a JavaScript string representation of a proper JSON object into its Elm's representation.
2013-03-03 05:43:44 +00:00
fromJSString : JSString -> Maybe Value
-- Extract Elm values from Json values
2013-03-10 08:54:37 +00:00
string : Value -> String
string v = case v of { String s -> s ; _ -> " " }
2013-03-03 05:43:44 +00:00
2013-03-10 08:54:37 +00:00
number : Value -> Float
number v = case v of { Number n -> n ; _ -> 0 }
2013-03-03 05:43:44 +00:00
2013-03-10 08:54:37 +00:00
boolean : Value -> Bool
boolean v = case v of { Boolean b -> b ; _ -> False }
2013-03-03 05:43:44 +00:00
2013-03-10 08:54:37 +00:00
array : Value -> [ Value ]
array v = case v of { Array a -> a ; _ -> [ ] }
2013-03-03 05:43:44 +00:00
2013-03-10 08:54:37 +00:00
object : Value -> Dict String Value
object v = case v of { Object o -> o ; _ -> Dict . empty }
2013-03-03 05:43:44 +00:00
-- Extract Elm values from dictionaries of Json values
2013-03-24 12:45:56 +00:00
-- 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.
2013-03-10 08:54:37 +00:00
find get base =
let f key dict =
2013-03-03 05:43:44 +00:00
case Dict . lookup key dict of
Nothing -> base
2013-03-10 08:54:37 +00:00
Just v -> get v
2013-03-03 05:43:44 +00:00
in f
2013-03-24 12:45:56 +00:00
-- 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.
2013-03-03 05:43:44 +00:00
findString : String -> Dict String Value -> String
2013-03-10 08:54:37 +00:00
findString = find string " "
2013-03-03 05:43:44 +00:00
2013-03-24 12:45:56 +00:00
-- 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
2013-03-10 08:54:37 +00:00
findNumber : String -> Dict String Value -> Float
findNumber = find number 0
2013-03-03 05:43:44 +00:00
2013-03-24 12:45:56 +00:00
-- 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.
2013-03-10 08:54:37 +00:00
findBoolean : String -> Dict String Value -> Bool
findBoolean = find boolean False
2013-03-03 05:43:44 +00:00
2013-03-24 12:45:56 +00:00
-- 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.
2013-03-10 08:54:37 +00:00
findArray : String -> Dict String Value -> [ Value ]
findArray = find array [ ]
2013-03-03 05:43:44 +00:00
2013-03-24 12:45:56 +00:00
-- 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.
2013-03-10 08:54:37 +00:00
findObject : String -> Dict String Value -> Dict String Value
findObject = find object Dict . empty