module Text.JSON.Combinator.Examples where

import Text.JSON.Combinator
import qualified Text.JSON as J
import Text.JSONb
import Text.JSON.Parsec
import Control.Applicative
import qualified Data.ByteString as S

-- | Using both @Text.JSON@ and @Text.JSONb@,
-- then given a file name of a JSON object,
-- go through each field of that object and:
--
-- * If it is a string, reverse it
--
-- * If it is a number, add 1 to it
--
-- * If it is a boolean, invert it
--
-- then pretty-print the result of each of the parsers.
example1 ::
  FilePath -- ^ The JSON object file name
  -> IO ()
example1 p =
  do r1 <- readJSONFile p :: IO (Either String JSON)
     r2 <- readJSONFile p :: IO (Either ParseError J.JSValue)
     let f r = withObjectFields (
                                  withString r
                                . withNumber (+1)
                                . jnot
                                )
         k1 = printJSON . f S.reverse <$> r1
         k2 = printJSON . f reverse <$> r2
     either print S.putStrLn k1
     either print putStrLn k2