on 577fc17 help sign in
elk/libraries/Json.elm
49 lines 1.4 kB history raw
2module Json where
4{-| Library for working with [JSON](https://en.wikipedia.org/wiki/JSON) values.
6@docs Value, fromString, toString
8-}
10import Dict
11import Maybe (Maybe)
12import Native.Json
14{-| This datatype can represent all valid JSON values. A `Value` can be sent
15through ports, and the runtime will do a conversion like this:
17 [2,'abc',null] == Array [Number 2, String "abc", Null]
19JSON is an intentional subset of JavaScript values which excludes functions.
20This ensures that impure functions cannot sneak into Elm through ports.
21-}
22data Value
23 = String String
24 | Number Float
25 | Boolean Bool
26 | Null
27 | Array [Value]
28 | Object (Dict.Dict String Value)
31{-| Convert a `Value` into a prettified string.
32The first argument is a separator token (e.g. \" \", \"\\n\", etc.) that will
33be used for indentation in the prettified string version of the JSON.
35 toString "" Null == "null"
36 toString "" (Number 4.0) == "4.0"
37-}
38toString : String -> Value -> String
39toString sep value = Native.Json.toString sep value
41{-| Parse a string representation of a proper JSON object into
42its Elm representation.
44 fromString "null" == Just Null
45 fromString "[true,3]" == Just (Array [Boolean True, Number 3])
46 fromString "{'abc':4}" == Just (Object (Dict.fromList [("abc", Number 4)]))
47-}
48fromString : String -> Maybe Value
49fromString str = Native.Json.fromString str