Skip to content

add FromAeson class to easily bring Aeson.Value across #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

dmvianna
Copy link
Contributor

I spent an inordinate time this afternoon on ToJSON and FromJSON only to realise the internal Parser type in Aeson is from Attoparsec and is really concerned with ByteString.

While what I needed was interoperability between JsonValue and Aeson.Value. Yes, the easiest win would always be to use fromAeson, which I did.

This is all so we don't waste time transforming strings. Maybe we should just expose fromAeson as it was before and be done with it.

@istathar
Copy link
Member

istathar commented Feb 23, 2023

Hey @dmvianna thanks for your contribution.

Hm!

I would like to think a FromJSON and ToJSON instance for our JsonValue wrapper shouldn't be beyond the pale. If you like I can try and figure that out with you.

I'd probably prefer that we figure those instances out rather than adding a new typeclass. If we did, then we'd probably want to follow the intoBlah and fromBlah naming convention used in the other packages in this library. Maybe

class Serialize α where
   parseJson :: JsonValue -> Maybe α
   intoJson :: α -> JsonValue

(following the slightly amended pattern in Externalize; not the worse name, although it's not generalized seralizing. Bloatable? Objectable? gawd Objectify maybe)

...but that really is ripping off the machinery in aeson. Not sure if it's worth adding. Maybe it is.

More to the point, I'm not sure if that's what you're looking for.

Anyway:

Option 1: add FromJSON and ToJSON instances for our JsonValue
Option 2: add Seralize (or whatever) typeclass.

@dmvianna
Copy link
Contributor Author

I think the whole trouble is that I got wrapped up in the idea of using ToJSON and FromJSON because that's what we do with Aeson. But I got into the task without thinking it through.

The ideal outcome would be a function such as (ToJSON a) => a -> JsonValue. Why? Because it is easy to derive ToJSON, many types have that in most codebases, and there aren't many ways to get nice shell output with Doc without implementing it by hand. There is pretty-simple, but it doesn't play nice with unbeliever.

@dmvianna
Copy link
Contributor Author

What I want is deriving (Pretty) or deriving (Doc).

@istathar
Copy link
Member

istathar commented Feb 23, 2023

Sounds like what you want is deriving (Render)! :)

Joking aside, we can almost certainly get

:: (ToJSON a) => a -> JsonValue

happening.

The question is do you need pretty printing (for which we have extensive support via prettyprinter and the Render typeclass) or automated JSON encoding/decoding?

@dmvianna
Copy link
Contributor Author

Pretty printing please. Not only of JSON.

@dmvianna
Copy link
Contributor Author

Pretty printing of Haskell records.

@istathar
Copy link
Member

You could probably come up with a derived Render instance using GHC Generics, though that's "Here be dragons" territory for me. I can think of a few other packages that seem to have done something similar if you want pointers of code to study.

@dmvianna dmvianna closed this Jun 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants