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

Add a strict option to Generator #519

Merged
merged 1 commit into from
Dec 1, 2023
Merged

Add a strict option to Generator #519

merged 1 commit into from
Dec 1, 2023

Conversation

casperisfine
Copy link

The default behavior of calling to_json or even to_s on unsupported object is problematic in many cases.

>> JSON.parse(JSON.dump(Object.new))
=> "#<Object:0x00000001034d8420>"

In some cases like blind serailization into some kind of store, you may want to assert that JSON.parse(JSON.dump(obj)) == obj.

The strict option is a way to ensure this by rejecting any type that doesn't map cleanly to a JSON native type.

The default behavior of calling `to_json` or even `to_s` on unsupported
object is problematic in many cases.

```
>> JSON.parse(JSON.dump(Object.new))
=> "#<Object:0x00000001034d8420>"
```

In some cases like blind serailization into some kind of store,
you may want to assert that `JSON.parse(JSON.dump(obj)) == obj`.

The `strict` option is a way to ensure this by rejecting any type
that doesn't map cleanly to a JSON native type.
@gjtorikian
Copy link

@flori @hsbt Would it be possible to have this reviewed?

@hsbt hsbt merged commit 978ee15 into flori:master Dec 1, 2023
@hsbt
Copy link
Collaborator

hsbt commented Dec 1, 2023

@casperisfine Thank you. I picked your commit and fixed conflict with #525

Comment on lines +344 to +346
if state.strict?
raise GeneratorError, "#{value.class} not allowed in JSON"
elsif value.respond_to?(:to_json)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bug, see #584

casperisfine pushed a commit to casperisfine/json that referenced this pull request May 9, 2024
casperisfine pushed a commit to casperisfine/json that referenced this pull request May 9, 2024
casperisfine pushed a commit to casperisfine/json that referenced this pull request May 9, 2024
casperisfine pushed a commit to casperisfine/json that referenced this pull request May 9, 2024
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

Successfully merging this pull request may close these issues.

None yet

5 participants