[Haskell-beginners] How to "cast" FromJSON instance value

Baa aquagnu at gmail.com
Tue Aug 1 11:47:00 UTC 2017


Ohh, pardon! Yes, signature was

  class GetValue a where
    getValue :: Text -> Map Text Value -> Maybe a

with `Maybe` sure. But I didn't understand you: what do you mean "to
use aeson directly"? Problem is to save `FromJSON a` instances in map
but get from the map `Text`, `Bool` and etc values. Sure, it possible
to parameterize all related functions/types with `FromJSON a => .. a`
but in this case I hit 2 problems:

1. rigid type error when I have `FromJSON a =>` in some signature and
   try to use this `a` as `Text` value, for example
2. big refactoring (many signatures, types)

So, I decided to save in the map `Value`s instead of `FromJSON a`s.
De-facto, this map is heterogenous and keeps values of different types.
And question for me was: to hide this fact under `FromJSON` or under
`Value`. I'm not sure what is the best solution and I don't know how to
solve 1.


> You'd still have to handle the case that the Value is not what you
> expected. Otherwise you'll have a partial function (i.e. error in
> case of certain inputs).
> 
> So maybe you'd best use aeson directly (which spares you the GetValue
> instances as well).
> 
> getValue :: (FromJSON a) => Text -> Map Text a -> Maybe a
> 
> Best
> Jan
> P.S. There is no such thing as a stupid question.
> 
> Baa <aquagnu at gmail.com> schrieb am Di., 1. Aug. 2017, 13:02:
> 
> > Hello, Jan! I "remove" the question :)
> > Problem was: reading of HTML form values, which can be any type and
> > saving them in a map. I done it w/ `Map Text Value` (instead of
> > previous `Map Text Text`). To get values from the map, I
> > implemented class and several instances which returns values of
> > different types, something like:
> >
> >   class GetValue a where
> >     getValue :: Text -> Map Text Value -> a
> >
> > (promoting of `a` anywhere in the existing code needs big
> > refactoring, so map's value is `::Value` but not `::a`). Cons is a
> > need to implement N instances of `GetValue`: one per getting type.
> >
> > Anywhere, Jan, thanks for quick answer, I'm sorry for this stupid
> > question.
> >
> >  
> > > Hi,
> > >
> > > You don't know anything about your a's but that they have FromJSON
> > > instances. That in turn means you can get values from aeson
> > > parsing functions. Think parseJSON :: Value -> Parser a
> > > Could you be more specific about the problem you try to solve?
> > >
> > > Best
> > > Jan
> > >
> > > Baa <aquagnu at gmail.com> schrieb am Di., 1. Aug. 2017, 10:32:
> > >  
> > > > Hello, List!
> > > >
> > > > I have, for example, function with signature like
> > > >
> > > >   fn :: FromJSON a => Map Text a -> BlahBlah
> > > >
> > > > and I want to treat this `a` as String (or `Text`), to read it
> > > > with `readMaybe`. Am I right that it's impossible? I get error
> > > > about "...a is rigid type and could not be matched with
> > > > Text...". But `Text` is `FromJSON` instance. Is it possible to
> > > > do it (cast, convert, unwrap, etc)?
> > > >
> > > > The problem's source is that early I worked with `Text` values
> > > > only (getting from HTML form) but now they become any type:
> > > > int's, bool's, etc).
> > > >
> > > > ===
> > > > Best regards,
> > > >   Paul
> > > > _______________________________________________
> > > > Beginners mailing list
> > > > Beginners at haskell.org
> > > > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> > > >  
> >
> > _______________________________________________
> > Beginners mailing list
> > Beginners at haskell.org
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> >  



More information about the Beginners mailing list