elm/libraries/Json.elm

135 lines
4.1 KiB
Elm
Raw Normal View History

module Json where
2013-09-10 13:46:26 +00:00
{-| Library for working with [JSON](https://en.wikipedia.org/wiki/JSON) values.
2013-12-11 21:05:31 +00:00
# Json Values
@docs JsonValue
2013-09-10 13:46:26 +00:00
# Strings
@docs fromString, toString
# JS Strings
@docs fromJSString, toJSString
# JS Objects
@docs fromJSObject, toJSObject
-}
import Basics (..)
import Dict
2013-07-26 14:38:11 +00:00
import Maybe (Maybe)
import JavaScript as JS
import Native.Json
2013-07-26 14:38:11 +00:00
import JavaScript (JSObject, JSString)
2013-09-10 13:46:26 +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 JsonValue)
which is a mapping from strings to Json Values. -}
data JsonValue
= String String
| Number Float
| Boolean Bool
| Null
| Array [JsonValue]
| Object (Dict.Dict String JsonValue)
-- String Converters
2013-09-10 13:46:26 +00:00
{-| Convert a `JsonValue` 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. -}
toString : String -> JsonValue -> String
toString sep v = JS.toString (Native.Json.toJSString sep v)
2013-09-10 13:46:26 +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. -}
toJSString : String -> JsonValue -> JSString
toJSString = Native.Json.toJSString
2013-09-10 13:46:26 +00:00
{-| Parse a string representation of a proper JSON object into
its Elm representation. -}
fromString : String -> Maybe JsonValue
fromString s = Native.Json.fromJSString (JS.fromString s)
2013-09-10 13:46:26 +00:00
{-| Parse a JavaScript string representation of a proper JSON object into
its Elm representation. -}
fromJSString : JSString -> Maybe JsonValue
fromJSString = Native.Json.fromJSString
2013-09-10 13:46:26 +00:00
{-| Convert a JS object into a `JsonValue`. -}
fromJSObject : JSObject -> JsonValue
fromJSObject = Native.Json.fromJSObject
2013-09-10 13:46:26 +00:00
{-| 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
toJSObject = Native.Json.toJSObject
{-- 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
2013-09-10 13:46:26 +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. -}
find get base =
let f key dict =
case Dict.lookup key dict of
Nothing -> base
Just v -> get v
in f
2013-09-10 13:46:26 +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. -}
findString : String -> Object -> String
findString = find string ""
2013-09-10 13:46:26 +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 -}
findNumber : String -> Object -> Float
findNumber = find number 0
2013-09-10 13:46:26 +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. -}
findBoolean : String -> Object -> Bool
findBoolean = find boolean False
2013-09-10 13:46:26 +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. -}
findArray : String -> Object -> [JsonValue]
findArray = find array []
2013-09-10 13:46:26 +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. -}
findObject : String -> Object -> Object
findObject = find object Dict.empty
2013-09-10 13:46:26 +00:00
--}