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

Omit undefineds in serialized objects #92

Open
cknitt opened this issue Sep 26, 2024 · 0 comments
Open

Omit undefineds in serialized objects #92

cknitt opened this issue Sep 26, 2024 · 0 comments
Labels
enhancement New feature or request v10 Features planned for the v10 version

Comments

@cknitt
Copy link
Contributor

cknitt commented Sep 26, 2024

Already discussed this one with @DZakh some time ago, but we did not really come to a conclusion and lost track of it.

When I have an object with a field of type option<'a>, e.g.

@schema
type t = {
  a: string,
  b: option<int>,
}

let obj = {a: "test", b: None}

and I serialize it using S.serializeOrRaiseWith(obj, schema), I get the following JS object:

{a: 'test', b: undefined}

I would prefer to get

{a: 'test'}

i.e., having the field b with value undefined omitted.

I know that b is stripped anyway when serializing the object to JSON using JSON.stringify, but:

  1. In other use cases, such as writing the object to IndexedDB, the undefined values are not stripped.

  2. The return type of S.serializeOrRaiseWith is JSON.t which is defined as

type rec t =
  | Boolean(bool)
  | @as(null) Null
  | String(string)
  | Number(float)
  | Object(Core__Dict.t<t>)
  | Array(array<t>)

and therefore actually cannot have undefined values.

@DZakh DZakh added the v10 Features planned for the v10 version label Sep 26, 2024
@DZakh DZakh added the v9 Features planned for the v9 version label Oct 11, 2024
@DZakh DZakh added the enhancement New feature or request label Oct 11, 2024
@DZakh DZakh removed the v9 Features planned for the v9 version label Nov 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request v10 Features planned for the v10 version
Projects
None yet
Development

No branches or pull requests

2 participants