json-litobj-0.1.0.0: Extends Text.JSON to handle literal JS objects.

Copyright(c) Jonathan Kochems 2015
LicenseBSD-3
Maintainerjonathan.kochems@gmail.com
Stabilityexperimental
Portabilityportable
Safe HaskellSafe-Inferred
LanguageHaskell2010

Text.JSON.Permissive

Description

This module extends Text.JSON to enable the decoding of strings containing literal JS objects. In particular, it relaxes the restriction that fields in JSON objects must be strings.

For example:

 JSON conformant:                   literal JS object:
 { "foo" : "bar" }                  { foo : "bar" }

Synopsis

Documentation

decodePermissive :: String -> Result (JSObject JSValue) Source

decodes a string encoding a JSON object in a relaxed fashion

decode "{ foo : \"bar\" }"
Error "Malformed JSON: expecting string: foo : \"b"

decodePermissive "{ foo : \"bar\" }"      == Ok $ toJSObject [("foo", JSString $ toJSString "bar")]
decodePermissive "{ \"foo\" : \"bar\" }"  == Ok $ toJSObject [("foo", JSString $ toJSString "bar")]

get_fields :: JSObject a -> [String] Source

returns the list of fields of a JSON Object

do obj <- decodePermissive "{ foo : \"bar\", fooz : \"baz\" }"
   return $ get_fields obj  == Ok ["foo", "fooz"]
do obj <- decodePermissive "{ foo : \"bar\", fooz : \"baz\" }"
   return $ get_field obj $ head $ get_fields obj  == Ok (Just $ JSString $ toJSString "bar" )