8 | module JSON.Simple.ToJSON
10 | import Data.List.Quantifiers as LQ
12 | import Data.Singleton
13 | import Data.SortedMap
16 | import Data.Vect.Quantifiers as VQ
18 | import JSON.Simple.Option
22 | interface ToJSON a where
23 | constructor MkToJSON
28 | interface ToJSONKey a where
29 | constructor MkToJSONKey
33 | jpair : ToJSONKey k => ToJSON a => k -> a -> (String,JSON)
34 | jpair s val = (toKey s, toJSON val)
37 | encode : ToJSON a => a -> String
38 | encode = show . toJSON
46 | singleField : String -> JSON -> JSON
47 | singleField s x = JObject [(s,x)]
54 | twoElemArray : String -> JSON -> JSON
55 | twoElemArray s x = JArray [JString s, x]
62 | taggedObject : (tagField, contentField, tag : String) -> JSON -> JSON
63 | taggedObject tf cf tag x = JObject [(tf, JString tag), (cf, x)]
70 | ToJSON JSON where toJSON = id
77 | ToJSON String where toJSON = JString
80 | ToJSONKey String where toKey = id
83 | ToJSON Char where toJSON = JString . singleton
86 | ToJSONKey Char where toKey = singleton
89 | ToJSON Double where toJSON = JDouble
92 | ToJSON Bits8 where toJSON = JInteger . cast
95 | ToJSON Bits16 where toJSON = JInteger . cast
98 | ToJSON Bits32 where toJSON = JInteger . cast
101 | ToJSON Bits64 where toJSON = JInteger . cast
104 | ToJSON Int8 where toJSON = JInteger . cast
107 | ToJSON Int16 where toJSON = JInteger . cast
110 | ToJSON Int32 where toJSON = JInteger . cast
113 | ToJSON Int64 where toJSON = JInteger . cast
116 | ToJSON Int where toJSON = JInteger . cast
119 | ToJSON Integer where toJSON = JInteger
122 | ToJSON Nat where toJSON = JInteger . cast
125 | ToJSON Bool where toJSON = JBool
128 | ToJSONKey Double where toKey = cast
131 | ToJSONKey Bits8 where toKey = cast
134 | ToJSONKey Bits16 where toKey = cast
137 | ToJSONKey Bits32 where toKey = cast
140 | ToJSONKey Bits64 where toKey = cast
143 | ToJSONKey Int8 where toKey = cast
146 | ToJSONKey Int16 where toKey = cast
149 | ToJSONKey Int32 where toKey = cast
152 | ToJSONKey Int64 where toKey = cast
155 | ToJSONKey Int where toKey = cast
158 | ToJSONKey Integer where toKey = cast
161 | ToJSONKey Nat where toKey = cast
164 | ToJSONKey Bool where toKey = show
167 | ToJSON a => ToJSON (Maybe a) where
168 | toJSON Nothing = JNull
169 | toJSON (Just a) = toJSON a
172 | ToJSON a => ToJSON (List a) where
173 | toJSON = JArray . map toJSON
176 | ToJSON a => ToJSON (SnocList a) where
177 | toJSON = toJSON . (<>> [])
180 | ToJSON a => ToJSON (List1 a) where
181 | toJSON = toJSON . forget
184 | ToJSON a => ToJSON (Vect n a) where
185 | toJSON = toJSON . toList
189 | toJSON () = JArray Nil
192 | ToJSON a => ToJSON b => ToJSON (Either a b) where
193 | toJSON (Left a) = JObject [jpair "Left" a]
194 | toJSON (Right b) = JObject [jpair "Right" b]
197 | ToJSON a => ToJSON b => ToJSON (a, b) where
198 | toJSON (x,y) = JArray [toJSON x, toJSON y]
201 | (ps : LQ.All.All (ToJSON . f) ts) => ToJSON (All f ts) where
202 | toJSON = JArray . forget . zipPropertyWith (\_,v => toJSON v) ps
205 | ToJSONKey k => ToJSON v => ToJSON (SortedMap k v) where
206 | toJSON = JObject . map (uncurry jpair) . SortedMap.toList
210 | (f : {0 x : a} -> p x -> q x -> r x)
214 | zipAllWith f [] [] = []
215 | zipAllWith f (px :: pxs) (qx :: qxs) = f px qx :: zipAllWith f pxs qxs
218 | (ps : VQ.All.All (ToJSON . f) ts) => ToJSON (VQ.All.All f ts) where
219 | toJSON = JArray . toList . forget . zipAllWith (\_,v => toJSON v) ps
222 | {v : a} -> ToJSON a => ToJSON (Singleton v) where
223 | toJSON _ = toJSON v