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.
Don't you want to keep the option around for Safari?
There was a problem hiding this comment.
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.
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.
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.