Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Data structure agnostic JSON serialization
Tag: v0.0.1

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.


json-builder is a data structure agnostic json serialization library. Most existing serializers operate on a specific data structure that corresponds to a json value. Other pieces of data must then marshaled into that data structure before they can be serialized to json.

json-builder avoids this marshaling, and thus is potentially more efficient when you want to serialize arbitrary data structures. Moreover, I've found that writing a serializer using this API to be somewhat simpler than writing a marshaller into the data structures used by the json or aeson packages.

Unfortunately, json-builder cannot yet read or process json data. Moreover, it's not clear to me how to pull a similar kind of trick above, and avoid unnecessary data structures.

This library revolves around a single typeclass Json.Value that represents pieces of data that can be serialized to the Json format. It's member is toBuilder, which returns a builder that represents the concrete syntax of that value.

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

If a user-supplied instance of Json.Value uses functions from blaze-builder, then it is possible to emit invalid JSON strings. This is one disadvantage relative to specifying a data structure. However valid JSON is guaranteed if these instances use only valid toBuilder methods to create builders.

Arrays are represented by an abstract type with a singleton constructor element and a monoid instance. Thus arbitrary sequences can be serialized using mempty and mappend.

element :: Value a => a -> Array
instance Monoid Array

Objects are similar. There is a typeclass "Key" used to distinguish Strings from other Json values which cannot be keys, a singleton constructor, and a monoid instance. Note that rows with duplicate keys will appear in the output; it is up to the user of this interface to ensure that keys are unique.

class Value a => Key a

row :: (Key a, Value b) => a -> b -> Object
instance Monoid Object
Something went wrong with that request. Please try again.