[more updates to tutorial jeremy@n-heptane.com**20080710083249] { move ./null.sh ./identity.sh move ./pages/null.sh ./pages/identity.sh hunk ./HAppSHspTutorial.lhs 9 - -> {-# LANGUAGE CPP #-} - hunk ./HAppSHspTutorial.lhs 13 +
Note: To load this .lhs file into GHCi, set the options -ipages -cpp
+ hunk ./HAppSHspTutorial.lhs 122 -In this example, our State component e
hunk ./State.lhs 5 -> {-# OPTIONS_GHC -F -pgmF ./null.sh #-} +> {-# OPTIONS_GHC -F -pgmF ./identity.sh #-} hunk ./pages/Interface.lhs 1 +JSON, short for Javascript Object Notation, is a lightweight data-interchange format. It's primary appeal is that it is natively supported by Javascript, meaning you can create JSON objects directly in the javascript program text, and easily convert between JSON and javascript objects at runtime.
+ +We can think of the HAppS portion of our application as a server which takes HTTP requests and returns JSON data. This JSON data can be used by HSP for templating, by javascript on the client for AJAX type applications, or by 3rd party sites who are allowed to access our API.
+happs-hsp-template uses RJson for converting Haskell values to and from JSON. See the README in the RJson tarball for more information on using RJson.
+We define our datatypes which will be turned into JSON objects in pages/Interface.lhs. It is in the pages subdirectory because it needs to be imported by both our HAppS backend as well the the HSP templates. This ensures that they are both talking the same specification.
+Note the use -F -pgmF
in the OPTIONS_GHC
pragma:
Instead of running this file through trhsx we are running it through identity.sh. Unfortunately, this is just an ugly hack. The current implementation of trhsx does not understand how to parse Template Haskell. However, the happs-hsp-template attempts to run all the modules through trhsx, so we need this hack to override it. You will need a copy of the identity.sh in the top-level directory and the pages subdirectory. The identity.sh script is a simple preprocessor which leaves the contents unmodified. It is implemented like this:
+ +#ifdef HsColour ++ #!/bin/sh + cat $2 > $3 ++#endif + +
Next we define an type which will be used as JSON data.
+ hunk ./pages/Interface.lhs 34 +One key thing to note is that we use the record syntax in our type
+ declaration. This is so that RJson can automatically convert our
+ data-type into a JSON object. If we did not do this, we would have to
+ manually declare instances of ToJson
and
+ FromJson
for HitCounter.
Because we might want to store HitCounter in the HAppS State we also deriveSerialize
for it, and create a Version
instance.