You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've got some code using a variant of the code from dependent-sum-aeson-orphans, but with ToJSON1 and FromJSON1 in the mix:
instance (ToJSON1f, ForallFToJSONk, HasToJSONk) =>ToJSON (DSumkf) where
toJSON ((k ::ka) :=> (f ::fa))
= whichever @ToJSON@k@a$ has @ToJSON k $let x = toJSON k
y = toJSON1 f
in toJSON (x, y)
instance (ToJSON1f, ForallFToJSONk, HasToJSONk) =>ToJSON (DMapkf) where
toJSON = toJSON .DMap.toList
instance (FromJSON1f, FromJSON (Somek), HasFromJSONk) =>FromJSON (DSumkf) where
parseJSON x =do
(jk, jf) <- parseJSON x
Some.This (k ::ka) <- parseJSON jk
f <- has @FromJSON k $ parseJSON1 jf
return$ k :=> f
instance (FromJSON1f, FromJSON (Somek), GComparek, HasFromJSONk) =>FromJSON (DMapkf) where
parseJSON =fmapDMap.fromList . parseJSON
which is quite a bit nicer than having to list out (ToJSON (f Int), ToJSON (f Bool), ...) => for all of the types associated with the constructors of FooKey.
I have used this in anger much, so I'm not sure what the various pros and cons are. If there is interest in this I can put it into a PR which adds the above code in a separate module.
The text was updated successfully, but these errors were encountered:
I've got some code using a variant of the code from dependent-sum-aeson-orphans, but with
ToJSON1
andFromJSON1
in the mix:That lets me write things like:
which is quite a bit nicer than having to list out
(ToJSON (f Int), ToJSON (f Bool), ...) =>
for all of the types associated with the constructors ofFooKey
.I have used this in anger much, so I'm not sure what the various pros and cons are. If there is interest in this I can put it into a PR which adds the above code in a separate module.
The text was updated successfully, but these errors were encountered: