aeson icon indicating copy to clipboard operation
aeson copied to clipboard

Add Data.Complex instances

Open TravisWhitaker opened this issue 1 year ago • 4 comments

There are loads of utility functions for defining the included instances. I guessed at which ones should be used for Data.Complex, but if I guessed incorrectly please say so.

I used a two-element array as the representation because that's how I've seen APIs in the wild do it, but perhaps {i : ..., q : ...} or {real : ..., imag : ...} would be preferred?

TravisWhitaker avatar Apr 06 '24 04:04 TravisWhitaker

@phadej any thoughts on this?

TravisWhitaker avatar Nov 11 '24 03:11 TravisWhitaker

I'd pick {real : ..., imag : ...}, it will be analogous to Ratio. Also add FromJSON1 and ToJSON1 instance. Ratio doesn't have those, as it has an invariant, but Complex is really just a homogeneous pair (yet the common use case as number could be confusing when encoding as just a pair).

EDIT: I made a quick google, and it doesn't seem that there is an agreement to how encode complex numbers in JSON, so having more self-explanatory encoding is better.

phadej avatar Nov 11 '24 12:11 phadej

Ah, sorry for missing *JSON1.

I'm curious what you'd think of having {real : ..., imag : ...} as the default instance, and then providing instances for the other two common ways via newtype instances (similar to the existing DotNetTime newtype).

TravisWhitaker avatar Nov 19 '24 05:11 TravisWhitaker

(similar to the existing DotNetTime newtype).

Not worth the trouble.

phadej avatar Nov 19 '24 12:11 phadej