Skip to content

Commit

Permalink
Initial haddock comments and .cabal description
Browse files Browse the repository at this point in the history
  • Loading branch information
lpsmith committed Jun 3, 2011
1 parent 7178b68 commit 387e3f3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
39 changes: 26 additions & 13 deletions json-builder.cabal
Expand Up @@ -5,28 +5,41 @@ License: BSD3
License-file: LICENSE
Author: Leon P Smith
Maintainer: leon@melding-monads.com
Stability: experimental
Copyright: (c) 2011 Leon P Smith
Category: Text, Web, JSON
Category: JSON
Build-type: Simple
Cabal-version: >=1.2

Cabal-version: >= 1.6
homepage: http://github.com/lpsmith/json-builder
bug-reports: http://github.com/lpsmith/json-builder/issues
description:
Most json packages define a data structure that corresponds to json values.
To serialize other values to a json string, then that value must be
marshalled into the json data structure.
.
This library avoids this marshalling step, and is thus potentially more
efficient when serializing arbitrary data structures. Unfortunately
json-builder cannot yet read or process json data, and it's not clear
to me yet how pull a similar kind of trick to avoid unnecessary data
structures when parsing json data into arbitrary data types.

Library
hs-source-dirs: src
-- Modules exported by the library.
Exposed-modules: Data.Json.Builder

-- Packages needed in order to build this package.

Build-depends: base >= 4 && < 5,
blaze-builder,
blaze-textual,
bytestring,
containers,
text,
blaze-textual,
utf8-string

-- Modules not exported by this package.
-- Other-modules:

-- Extra tools (e.g. alex, hsc2hs, ...) needed to build the source.
-- Build-tools:

source-repository head
type: git
location: http://github.com/lpsmith/json-builder

source-repository this
type: git
location: http://github.com/lpsmith/json-builder
tag: v0.0
28 changes: 27 additions & 1 deletion src/Data/Json/Builder.hs
Expand Up @@ -58,14 +58,27 @@ import Data.ByteString.Internal ( c2w )
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL

---- The "core" of json-builder
-- | The 'Key' typeclass represents types that are rendered
-- into json strings. They are special because only strings
-- can appear as field names of a json objects.

class Value a => Key a where
escape :: a -> Escaped

-- | The 'Value' typeclass represents types that can be rendered
-- into valid json syntax.

class Value a where
toBuilder :: a -> Blaze.Builder

-- | The 'Escaped' type is a special Builder value that represents a UTF-8
-- encoded string with all necessary characters json-escaped. These builders
-- must not render the opening or closing quotes, which are instead rendered
-- by 'toBuilder'. This is so that Json strings can be efficiently constructed
-- from multiple Haskell strings without actually concatinating the Haskell
-- strings (which might require some kind of conversion in addition to
-- concatination.)

newtype Escaped = Escaped Blaze.Builder deriving (Monoid)

instance Key Escaped where
Expand All @@ -81,6 +94,12 @@ comma b f True = b `mappend` f False
comma b f False = fromChar ',' `mappend` b `mappend` f False
{-# INLINE comma #-}

-- | The 'Object' type represents a builder that constructs syntax for a
-- json object. It has a singleton constructor 'row', and an instance of
-- monoid, so that arbitrary objects can be constructed. Note that
-- duplicate field names will appear in the output, so it is up to the
-- user of this interface to avoid duplicate field names.

newtype Object = Object CommaTracker

instance Value Object where
Expand All @@ -90,11 +109,16 @@ instance Monoid Object where
mempty = Object id
mappend (Object f) (Object g) = Object (f . g)

-- | The 'row' constructs a json object consisting of exactly one field.
-- These objects can be concatinated using 'mappend'.
row :: (Key k, Value a) => k -> a -> Object
row k a = Object syntax
where
syntax = comma (mconcat [ toBuilder k, fromChar ':', toBuilder a ])

-- | The 'Array' type represents a builder that constructs syntax for a
-- json array. It has a singleton constructor 'element' and an instance of
-- monoid, so that arbitrary arrays can be constructed.

newtype Array = Array CommaTracker

Expand All @@ -105,6 +129,8 @@ instance Monoid Array where
mempty = Array id
mappend (Array f) (Array g) = Array (f . g)

-- | The 'element' function constructs a json array consisting of exactly
-- one value. These arrays can be concatinated using 'mappend'.
element :: Value a => a -> Array
element a = Array $ comma (toBuilder a)

Expand Down

0 comments on commit 387e3f3

Please sign in to comment.