Skip to content
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

library on top of this which provides an aeson-like experience? #1

Open
chessai opened this issue Nov 6, 2019 · 4 comments
Open

library on top of this which provides an aeson-like experience? #1

chessai opened this issue Nov 6, 2019 · 4 comments

Comments

@chessai
Copy link
Member

chessai commented Nov 6, 2019

aeson has an extremely nice api. it's very easy to write ToJSON/FromJSON instances manually, and its generics machinery is great. i think there should be a library written on top of this which offers a similar experience. then i wouldn't have a reason to use aeson most of the time.

@andrewthad
Copy link
Member

I’m in favor of this as well. I even put a mild CTA in the readme about this. The easy, one-option-in-the-design-space parts are the encoding typeclass and codecs for integral types, bool, text, list. The thornier questions are:

  • String: special support for the typeclass instances for a list of Char is a pain, and no one should be using them anyway.
  • Keeping a finger to the current position for good decoding exceptions. Aeson does this, but it’s somewhat manual and easy to screw up. I don’t know if there is a better way to do this than how aeson does it.
  • How to decode objects to records. Since this library doesn’t use hash maps, there is more freedom here. You could just build a hash map and proceed as aeson does, but there are some other cool options available. The main one I’ve played around before with in this context involves setting up a mutable variable for each field and then folding over the key-value pairs, writing each value into the appropriate slot when the corresponding key matches the slot key. Then, when you’re done, you make sure that something was written to all the required slots and then read everything. Definitely doable with TH. Not sure if it could be done with generics. Anyway, I digress.

@andrewthad
Copy link
Member

Oh, and I forgot, the thorniest question of all: What to do about Maybe? If you use JSON’s null, then nested Maybes are busted. (Also, any data type that could encode itself as null would be busted when used as the type argument to Maybe). But nesting Maybes is really uncommon, and people usually want the “null is Maybe” behavior. Aeson never figured out a good answer to this problem. Maybe there’s one out there.

@goolord
Copy link
Member

goolord commented Nov 7, 2019

rust's most popular json serializing/deserializing library does the 'null is Nothing' behavior https://docs.rs/serde_json/0.7.4/src/serde_json/.cargo/registry/src/github.com-1ecc6299db9ec823/serde_json-0.7.4/src/de.rs.html#535

nested Maybe where Just Nothing is semantically different from Nothing is totally an anti-pattern

@chessai
Copy link
Member Author

chessai commented Nov 7, 2019

I started working on this but haven't pushed yet

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

No branches or pull requests

3 participants