8 | module JSON.Simple.ToJSON
10 | import Data.List.Quantifiers as LQ
12 | import Data.SortedMap
15 | import Data.Vect.Quantifiers as VQ
17 | import JSON.Simple.Option
21 | interface ToJSON a where
22 | constructor MkToJSON
27 | interface ToJSONKey a where
28 | constructor MkToJSONKey
32 | jpair : ToJSONKey k => ToJSON a => k -> a -> (String,JSON)
33 | jpair s val = (toKey s, toJSON val)
36 | encode : ToJSON a => a -> String
37 | encode = show . toJSON
45 | singleField : String -> JSON -> JSON
46 | singleField s x = JObject [(s,x)]
53 | twoElemArray : String -> JSON -> JSON
54 | twoElemArray s x = JArray [JString s, x]
61 | taggedObject : (tagField, contentField, tag : String) -> JSON -> JSON
62 | taggedObject tf cf tag x = JObject [(tf, JString tag), (cf, x)]
69 | ToJSON JSON where toJSON = id
76 | ToJSON String where toJSON = JString
79 | ToJSONKey String where toKey = id
82 | ToJSON Char where toJSON = JString . singleton
85 | ToJSONKey Char where toKey = singleton
88 | ToJSON Double where toJSON = JDouble
91 | ToJSON Bits8 where toJSON = JInteger . cast
94 | ToJSON Bits16 where toJSON = JInteger . cast
97 | ToJSON Bits32 where toJSON = JInteger . cast
100 | ToJSON Bits64 where toJSON = JInteger . cast
103 | ToJSON Int8 where toJSON = JInteger . cast
106 | ToJSON Int16 where toJSON = JInteger . cast
109 | ToJSON Int32 where toJSON = JInteger . cast
112 | ToJSON Int64 where toJSON = JInteger . cast
115 | ToJSON Int where toJSON = JInteger . cast
118 | ToJSON Integer where toJSON = JInteger
121 | ToJSON Nat where toJSON = JInteger . cast
124 | ToJSON Bool where toJSON = JBool
127 | ToJSONKey Double where toKey = cast
130 | ToJSONKey Bits8 where toKey = cast
133 | ToJSONKey Bits16 where toKey = cast
136 | ToJSONKey Bits32 where toKey = cast
139 | ToJSONKey Bits64 where toKey = cast
142 | ToJSONKey Int8 where toKey = cast
145 | ToJSONKey Int16 where toKey = cast
148 | ToJSONKey Int32 where toKey = cast
151 | ToJSONKey Int64 where toKey = cast
154 | ToJSONKey Int where toKey = cast
157 | ToJSONKey Integer where toKey = cast
160 | ToJSONKey Nat where toKey = cast
163 | ToJSONKey Bool where toKey = show
166 | ToJSON a => ToJSON (Maybe a) where
167 | toJSON Nothing = JNull
168 | toJSON (Just a) = toJSON a
171 | ToJSON a => ToJSON (List a) where
172 | toJSON = JArray . map toJSON
175 | ToJSON a => ToJSON (SnocList a) where
176 | toJSON = toJSON . (<>> [])
179 | ToJSON a => ToJSON (List1 a) where
180 | toJSON = toJSON . forget
183 | ToJSON a => ToJSON (Vect n a) where
184 | toJSON = toJSON . toList
188 | toJSON () = JArray Nil
191 | ToJSON a => ToJSON b => ToJSON (Either a b) where
192 | toJSON (Left a) = JObject [jpair "Left" a]
193 | toJSON (Right b) = JObject [jpair "Right" b]
196 | ToJSON a => ToJSON b => ToJSON (a, b) where
197 | toJSON (x,y) = JArray [toJSON x, toJSON y]
200 | (ps : LQ.All.All (ToJSON . f) ts) => ToJSON (All f ts) where
201 | toJSON = JArray . forget . zipPropertyWith (\_,v => toJSON v) ps
204 | ToJSONKey k => ToJSON v => ToJSON (SortedMap k v) where
205 | toJSON = JObject . map (uncurry jpair) . SortedMap.toList
209 | (f : {0 x : a} -> p x -> q x -> r x)
213 | zipAllWith f [] [] = []
214 | zipAllWith f (px :: pxs) (qx :: qxs) = f px qx :: zipAllWith f pxs qxs
217 | (ps : VQ.All.All (ToJSON . f) ts) => ToJSON (VQ.All.All f ts) where
218 | toJSON = JArray . toList . forget . zipAllWith (\_,v => toJSON v) ps