on 1.0.2.0 help sign in
typeable/aeson/tests/Encoders.hs
343 lines 14 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-- IncoherentInstancesNeeded
240--------------------------------------------------------------------------------
242-- | This test demonstrates the need for IncoherentInstances. See the definition
243-- of 'IncoherentInstancesNeeded' for a discussion of the issue.
244--
245-- NOTE 1: We only need to compile this test. We do not need to run it.
246--
247-- NOTE 2: We actually only use the INCOHERENT pragma on specific instances
248-- instead of the IncoherentInstances language extension. Therefore, this is
249-- only supported on GHC versions >= 7.10.
250#if __GLASGOW_HASKELL__ >= 710
251incoherentInstancesNeededParseJSONString :: FromJSON a => Value -> Parser (IncoherentInstancesNeeded a)
252incoherentInstancesNeededParseJSONString = case () of
253 _ | True -> $(mkParseJSON defaultOptions ''IncoherentInstancesNeeded)
254 | False -> genericParseJSON defaultOptions
255#endif
257-------------------------------------------------------------------------------
258-- EitherTextInt encoders/decodes
259-------------------------------------------------------------------------------
261thEitherTextIntToJSONUntaggedValue :: EitherTextInt -> Value
262thEitherTextIntToJSONUntaggedValue = $(mkToJSON optsUntaggedValue ''EitherTextInt)
264thEitherTextIntToEncodingUntaggedValue :: EitherTextInt -> Encoding
265thEitherTextIntToEncodingUntaggedValue = $(mkToEncoding optsUntaggedValue ''EitherTextInt)
267thEitherTextIntParseJSONUntaggedValue :: Value -> Parser EitherTextInt
268thEitherTextIntParseJSONUntaggedValue = $(mkParseJSON optsUntaggedValue ''EitherTextInt)
271gEitherTextIntToJSONUntaggedValue :: EitherTextInt -> Value
272gEitherTextIntToJSONUntaggedValue = genericToJSON optsUntaggedValue
274gEitherTextIntToEncodingUntaggedValue :: EitherTextInt -> Encoding
275gEitherTextIntToEncodingUntaggedValue = genericToEncoding optsUntaggedValue
277gEitherTextIntParseJSONUntaggedValue :: Value -> Parser EitherTextInt
278gEitherTextIntParseJSONUntaggedValue = genericParseJSON optsUntaggedValue
280--------------------------------------------------------------------------------
281-- Approx encoders/decoders
282--------------------------------------------------------------------------------
284thApproxToJSONUnwrap :: Approx String -> Value
285thApproxToJSONUnwrap = $(mkToJSON optsUnwrapUnaryRecords ''Approx)
287thApproxToEncodingUnwrap :: Approx String -> Encoding
288thApproxToEncodingUnwrap = $(mkToEncoding optsUnwrapUnaryRecords ''Approx)
290thApproxParseJSONUnwrap :: Value -> Parser (Approx String)
291thApproxParseJSONUnwrap = $(mkParseJSON optsUnwrapUnaryRecords ''Approx)
294thApproxToJSONDefault :: Approx String -> Value
295thApproxToJSONDefault = $(mkToJSON defaultOptions ''Approx)
297thApproxToEncodingDefault :: Approx String -> Encoding
298thApproxToEncodingDefault = $(mkToEncoding defaultOptions ''Approx)
300thApproxParseJSONDefault :: Value -> Parser (Approx String)
301thApproxParseJSONDefault = $(mkParseJSON defaultOptions ''Approx)
303gApproxToJSONUnwrap :: Approx String -> Value
304gApproxToJSONUnwrap = genericToJSON optsUnwrapUnaryRecords
306gApproxToEncodingUnwrap :: Approx String -> Encoding
307gApproxToEncodingUnwrap = genericToEncoding optsUnwrapUnaryRecords
309gApproxParseJSONUnwrap :: Value -> Parser (Approx String)
310gApproxParseJSONUnwrap = genericParseJSON optsUnwrapUnaryRecords
313gApproxToJSONDefault :: Approx String -> Value
314gApproxToJSONDefault = genericToJSON defaultOptions
316gApproxToEncodingDefault :: Approx String -> Encoding
317gApproxToEncodingDefault = genericToEncoding defaultOptions
319gApproxParseJSONDefault :: Value -> Parser (Approx String)
320gApproxParseJSONDefault = genericParseJSON defaultOptions
322--------------------------------------------------------------------------------
323-- GADT encoders/decoders
324--------------------------------------------------------------------------------
326thGADTToJSONUnwrap :: GADT String -> Value
327thGADTToJSONUnwrap = $(mkToJSON optsUnwrapUnaryRecords ''GADT)
329thGADTToEncodingUnwrap :: GADT String -> Encoding
330thGADTToEncodingUnwrap = $(mkToEncoding optsUnwrapUnaryRecords ''GADT)
332thGADTParseJSONUnwrap :: Value -> Parser (GADT String)
333thGADTParseJSONUnwrap = $(mkParseJSON optsUnwrapUnaryRecords ''GADT)
336thGADTToJSONDefault :: GADT String -> Value
337thGADTToJSONDefault = $(mkToJSON defaultOptions ''GADT)
339thGADTToEncodingDefault :: GADT String -> Encoding
340thGADTToEncodingDefault = $(mkToEncoding defaultOptions ''GADT)
342thGADTParseJSONDefault :: Value -> Parser (GADT String)
343thGADTParseJSONDefault = $(mkParseJSON defaultOptions ''GADT)