purescript-contrib/purescript-argonaut-codecs

Add `fromJson` and `toJson`

sigma-andex opened this issue · 3 comments

The first thing I always need to do when using argonaut in a project is to write functions fromJson function which composes parseJson and decodeJson and toJson composing encodeJson and stringify:

fromJson :: forall t. DecodeJson t => String -> Either JsonDecodeError t
fromJson = parseJson >=> decodeJson

toJson :: forall t. EncodeJson t => t -> String
toJson = encodeJson >>> stringify

So why not add them to argonaut? They could also be named parseAndDecode/ encodeAndStringify or sth else, I don't really mind. Though I find fromJson and toJson just nicely minimalistic.

I have created a pr for it in #109 .

Update:
changed type variable from json to t in the first case to avoid confusion.

wclr commented

fromJson that gets String and toJson that outputs String, this seems to be a bit misleading naming.

I think it is only misleading if you think in terms of Argonaut where you have this intermediate Json type.
If you are coming from JS it makes perfect sense: fromJson converts from Json (which is a string) to an object and toJson converts an object to a Json (string). The JS equivalents would be JSON.parse and JSON.stringify()but unfortunately these are already taken.

wclr commented

I also think that such helpers are not needed to be supplied with the library just to shortcut parseJson >=> decodeJson, one can do it in its code if one wants the shortcut.