This is just a proof-of-concept, so it doesn’t cover everything – and the focus is on keeping the JSON structure as shallow and simple as possible. The output should be something like the following:
"summary":"By publishing content as JSON, retrieving it using webservices and using a framework like Knockout, we can do templating client-side.",
"keywords":"this, page, is, cool",
The full source code of the TBB is shown below, a few things worth noting are:
For easy access, there is no fields, or content container element for content fields – they are added directly to the root element, metadata fields are added to a sub element; metadata
For component link fields, which link to MM component, we publish the binary and add published url and metadata to the output(see the image field example above). For non MM component link fields, just the component uri is output
For keyword fields, we add the keyword id, key, title and description.
For date fields, we convert to JSON friendly ISO 8601 format (this will save some pain)
For embedded schema fields, we build the substructure you would expect (see the standard metadata field above)
Rich Text fields are skipped – these tend to be very heavy and serialize messily, and I want to keep my JSON lightweight – I don’t intend to use RTF in my JS templates anyway – we will use traditional templating methods for that.