module Text.JSON.Combinator
(
jnot
, withNumber
, withString
, withArray
, withObject
, withObjectFields
, jzero
, jemptystring
, jemptyarray
, jemptyobject
, jsinglearray
, jsingleobject
, getBool
, getNumber
, getString
, getArray
, getObject
, isBool
, isTrue
, isFalse
, isNumber
, isString
, isArray
, isObject
, numberOr
, stringOr
, arrayOr
, objectOr
, fieldsOr
, valuesOr
, numberOrZero
, stringOrEmpty
, arrayOrEmpty
, objectOrEmpty
, objectFieldsOrEmpty
, objectValuesOrEmpty
, usingNumber
, usingString
, usingArray
, usingObject
, usingObjectFields
, usingObjectValues
, hasField
, (-?)
, (-|)
, fieldOr
, fieldOrNull
, fieldOrTrue
, fieldOrFalse
, fieldOrZero
, fieldOrEmptyString
, fieldOrEmptyArray
, fieldOrEmptyObject
, hasField'
, (-??)
, field'
, (-||)
, field'Or
, field'OrNull
, field'OrTrue
, field'OrFalse
, field'OrZero
, field'OrEmptyString
, field'OrEmptyArray
, field'OrEmptyObject
, interactJSON
, interactJSON'
, withJSON
, readJSONFile
, writeJSONFile
, interactJSONFile
, interactJSONFile'
, withJSONFile
, module Text.JSON.JSONLike
, module Text.JSON.JSONField
, module Text.JSON.JSONParse
, module Text.JSON.JSONPrepend
, module Text.JSON.JSONPrint
) where
import Text.JSON.JSONLike
import Text.JSON.JSONField
import Text.JSON.JSONParse
import Text.JSON.JSONPrepend
import Text.JSON.JSONPrint
import Text.JSON.Interact
import Text.JSON.InteractFile
import Control.Applicative
import Data.Maybe
import Data.Monoid
import qualified Data.Foldable as F
isTrue ::
JSONLike j s a o f =>
j
-> Bool
isTrue =
foldJSON False True False (const False) (const False) (const False) (const False)
isFalse ::
JSONLike j s a o f =>
j
-> Bool
isFalse =
foldJSON False False True (const False) (const False) (const False) (const False)
isBool ::
JSONLike j s a o f =>
j
-> Bool
isBool =
liftA2 (||) isTrue isFalse
isNumber ::
JSONLike j s a o f =>
j
-> Bool
isNumber =
foldJSON False False False (const True) (const False) (const False) (const False)
isString ::
JSONLike j s a o f =>
j
-> Bool
isString =
foldJSON False False False (const False) (const True) (const False) (const False)
isArray ::
JSONLike j s a o f =>
j
-> Bool
isArray =
foldJSON False False False (const False) (const False) (const True) (const False)
isObject ::
JSONLike j s a o f =>
j
-> Bool
isObject =
foldJSON False False False (const False) (const False) (const False) (const True)
jnot ::
JSONLike j s a o f =>
j
-> j
jnot j =
if isTrue j
then jfalse
else
if isFalse j
then
jtrue
else
j
withNumber ::
JSONLike j s a o f =>
(Rational -> Rational)
-> j
-> j
withNumber f j =
foldJSON j j j (jnumber . f) (const j) (const j) (const j) j
withString ::
JSONLike j s a o f =>
(s -> s)
-> j
-> j
withString f j =
foldJSON j j j (const j) (jstring . f) (const j) (const j) j
withArray ::
JSONLike j s a o f =>
(a j -> a j)
-> j
-> j
withArray f j =
foldJSON j j j (const j) (const j) (jarray . f) (const j) j
withObject ::
JSONLike j s a o f =>
(o j -> o j)
-> j
-> j
withObject f j =
foldJSON j j j (const j) (const j) (const j) (jobject . f) j
withObjectFields ::
(Functor o, JSONLike j s a o f) =>
(j -> j)
-> j
-> j
withObjectFields f =
withObject (fmap f)
getBool ::
JSONLike j s a o f =>
j
-> Maybe Bool
getBool =
foldJSON Nothing (Just True) (Just False) (const Nothing) (const Nothing) (const Nothing) (const Nothing)
getNumber ::
JSONLike j s a o f =>
j
-> Maybe Rational
getNumber =
foldJSON Nothing Nothing Nothing Just (const Nothing) (const Nothing) (const Nothing)
getString ::
JSONLike j s a o f =>
j
-> Maybe s
getString =
foldJSON Nothing Nothing Nothing (const Nothing) Just (const Nothing) (const Nothing)
getArray ::
JSONLike j s a o f =>
j
-> Maybe (a j)
getArray =
foldJSON Nothing Nothing Nothing (const Nothing) (const Nothing) Just (const Nothing)
getObject ::
JSONLike j s a o f =>
j
-> Maybe (o j)
getObject =
foldJSON Nothing Nothing Nothing (const Nothing) (const Nothing) (const Nothing) Just
numberOr ::
JSONLike j s a o f =>
Rational
-> j
-> Rational
numberOr x =
fromMaybe x . getNumber
stringOr ::
JSONLike j s a o f =>
s
-> j
-> s
stringOr x =
fromMaybe x . getString
arrayOr ::
JSONLike j s a o f =>
a j
-> j
-> a j
arrayOr x =
fromMaybe x . getArray
objectOr ::
JSONLike j s a o f =>
o j
-> j
-> o j
objectOr x =
fromMaybe x . getObject
fieldsOr ::
JSONField j f =>
[f]
-> j
-> [f]
fieldsOr x =
fromMaybe x . fields
valuesOr ::
JSONField j f =>
[j]
-> j
-> [j]
valuesOr x =
fromMaybe x . values
usingNumber ::
JSONLike j s a o f =>
x
-> (Rational -> x)
-> j
-> x
usingNumber a f =
maybe a f . getNumber
usingString ::
JSONLike j s a o f =>
x
-> (s -> x)
-> j
-> x
usingString a f =
maybe a f . getString
usingArray ::
JSONLike j s a o f =>
x
-> (a j -> x)
-> j
-> x
usingArray a f =
maybe a f . getArray
usingObject ::
JSONLike j s a o f =>
x
-> (o j -> x)
-> j
-> x
usingObject a f =
maybe a f . getObject
usingObjectFields ::
JSONField j f =>
a
-> ([f] -> a)
-> j
-> a
usingObjectFields a f =
maybe a f . fields
usingObjectValues ::
JSONField j f =>
a
-> ([j] -> a)
-> j
-> a
usingObjectValues a f =
maybe a f . values
jzero ::
JSONLike j s a o f =>
j
jzero =
jnumber 0
jemptystring ::
(Monoid s, JSONLike j s a o f) =>
j
jemptystring =
jstring mempty
jemptyarray ::
(Monoid (a j), JSONLike j s a o f) =>
j
jemptyarray =
jarray mempty
jemptyobject ::
(Monoid (o j), JSONLike j s a o f) =>
j
jemptyobject =
jobject mempty
jsinglearray ::
(Applicative a, JSONLike j s a o f) =>
j
-> j
jsinglearray =
jarray . pure
jsingleobject ::
(Applicative o, JSONLike j s a o f) =>
j
-> j
jsingleobject =
jobject . pure
(-|) ::
JSONField j s =>
s
-> j
-> Maybe j
(-|) =
field
numberOrZero ::
JSONLike j s a o f =>
j
-> Rational
numberOrZero =
numberOr 0
stringOrEmpty ::
(JSONLike j s a o f, Monoid s) =>
j
-> s
stringOrEmpty =
stringOr mempty
arrayOrEmpty ::
(JSONLike j s a o f, Monoid (a j)) =>
j
-> a j
arrayOrEmpty =
arrayOr mempty
objectOrEmpty ::
(JSONLike j s a o f, Monoid (o j)) =>
j
-> o j
objectOrEmpty =
objectOr mempty
objectFieldsOrEmpty ::
(JSONField j f, Monoid (o j)) =>
j
-> [f]
objectFieldsOrEmpty =
fieldsOr []
objectValuesOrEmpty ::
(JSONField j f, Monoid (o j)) =>
j
-> [j]
objectValuesOrEmpty =
valuesOr []
hasField ::
JSONField j f =>
f
-> j
-> Bool
hasField s =
isJust . field s
(-?) ::
JSONField j f =>
f
-> j
-> Bool
(-?) =
hasField
fieldOr ::
JSONField j f =>
f
-> j
-> j
-> j
fieldOr s =
flip fromMaybe . field s
fieldOrNull ::
(JSONLike j s a o f, JSONField j f) =>
f
-> j
-> j
fieldOrNull s j =
fieldOr s j jnull
fieldOrTrue ::
(JSONLike j s a o f, JSONField j f) =>
f
-> j
-> j
fieldOrTrue s j =
fieldOr s j jtrue
fieldOrFalse ::
(JSONLike j s a o f, JSONField j f) =>
f
-> j
-> j
fieldOrFalse s j =
fieldOr s j jfalse
fieldOrZero ::
(JSONLike j s a o f, JSONField j f) =>
f
-> j
-> j
fieldOrZero s j =
fieldOr s j jzero
fieldOrEmptyString ::
(JSONLike j s a o f, JSONField j f, Monoid s) =>
f
-> j
-> j
fieldOrEmptyString s j =
fieldOr s j jemptystring
fieldOrEmptyArray ::
(JSONLike j s a o f, JSONField j f, Monoid (a j)) =>
f
-> j
-> j
fieldOrEmptyArray s j =
fieldOr s j jemptyarray
fieldOrEmptyObject ::
(JSONLike j s a o f, JSONField j f, Monoid (o j)) =>
f
-> j
-> j
fieldOrEmptyObject s j =
fieldOr s j jemptyobject
field' ::
(JSONField j f, F.Foldable t) =>
t f
-> j
-> Maybe j
field' =
flip (F.foldrM field)
(-||) ::
(JSONField j f, F.Foldable t) =>
t f
-> j
-> Maybe j
(-||) =
field'
hasField' ::
(JSONField j f, F.Foldable t) =>
t f
-> j
-> Bool
hasField' s =
isJust . field' s
(-??) ::
(JSONField j f, F.Foldable t) =>
t f
-> j
-> Bool
(-??) =
hasField'
field'Or ::
(JSONField j f, F.Foldable t) =>
j
-> t f
-> j
-> j
field'Or d s =
fromMaybe d . field' s
field'OrNull ::
(JSONLike j s a o f, JSONField j f, F.Foldable t) =>
t f
-> j
-> j
field'OrNull =
field'Or jnull
field'OrTrue ::
(JSONLike j s a o f, JSONField j f, F.Foldable t) =>
t f
-> j
-> j
field'OrTrue =
field'Or jtrue
field'OrFalse ::
(JSONLike j s a o f, JSONField j f, F.Foldable t) =>
t f
-> j
-> j
field'OrFalse =
field'Or jfalse
field'OrZero ::
(JSONLike j s a o f, JSONField j f, F.Foldable t) =>
t f
-> j
-> j
field'OrZero =
field'Or jzero
field'OrEmptyString ::
(JSONLike j s a o f, JSONField j f, F.Foldable t, Monoid s) =>
t f
-> j
-> j
field'OrEmptyString =
field'Or jemptystring
field'OrEmptyArray ::
(JSONLike j s a o f, JSONField j f, F.Foldable t, Monoid (a j)) =>
t f
-> j
-> j
field'OrEmptyArray =
field'Or jemptyarray
field'OrEmptyObject ::
(JSONLike j s a o f, JSONField j f, F.Foldable t, Monoid (o j)) =>
t f
-> j
-> j
field'OrEmptyObject =
field'Or jemptyobject
readJSONFile ::
(JSONParse j a e, InteractFile a) =>
FilePath
-> IO (Either e j)
readJSONFile f =
parseJSON f `fmap` readFile' f
writeJSONFile ::
(InteractFile b, JSONPrint a b) =>
FilePath
->
a
-> IO ()
writeJSONFile f =
writeFile' f . printJSON
interactJSON ::
(JSONPrint j' s, JSONParse j s e, Interact s) =>
(Either e j -> j')
-> IO ()
interactJSON f =
interact' (printJSON . f . parseJSON "stdin")
interactJSON' ::
(JSONPrint j' s, JSONParse j s e, Interact s) =>
(e -> j')
-> (j -> j')
-> IO ()
interactJSON' l =
interactJSON . either l
withJSON ::
(Interact p, Interact s, JSONPrint j' s, JSONParse j p e) =>
(e -> IO ())
-> (j -> j')
-> IO ()
withJSON f g =
getContents' >>= either f (putStr' . printJSON . g) . parseJSON "stdin"
interactJSONFile ::
(InteractFile p, InteractFile s, JSONPrint j' s, JSONParse j p e) =>
(Either e j -> j')
-> FilePath
-> FilePath
-> IO ()
interactJSONFile f i o =
readFile' i >>= writeFile' o . (printJSON . f) . parseJSON "stdin"
interactJSONFile' ::
(InteractFile p, InteractFile s, JSONPrint j' s, JSONParse j p e) =>
(e -> j')
-> (j -> j')
-> FilePath
-> FilePath
-> IO ()
interactJSONFile' l =
interactJSONFile . either l
withJSONFile ::
(InteractFile p, InteractFile s, JSONPrint j' s, JSONParse j p e) =>
(e -> IO ())
-> (j -> j')
-> FilePath
-> FilePath
-> IO ()
withJSONFile f g i o =
readFile' i >>= either f (writeFile' o . printJSON . g) . parseJSON i