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

Serialize unions without fields as string instead of object with empty array? #27

Closed
drhumlen opened this issue Jun 7, 2019 · 9 comments

Comments

@drhumlen
Copy link

drhumlen commented Jun 7, 2019

type LogLevel = Info | Warn | Error
type LogEntry = {Level: LogLevel; Message: string}

shouldn't this be serialized as

{message: "My Message", level: "info"}

instead of

{message: "My Message", level: {"Info": []}} ?

SharpLu.Json does this, but you've went the other way? Is that a conscious decision, or should I make a PR to change it?

Other than that, this library is very promising :)

SharplLu.Json is pretty good too, but the lack of a single-case union serialization support was a dealbreak for me on that one. This library supports that, but lacks the above 🤔

Also, is there a way to use camelCasing instead of PascalCasing in the serialized json? PascalCasing is very unusual in json.

@vsapronov
Copy link
Collaborator

Hmmm, I think it's a mistake. I will check more and come back with some solution.

@drhumlen
Copy link
Author

drhumlen commented Jul 8, 2019

It depends on who your consumer is. If it's only for serializing F# to F#, then it's fine as it is. But I would be very surprised to see this type definition real typescript code:

type LogEntry = {
  message: string;
  level: {Info: []} | {Warn: []} | {Error: []};
}

and then

if ('Info' in logEntry.level) {
   return <InfoIcon />
} else if (....

you get the picture. It's very unnatural for an API-consumer.

I realize there's probably some technical edge-cases though.

@vsapronov
Copy link
Collaborator

I meant more my mistake that I never considered "enum" style use case for DU.
I will fix this but probably in the next major release as it breaks backward compatibility.

@dpraimeyuu
Copy link

hi @vsapronov ! Have you had chance to work on that issue? Is there anything I could help with? :-) I also ran into similar issue mentioned in the first comment.

@pkese
Copy link

pkese commented Oct 7, 2019

I'm having a similar issue with a list of Enums

type MailFlag = | AirMail | Priority | Confidential | InformDelivery
type Mail = {
   address: string
   flags: MailFlag list
}

and data

{
  "address": "somewhere",
  "flags": ["Priority", "InformDelivery"]
}

@vsapronov
Copy link
Collaborator

Sorry, I was distracted by something. Will do my best to make a release with the fix this week.

@dpraimeyuu
Copy link

Hi @vsapronov ! Have you had chance to find a solution? 😄

@vsapronov
Copy link
Collaborator

Please try version 0.4.0 and let me know if it works for you.

@vsapronov
Copy link
Collaborator

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

4 participants