Question

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.

Was it helpful?

Solution

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top