on master help sign in
Fix up a type ambiguity problem with Template Haskell

When I turned on building of the TH example, I got a type error due to use of OverloadedStrings. The splice that generates toEncoding code had insufficiently strongly typed functions for encoding literal strings. I think I've got it sorted now, right down to using unpackCString# to generate the field name Text for fastest encoding.

commit1fa91b750fae6499d4746bfacc97e821ddbaeb4c authorBryan O'Sullivan <bos@serpentine.com> dateMon 6 Jul 2015, 21:32 (11 years ago) parent75b2b13
2 files changed+6−6
Data/Aeson/TH.hs+6−4 examples/aeson-examples.cabal−2
Data/Aeson/TH.hs+6−4
@@ -436,8 +436,8 @@ sumToEncoding opts multiCons conName exp
436436 array (encStr opts conName <%> fexp)
437437 TaggedObject{tagFieldName, contentsFieldName} ->
438438 object $
439− ([|E.builder tagFieldName|] <:> encStr opts conName) <%>
440− ([|E.builder contentsFieldName|] <:> fexp)
439+ ([|E.text (T.pack tagFieldName)|] <:> encStr opts conName) <%>
440+ ([|E.text (T.pack contentsFieldName)|] <:> fexp)
441441 ObjectWithSingleField ->
442442 object (encStr opts conName <:> fexp)
443443
@@ -500,7 +500,8 @@ argsToEncoding opts multiCons (RecC conName ts) = case (unwrapUnaryRecords opts,
500500 toPair (arg, (field, _, _)) =
501501 toFieldName field <:> [|E.builder|] `appE` varE arg
502502
503− toFieldName field = [|E.builder|] `appE` fieldLabelExp opts field
503+ toFieldName field = [|E.text|] `appE`
504+ ([|T.pack|] `appE` fieldLabelExp opts field)
504505
505506 match (conP conName $ map varP args)
506507 ( normalB
@@ -509,7 +510,8 @@ argsToEncoding opts multiCons (RecC conName ts) = case (unwrapUnaryRecords opts,
509510 TwoElemArray -> array $
510511 encStr opts conName <%> [|fromEncoding|] `appE` exp
511512 TaggedObject{tagFieldName} -> object $
512− ([|E.builder tagFieldName|] <:> encStr opts conName) <%>
513+ ([|E.text (T.pack tagFieldName)|] <:>
514+ encStr opts conName) <%>
513515 objBody
514516 ObjectWithSingleField -> object $
515517 encStr opts conName <:> [|fromEncoding|] `appE` exp
examples/aeson-examples.cabal−2
@@ -21,8 +21,6 @@ executable aeson-example-simplest
2121 bytestring
2222
2323executable aeson-example-th
24− -- FIXME Disabled until TH support for toEncoding is implemented
25− buildable: False
2624 main-is: TemplateHaskell.hs
2725 ghc-options: -Wall
2826 build-depends: