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

to_json() on a nested data structure containing undefined should not fail #3842

Open
gschier opened this issue May 8, 2024 · 3 comments
Open
Labels
bug Something isn't working E-Easy Easy good first issue Good for newcomers

Comments

@gschier
Copy link

gschier commented May 8, 2024

Describe the bug

When calling to_json on an object like {foo: undefined}, it should result in {} and not panic. Here are some various behaviors (demo's using Node) which it would be good to follow:

> JSON.stringify({foo: undefined, bar: 1})
'{"bar":1}'
> JSON.stringify(undefined)
undefined
> JSON.stringify([1,2,undefined,4])
'[1,2,null,4]'

For context, I'm building a 3rd-party plugin system for Yaak using Boa and users may return a nested data structure that contain undefined values.

To Reproduce

Return an object from the JS context and call to_json on it.

export function callMe() {
  return { foo: undefined };
}

Expected behavior

The value should serialize to {} and not throw not yet implemented: undefined to JSON.

Build environment (please complete the following information):

  • OS: macOS
  • Version: 14
  • Target triple: aarch64-apple-darwin
  • Rustc version: rustc 1.77.2 (25ef9e3d8 2024-04-09)

Additional context

The current workaround is to return JSON.parse(JSON.stringify(objectContainingUndefineds)). However, as I'm using Boa to build a plugin system, I wouldn't expect users to have to do this themselves.

Another potential workaround is to recursively crawl the JSValue and remove undefined values from all the objects and arrays.

Thanks for taking the time to look into this!

@gschier gschier added the bug Something isn't working label May 8, 2024
@jedel1043
Copy link
Member

jedel1043 commented Jun 1, 2024

I think the confusion here is that the to_json and from_json methods are misnomers; those functions just try to convert from serde_json's Value to Boa's JsValue, and since serde_json does not support "undefined", we cannot convert to it.

The solution here would be to expose JSON::stringify and JSON::parse as Rust APIs, and rename the serde_json APIs to to_serde_json_value and from_serde_json_value.

@jedel1043 jedel1043 added good first issue Good for newcomers E-Easy Easy labels Jun 1, 2024
@Fnll
Copy link

Fnll commented Jun 18, 2024

@gschier I'm not sure if I get it right, are you tying to call to_json in rust side with a JSValue::Object contains undefined and result in an error shows not yet implemented: undefined to JSON? As @jedel1043 explained, serde_json dose not allow convert value to undefined.

So what behavior is it expected to be?

@Taym95
Copy link

Taym95 commented Aug 29, 2024

I think what serde_json is doing is a Serde-specific implementation. boa should follow the spec and return {}.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working E-Easy Easy good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants