VinylRecords / Vinyl

Extensible Records for Haskell. Pull requests welcome! Come visit us on #vinyl on freenode.
http://hackage.haskell.org/package/vinyl
MIT License
260 stars 49 forks source link

implement FromJSON typeclass #129

Open clojurians-org opened 4 years ago

clojurians-org commented 4 years ago

i read the ToJSON example: https://github.com/VinylRecords/Vinyl/blob/master/tests/Aeson.hs

and i want to implement FromJSON, i have no idea how to offer the label value. for pure $ (undefined =: f) :& rest

import Data.String.Conversions (cs)

instance J.FromJSON (V.FieldRec '[]) where
  parseJSON _ = return V.RNil

instance (KnownSymbol s, J.FromJSON a, J.FromJSON (V.FieldRec rs))
  => J.FromJSON (V.FieldRec (s ::: a ': rs)) where
  parseJSON o@(J.Object v) = do
    f :: a <- v .: cs (symbolVal (Proxy :: Proxy s))
    rest <- J.parseJSON o
    pure $ (undefined =: f) :& rest
clojurians-org commented 4 years ago

i use Label to resolve it. i want to know whether exist a general FromJSON version

instance (KnownSymbol s, J.FromJSON a, J.FromJSON (V.FieldRec rs))
  => J.FromJSON (V.FieldRec (s ::: a ': rs)) where
  parseJSON o@(J.Object v) = do
    f :: a <- v .: cs (symbolVal (Proxy :: Proxy s))
    rest <- J.parseJSON o
    pure $ ((V.Label:: V.Label s)=: f) :& rest