aboutsummaryrefslogtreecommitdiff
path: root/spec/src/Thermoprint/Printout.hs
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2016-01-21 09:12:20 +0000
committerGregor Kleen <gkleen@yggdrasil.li>2016-01-21 09:12:20 +0000
commit58d26527560f8d3e21f5f50ec9a5e993cf044ae0 (patch)
treef6dec7ab95bcdc0fd10e9cb9eed6d76040c1cd51 /spec/src/Thermoprint/Printout.hs
parentc51e334bc5a537300d9421f43bd355850e2013b4 (diff)
downloadthermoprint-58d26527560f8d3e21f5f50ec9a5e993cf044ae0.tar
thermoprint-58d26527560f8d3e21f5f50ec9a5e993cf044ae0.tar.gz
thermoprint-58d26527560f8d3e21f5f50ec9a5e993cf044ae0.tar.bz2
thermoprint-58d26527560f8d3e21f5f50ec9a5e993cf044ae0.tar.xz
thermoprint-58d26527560f8d3e21f5f50ec9a5e993cf044ae0.zip
More concise JSON for Printout
Diffstat (limited to 'spec/src/Thermoprint/Printout.hs')
-rw-r--r--spec/src/Thermoprint/Printout.hs13
1 files changed, 13 insertions, 0 deletions
diff --git a/spec/src/Thermoprint/Printout.hs b/spec/src/Thermoprint/Printout.hs
index 89be802..aae71aa 100644
--- a/spec/src/Thermoprint/Printout.hs
+++ b/spec/src/Thermoprint/Printout.hs
@@ -52,6 +52,7 @@ import Data.Monoid (Monoid(..), (<>))
52import Data.List (dropWhile, dropWhileEnd, groupBy, genericLength, genericReplicate) 52import Data.List (dropWhile, dropWhileEnd, groupBy, genericLength, genericReplicate)
53 53
54import Data.Sequence as Seq (fromList, null, singleton) 54import Data.Sequence as Seq (fromList, null, singleton)
55import Data.Sequence (ViewL(..), viewl)
55 56
56import Data.Function (on) 57import Data.Function (on)
57 58
@@ -80,6 +81,18 @@ instance PersistFieldSql Printout where
80-- | A 'Paragraph' is a non-seperated sequence of 'Chunk's 81-- | A 'Paragraph' is a non-seperated sequence of 'Chunk's
81type Paragraph = Seq Chunk 82type Paragraph = Seq Chunk
82 83
84instance FromJSON Paragraph where
85 parseJSON o@(Array _) = Seq.fromList <$> parseJSON o
86 parseJSON o@(Object _) = Seq.singleton <$> parseJSON o
87 parseJSON o@(String _) = Seq.singleton <$> parseJSON o
88 parseJSON v = typeMismatch "Paragraph" v
89
90instance ToJSON Paragraph where
91 toJSON cs
92 | (a :< as) <- viewl cs
93 , Seq.null as = toJSON a
94 | otherwise = toJSON $ toList cs
95
83-- | We introduce both 'Chunk' and 'Paragraph' mainly to allow 'Raw'. 96-- | We introduce both 'Chunk' and 'Paragraph' mainly to allow 'Raw'.
84-- 97--
85-- Were we to disallow 'Raw', 'Block' would be identical to 'Paragraph' 98-- Were we to disallow 'Raw', 'Block' would be identical to 'Paragraph'