-
Notifications
You must be signed in to change notification settings - Fork 121
Add aggressiveFloatParsing #1100
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
Conversation
|
JSON.parse('[3.0]', (key, value, context) => {
// key is 0, value is 3, context is { source: "3.0" }
return value;
})With that, we could convert 3.0 to "3.0f" without being aggressive. Something like: JSON.parse('{"key":"value", "array": [1, 2, null, 3.0, false]}', (key, value, context) => {
if (Number.isInteger(value) && context.source.endsWith(".0")) {
return context.source + "f";
}
return value;
})And it's not hard to detect support for context: function testHasReviverContext() {
return JSON.parse("0", (key, value, context) => context != null)
} |
|
That is very interesting ! |
|
But safari (osx and ios) doesn't support it, that's unfortunate (https://caniuse.com/mdn-javascript_builtins_json_parse_reviver_parameter_context_argument) |
|
Yes, that's why I mentioned testing for support. |
src/engine/SimpleJson.ts
Outdated
| export class SimpleJson { | ||
| public static TextToDictionary( | ||
| text: string, | ||
| aggressiveFloatParsing: boolean = false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't you want to keep the option around for Safari?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I chose the lesser of two evils :
- either the math is totally wrong on safari unless aggressiveFloatParsing is explicit
- or there's a very minuscule chance of ", 5.0" being displayed ", 5.0f" in a text somewhere
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By essence, you never know where your code will be executed (Safari or elsewhere) so you basically never have the choice to turn the aggressiveFloatParsing. It is now on by default for Safari. And for everyone else, I use your suggestion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I read too fast and missed the fact that you only removed the option but kept the aggressive code. 👍
…s into feat/aggressiveFloatParsing
|
I think this part of the documentation can be removed or edited: |
Checklist
npm test).npm run-script lint).Description
Add
aggressive float parsingoptionso that when loading a json file that contain explicit floating point number ending in.0, they are understood asFloatValueand notIntValue, leading to errors in games loaded with previous version of inkjs.