4

I am attempting to use Data.Aeson.TH deriveJSON to generate the ToJSON and FromJSON instances for MongoDB Data.Bson.

At the moment I am using:

$(deriveJSON id ''Data.Bson.Field)
$(deriveJSON id ''Data.Bson.Value)
$(deriveJSON id ''Data.Bson.Binary)
$(deriveJSON id ''Data.Bson.UUID)
$(deriveJSON id ''Data.Bson.UserDefined)
$(deriveJSON id ''Data.Bson.Regex)
$(deriveJSON id ''Data.Bson.Symbol)
$(deriveJSON id ''Data.Bson.MinMaxKey)
$(deriveJSON id ''Data.Bson.MongoStamp)
$(deriveJSON id ''Data.Bson.Javascript)
$(deriveJSON id ''Data.Bson.ObjectId)
$(deriveJSON id ''Data.Bson.MD5)
$(deriveJSON id ''Data.Bson.Function)
$(deriveJSON id ''Data.Bson.UString)

Which generates the following error at compile time:

Exception when trying to run compile-time code:
Data.Aeson.TH.withType: Unsupported type: TySynD Data.UString.UString [] (ConT Data.CompactString.UTF8.CompactString)
Code: deriveJSON (id) 'UString

I think the issue here is that the string inside the BSON document is a Ustring. I need to convert or otherwise map the the UString expected within the BSON data to another String type ... but stumped as to how.

Toby Hede
  • 36,755
  • 28
  • 133
  • 162
  • It seems that Aeson does not support type synonyms ([aeson src](http://hackage.haskell.org/packages/archive/aeson/0.4.0.0/doc/html/src/Data-Aeson-TH.html#withType)). But `UString` is a type synonym ([bson doc](http://hackage.haskell.org/packages/archive/bson/0.1.6/doc/html/Data-UString.html)). Adding `TySynD` case to `withType` may help, but I'm not sure. – max taldykin Dec 21 '11 at 13:25
  • You might also be interested in [aesonBson](http://hackage.haskell.org/package/AesonBson). – nh2 May 14 '13 at 08:17

1 Answers1

2

As I mentioned, Aeson does not support type synonyms, but nothing prevents us from unfolding UString.

type UString = Data.CompactString.CompactString
type CompactString = Data.CompactString.Internal.CompactString UTF8

So, this one (instead of derive for UString) will work:

$(deriveJSON id ''Data.CompactString.Internal.CompactString)
$(deriveJSON id ''Data.CompactString.Encodings.UTF8)
max taldykin
  • 12,459
  • 5
  • 45
  • 64