on 1.0.1.0 help sign in
typeable/aeson/tests/Encoders.hs
324 lines 13 kB history raw
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE GADTs #-}
3{-# LANGUAGE TemplateHaskell #-}
5module Encoders where
7import Prelude ()
8import Prelude.Compat
10import Data.Aeson.TH
11import Data.Aeson.Types
12import Options
13import Types
15--------------------------------------------------------------------------------
16-- Nullary encoders/decoders
17--------------------------------------------------------------------------------
19thNullaryToJSONString :: Nullary -> Value
20thNullaryToJSONString = $(mkToJSON defaultOptions ''Nullary)
22thNullaryToEncodingString :: Nullary -> Encoding
23thNullaryToEncodingString = $(mkToEncoding defaultOptions ''Nullary)
25thNullaryParseJSONString :: Value -> Parser Nullary
26thNullaryParseJSONString = $(mkParseJSON defaultOptions ''Nullary)
29thNullaryToJSON2ElemArray :: Nullary -> Value
30thNullaryToJSON2ElemArray = $(mkToJSON opts2ElemArray ''Nullary)
32thNullaryToEncoding2ElemArray :: Nullary -> Encoding
33thNullaryToEncoding2ElemArray = $(mkToEncoding opts2ElemArray ''Nullary)
35thNullaryParseJSON2ElemArray :: Value -> Parser Nullary
36thNullaryParseJSON2ElemArray = $(mkParseJSON opts2ElemArray ''Nullary)
39thNullaryToJSONTaggedObject :: Nullary -> Value
40thNullaryToJSONTaggedObject = $(mkToJSON optsTaggedObject ''Nullary)
42thNullaryToEncodingTaggedObject :: Nullary -> Encoding
43thNullaryToEncodingTaggedObject = $(mkToEncoding optsTaggedObject ''Nullary)
45thNullaryParseJSONTaggedObject :: Value -> Parser Nullary
46thNullaryParseJSONTaggedObject = $(mkParseJSON optsTaggedObject ''Nullary)
49thNullaryToJSONObjectWithSingleField :: Nullary -> Value
50thNullaryToJSONObjectWithSingleField =
51 $(mkToJSON optsObjectWithSingleField ''Nullary)
53thNullaryToEncodingObjectWithSingleField :: Nullary -> Encoding
54thNullaryToEncodingObjectWithSingleField =
55 $(mkToEncoding optsObjectWithSingleField ''Nullary)
57thNullaryParseJSONObjectWithSingleField :: Value -> Parser Nullary
58thNullaryParseJSONObjectWithSingleField = $(mkParseJSON optsObjectWithSingleField ''Nullary)
60gNullaryToJSONString :: Nullary -> Value
61gNullaryToJSONString = genericToJSON defaultOptions
63gNullaryToEncodingString :: Nullary -> Encoding
64gNullaryToEncodingString = genericToEncoding defaultOptions
66gNullaryParseJSONString :: Value -> Parser Nullary
67gNullaryParseJSONString = genericParseJSON defaultOptions
70gNullaryToJSON2ElemArray :: Nullary -> Value
71gNullaryToJSON2ElemArray = genericToJSON opts2ElemArray
73gNullaryToEncoding2ElemArray :: Nullary -> Encoding
74gNullaryToEncoding2ElemArray = genericToEncoding opts2ElemArray
76gNullaryParseJSON2ElemArray :: Value -> Parser Nullary
77gNullaryParseJSON2ElemArray = genericParseJSON opts2ElemArray
80gNullaryToJSONTaggedObject :: Nullary -> Value
81gNullaryToJSONTaggedObject = genericToJSON optsTaggedObject
83gNullaryToEncodingTaggedObject :: Nullary -> Encoding
84gNullaryToEncodingTaggedObject = genericToEncoding optsTaggedObject
86gNullaryParseJSONTaggedObject :: Value -> Parser Nullary
87gNullaryParseJSONTaggedObject = genericParseJSON optsTaggedObject
90gNullaryToJSONObjectWithSingleField :: Nullary -> Value
91gNullaryToJSONObjectWithSingleField = genericToJSON optsObjectWithSingleField
93gNullaryToEncodingObjectWithSingleField :: Nullary -> Encoding
94gNullaryToEncodingObjectWithSingleField = genericToEncoding optsObjectWithSingleField
96gNullaryParseJSONObjectWithSingleField :: Value -> Parser Nullary
97gNullaryParseJSONObjectWithSingleField = genericParseJSON optsObjectWithSingleField
100--------------------------------------------------------------------------------
101-- SomeType encoders/decoders
102--------------------------------------------------------------------------------
104-- Unary types
105type LiftToJSON f a =
106 (a -> Value) -> ([a] -> Value) -> f a -> Value
107type LiftToEncoding f a =
108 (a -> Encoding) -> ([a] -> Encoding) -> f a -> Encoding
109type LiftParseJSON f a =
110 (Value -> Parser a) -> (Value -> Parser [a]) -> Value -> Parser (f a)
112thSomeTypeToJSON2ElemArray :: SomeType Int -> Value
113thSomeTypeToJSON2ElemArray = $(mkToJSON opts2ElemArray ''SomeType)
115thSomeTypeToEncoding2ElemArray :: SomeType Int -> Encoding
116thSomeTypeToEncoding2ElemArray = $(mkToEncoding opts2ElemArray ''SomeType)
118thSomeTypeLiftToJSON2ElemArray :: LiftToJSON SomeType a
119thSomeTypeLiftToJSON2ElemArray = $(mkLiftToJSON opts2ElemArray ''SomeType)
121thSomeTypeLiftToEncoding2ElemArray :: LiftToEncoding SomeType a
122thSomeTypeLiftToEncoding2ElemArray = $(mkLiftToEncoding opts2ElemArray ''SomeType)
124thSomeTypeParseJSON2ElemArray :: Value -> Parser (SomeType Int)
125thSomeTypeParseJSON2ElemArray = $(mkParseJSON opts2ElemArray ''SomeType)
127thSomeTypeLiftParseJSON2ElemArray :: LiftParseJSON SomeType a
128thSomeTypeLiftParseJSON2ElemArray = $(mkLiftParseJSON opts2ElemArray ''SomeType)
131thSomeTypeToJSONTaggedObject :: SomeType Int -> Value
132thSomeTypeToJSONTaggedObject = $(mkToJSON optsTaggedObject ''SomeType)
134thSomeTypeToEncodingTaggedObject :: SomeType Int -> Encoding
135thSomeTypeToEncodingTaggedObject = $(mkToEncoding optsTaggedObject ''SomeType)
137thSomeTypeLiftToJSONTaggedObject :: LiftToJSON SomeType a
138thSomeTypeLiftToJSONTaggedObject = $(mkLiftToJSON optsTaggedObject ''SomeType)
140thSomeTypeLiftToEncodingTaggedObject :: LiftToEncoding SomeType a
141thSomeTypeLiftToEncodingTaggedObject = $(mkLiftToEncoding optsTaggedObject ''SomeType)
143thSomeTypeParseJSONTaggedObject :: Value -> Parser (SomeType Int)
144thSomeTypeParseJSONTaggedObject = $(mkParseJSON optsTaggedObject ''SomeType)
146thSomeTypeLiftParseJSONTaggedObject :: LiftParseJSON SomeType a
147thSomeTypeLiftParseJSONTaggedObject = $(mkLiftParseJSON optsTaggedObject ''SomeType)
150thSomeTypeToJSONObjectWithSingleField :: SomeType Int -> Value
151thSomeTypeToJSONObjectWithSingleField = $(mkToJSON optsObjectWithSingleField ''SomeType)
153thSomeTypeToEncodingObjectWithSingleField :: SomeType Int -> Encoding
154thSomeTypeToEncodingObjectWithSingleField = $(mkToEncoding optsObjectWithSingleField ''SomeType)
156thSomeTypeLiftToJSONObjectWithSingleField :: LiftToJSON SomeType a
157thSomeTypeLiftToJSONObjectWithSingleField = $(mkLiftToJSON optsObjectWithSingleField ''SomeType)
159thSomeTypeLiftToEncodingObjectWithSingleField :: LiftToEncoding SomeType a
160thSomeTypeLiftToEncodingObjectWithSingleField = $(mkLiftToEncoding optsObjectWithSingleField ''SomeType)
162thSomeTypeParseJSONObjectWithSingleField :: Value -> Parser (SomeType Int)
163thSomeTypeParseJSONObjectWithSingleField = $(mkParseJSON optsObjectWithSingleField ''SomeType)
165thSomeTypeLiftParseJSONObjectWithSingleField :: LiftParseJSON SomeType a
166thSomeTypeLiftParseJSONObjectWithSingleField = $(mkLiftParseJSON optsObjectWithSingleField ''SomeType)
169gSomeTypeToJSON2ElemArray :: SomeType Int -> Value
170gSomeTypeToJSON2ElemArray = genericToJSON opts2ElemArray
172gSomeTypeToEncoding2ElemArray :: SomeType Int -> Encoding
173gSomeTypeToEncoding2ElemArray = genericToEncoding opts2ElemArray
175gSomeTypeParseJSON2ElemArray :: Value -> Parser (SomeType Int)
176gSomeTypeParseJSON2ElemArray = genericParseJSON opts2ElemArray
178#if __GLASGOW_HASKELL__ >= 706
179gSomeTypeLiftToEncoding2ElemArray :: LiftToEncoding SomeType a
180gSomeTypeLiftToEncoding2ElemArray = genericLiftToEncoding opts2ElemArray
182gSomeTypeLiftToJSON2ElemArray :: LiftToJSON SomeType a
183gSomeTypeLiftToJSON2ElemArray = genericLiftToJSON opts2ElemArray
185gSomeTypeLiftParseJSON2ElemArray :: LiftParseJSON SomeType a
186gSomeTypeLiftParseJSON2ElemArray = genericLiftParseJSON opts2ElemArray
187#endif
190gSomeTypeToJSONTaggedObject :: SomeType Int -> Value
191gSomeTypeToJSONTaggedObject = genericToJSON optsTaggedObject
193gSomeTypeToEncodingTaggedObject :: SomeType Int -> Encoding
194gSomeTypeToEncodingTaggedObject = genericToEncoding optsTaggedObject
196gSomeTypeParseJSONTaggedObject :: Value -> Parser (SomeType Int)
197gSomeTypeParseJSONTaggedObject = genericParseJSON optsTaggedObject
199#if __GLASGOW_HASKELL__ >= 706
200gSomeTypeLiftToEncodingTaggedObject :: LiftToEncoding SomeType a
201gSomeTypeLiftToEncodingTaggedObject = genericLiftToEncoding optsTaggedObject
203gSomeTypeLiftToJSONTaggedObject :: LiftToJSON SomeType a
204gSomeTypeLiftToJSONTaggedObject = genericLiftToJSON optsTaggedObject
206gSomeTypeLiftParseJSONTaggedObject :: LiftParseJSON SomeType a
207gSomeTypeLiftParseJSONTaggedObject = genericLiftParseJSON optsTaggedObject
208#endif
211gSomeTypeToJSONObjectWithSingleField :: SomeType Int -> Value
212gSomeTypeToJSONObjectWithSingleField = genericToJSON optsObjectWithSingleField
214gSomeTypeToEncodingObjectWithSingleField :: SomeType Int -> Encoding
215gSomeTypeToEncodingObjectWithSingleField = genericToEncoding optsObjectWithSingleField
217gSomeTypeParseJSONObjectWithSingleField :: Value -> Parser (SomeType Int)
218gSomeTypeParseJSONObjectWithSingleField = genericParseJSON optsObjectWithSingleField
220#if __GLASGOW_HASKELL__ >= 706
221gSomeTypeLiftToEncodingObjectWithSingleField :: LiftToEncoding SomeType a
222gSomeTypeLiftToEncodingObjectWithSingleField = genericLiftToEncoding optsObjectWithSingleField
224gSomeTypeLiftToJSONObjectWithSingleField :: LiftToJSON SomeType a
225gSomeTypeLiftToJSONObjectWithSingleField = genericLiftToJSON optsObjectWithSingleField
227gSomeTypeLiftParseJSONObjectWithSingleField :: LiftParseJSON SomeType a
228gSomeTypeLiftParseJSONObjectWithSingleField = genericLiftParseJSON optsObjectWithSingleField
229#endif
232gSomeTypeToJSONOmitNothingFields :: SomeType Int -> Value
233gSomeTypeToJSONOmitNothingFields = genericToJSON optsOmitNothingFields
235gSomeTypeToEncodingOmitNothingFields :: SomeType Int -> Encoding
236gSomeTypeToEncodingOmitNothingFields = genericToEncoding optsOmitNothingFields
238-------------------------------------------------------------------------------
239-- EitherTextInt encoders/decodes
240-------------------------------------------------------------------------------
242thEitherTextIntToJSONUntaggedValue :: EitherTextInt -> Value
243thEitherTextIntToJSONUntaggedValue = $(mkToJSON optsUntaggedValue ''EitherTextInt)
245thEitherTextIntToEncodingUntaggedValue :: EitherTextInt -> Encoding
246thEitherTextIntToEncodingUntaggedValue = $(mkToEncoding optsUntaggedValue ''EitherTextInt)
248thEitherTextIntParseJSONUntaggedValue :: Value -> Parser EitherTextInt
249thEitherTextIntParseJSONUntaggedValue = $(mkParseJSON optsUntaggedValue ''EitherTextInt)
252gEitherTextIntToJSONUntaggedValue :: EitherTextInt -> Value
253gEitherTextIntToJSONUntaggedValue = genericToJSON optsUntaggedValue
255gEitherTextIntToEncodingUntaggedValue :: EitherTextInt -> Encoding
256gEitherTextIntToEncodingUntaggedValue = genericToEncoding optsUntaggedValue
258gEitherTextIntParseJSONUntaggedValue :: Value -> Parser EitherTextInt
259gEitherTextIntParseJSONUntaggedValue = genericParseJSON optsUntaggedValue
261--------------------------------------------------------------------------------
262-- Approx encoders/decoders
263--------------------------------------------------------------------------------
265thApproxToJSONUnwrap :: Approx String -> Value
266thApproxToJSONUnwrap = $(mkToJSON optsUnwrapUnaryRecords ''Approx)
268thApproxToEncodingUnwrap :: Approx String -> Encoding
269thApproxToEncodingUnwrap = $(mkToEncoding optsUnwrapUnaryRecords ''Approx)
271thApproxParseJSONUnwrap :: Value -> Parser (Approx String)
272thApproxParseJSONUnwrap = $(mkParseJSON optsUnwrapUnaryRecords ''Approx)
275thApproxToJSONDefault :: Approx String -> Value
276thApproxToJSONDefault = $(mkToJSON defaultOptions ''Approx)
278thApproxToEncodingDefault :: Approx String -> Encoding
279thApproxToEncodingDefault = $(mkToEncoding defaultOptions ''Approx)
281thApproxParseJSONDefault :: Value -> Parser (Approx String)
282thApproxParseJSONDefault = $(mkParseJSON defaultOptions ''Approx)
284gApproxToJSONUnwrap :: Approx String -> Value
285gApproxToJSONUnwrap = genericToJSON optsUnwrapUnaryRecords
287gApproxToEncodingUnwrap :: Approx String -> Encoding
288gApproxToEncodingUnwrap = genericToEncoding optsUnwrapUnaryRecords
290gApproxParseJSONUnwrap :: Value -> Parser (Approx String)
291gApproxParseJSONUnwrap = genericParseJSON optsUnwrapUnaryRecords
294gApproxToJSONDefault :: Approx String -> Value
295gApproxToJSONDefault = genericToJSON defaultOptions
297gApproxToEncodingDefault :: Approx String -> Encoding
298gApproxToEncodingDefault = genericToEncoding defaultOptions
300gApproxParseJSONDefault :: Value -> Parser (Approx String)
301gApproxParseJSONDefault = genericParseJSON defaultOptions
303--------------------------------------------------------------------------------
304-- GADT encoders/decoders
305--------------------------------------------------------------------------------
307thGADTToJSONUnwrap :: GADT String -> Value
308thGADTToJSONUnwrap = $(mkToJSON optsUnwrapUnaryRecords ''GADT)
310thGADTToEncodingUnwrap :: GADT String -> Encoding
311thGADTToEncodingUnwrap = $(mkToEncoding optsUnwrapUnaryRecords ''GADT)
313thGADTParseJSONUnwrap :: Value -> Parser (GADT String)
314thGADTParseJSONUnwrap = $(mkParseJSON optsUnwrapUnaryRecords ''GADT)
317thGADTToJSONDefault :: GADT String -> Value
318thGADTToJSONDefault = $(mkToJSON defaultOptions ''GADT)
320thGADTToEncodingDefault :: GADT String -> Encoding
321thGADTToEncodingDefault = $(mkToEncoding defaultOptions ''GADT)
323thGADTParseJSONDefault :: Value -> Parser (GADT String)
324thGADTParseJSONDefault = $(mkParseJSON defaultOptions ''GADT)