diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2016-01-21 09:12:20 +0000 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2016-01-21 09:12:20 +0000 |
commit | 58d26527560f8d3e21f5f50ec9a5e993cf044ae0 (patch) | |
tree | f6dec7ab95bcdc0fd10e9cb9eed6d76040c1cd51 /spec | |
parent | c51e334bc5a537300d9421f43bd355850e2013b4 (diff) | |
download | thermoprint-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')
-rw-r--r-- | spec/src/Thermoprint/Printout.hs | 13 |
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(..), (<>)) | |||
52 | import Data.List (dropWhile, dropWhileEnd, groupBy, genericLength, genericReplicate) | 52 | import Data.List (dropWhile, dropWhileEnd, groupBy, genericLength, genericReplicate) |
53 | 53 | ||
54 | import Data.Sequence as Seq (fromList, null, singleton) | 54 | import Data.Sequence as Seq (fromList, null, singleton) |
55 | import Data.Sequence (ViewL(..), viewl) | ||
55 | 56 | ||
56 | import Data.Function (on) | 57 | import 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 |
81 | type Paragraph = Seq Chunk | 82 | type Paragraph = Seq Chunk |
82 | 83 | ||
84 | instance 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 | |||
90 | instance 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' |