{-# LANGUAGE CPP #-}
-- |
--
-- Builder structures for 'JObject's
--
module Waargonaut.Encode.Builder.JObject (jObjectBuilder) where

#if !MIN_VERSION_base(4,11,0)
import           Data.Monoid                        ((<>))
#endif

import           Waargonaut.Types.JObject           (JAssoc (..), JObject (..))

import           Waargonaut.Encode.Builder.CommaSep (commaSeparatedBuilder)
import           Waargonaut.Encode.Builder.JString  (jStringBuilder)
import           Waargonaut.Encode.Builder.Types    (Builder (..))

-- | Builder for a single "key:value" pair.
jAssocBuilder
  :: Monoid b
  => (Builder t b -> ws -> b)
  -> ((Builder t b -> ws -> b) -> Builder t b -> a -> b)
  -> Builder t b
  -> JAssoc ws a
  -> b
jAssocBuilder :: (Builder t b -> ws -> b)
-> ((Builder t b -> ws -> b) -> Builder t b -> a -> b)
-> Builder t b
-> JAssoc ws a
-> b
jAssocBuilder Builder t b -> ws -> b
ws (Builder t b -> ws -> b) -> Builder t b -> a -> b
aBuilder Builder t b
bldr (JAssoc JString
k ws
ktws ws
vpws a
v) =
  Builder t b -> JString -> b
forall b t. Monoid b => Builder t b -> JString -> b
jStringBuilder Builder t b
bldr JString
k b -> b -> b
forall a. Semigroup a => a -> a -> a
<> Builder t b -> ws -> b
ws Builder t b
bldr ws
ktws b -> b -> b
forall a. Semigroup a => a -> a -> a
<> Builder t b -> Char -> b
forall t b. Builder t b -> Char -> b
fromChar Builder t b
bldr Char
':' b -> b -> b
forall a. Semigroup a => a -> a -> a
<> Builder t b -> ws -> b
ws Builder t b
bldr ws
vpws b -> b -> b
forall a. Semigroup a => a -> a -> a
<> (Builder t b -> ws -> b) -> Builder t b -> a -> b
aBuilder Builder t b -> ws -> b
ws Builder t b
bldr a
v

-- | Construct a 'Builder' for an entire 'JObject', duplicate keys are preserved.
jObjectBuilder
  :: Monoid b
  => Builder t b
  -> (Builder t b -> ws -> b)
  -> ((Builder t b -> ws -> b) -> Builder t b -> a -> b)
  -> JObject ws a
  -> b
jObjectBuilder :: Builder t b
-> (Builder t b -> ws -> b)
-> ((Builder t b -> ws -> b) -> Builder t b -> a -> b)
-> JObject ws a
-> b
jObjectBuilder Builder t b
bldr Builder t b -> ws -> b
ws (Builder t b -> ws -> b) -> Builder t b -> a -> b
aBuilder (JObject CommaSeparated ws (JAssoc ws a)
c) =
  Builder t b
-> Char
-> Char
-> (Builder t b -> ws -> b)
-> (Builder t b -> JAssoc ws a -> b)
-> CommaSeparated ws (JAssoc ws a)
-> b
forall ws a t b.
Monoid b =>
Builder t b
-> Char
-> Char
-> (Builder t b -> ws -> b)
-> (Builder t b -> a -> b)
-> CommaSeparated ws a
-> b
commaSeparatedBuilder Builder t b
bldr Char
'{' Char
'}' Builder t b -> ws -> b
ws ((Builder t b -> ws -> b)
-> ((Builder t b -> ws -> b) -> Builder t b -> a -> b)
-> Builder t b
-> JAssoc ws a
-> b
forall b t ws a.
Monoid b =>
(Builder t b -> ws -> b)
-> ((Builder t b -> ws -> b) -> Builder t b -> a -> b)
-> Builder t b
-> JAssoc ws a
-> b
jAssocBuilder Builder t b -> ws -> b
ws (Builder t b -> ws -> b) -> Builder t b -> a -> b
aBuilder) CommaSeparated ws (JAssoc ws a)
c