[PutJSON Bryan O'Sullivan **20080222063743] { addfile ./examples/ch06/PutJSON.hs hunk ./en/ch06-library.xml 221 + + + + Turning Haskell values into JSON data + + Now that we have a Haskell representation for JSON's types, + we'd like to be able to take Haskell values and render them + as JSON data. + + There are a few ways we could go about this. Perhaps the + most direct would be to write a rendering function that + prints a value in JSON form. hunk ./examples/ch06/PutJSON.hs 1 +module PutJSON where + +import SimpleJSON + +putJValue (JString s) = putStr (show s) +putJValue (JNumber n) = putStr (show n) +putJValue (JBool True) = putStr "true" +putJValue (JBool False) = putStr "false" +putJValue JNull = putStr "null" + +putJValue (JObject xs) = do + putChar '{' + case xs of + [] -> return () + (p:ps) -> do putPair p + putPairs ps + putChar '}' + where putPair (k,v) = do putStr (show k) + putStr ": " + putJValue v + putPairs (p:ps) = do putStr ", " + putPair p + putPairs ps + putPairs [] = return () + +putJValue (JArray xs) = do + putChar '[' + case xs of + [] -> return () + (p:ps) -> do putJValue p + putJValues ps + putChar ']' + where putJValues (p:ps) = do putStr ", " + putJValue p + putJValues ps + putJValues _ = return () hunk ./web/Index.hs 43 - $ Alpha "2008-01-21" + $ Beta "2008-01-21" hunk ./web/Index.hs 45 - Unpublished + $ Alpha "2008-02-21" }