Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@smwhr
Copy link
Collaborator

@smwhr smwhr commented Feb 8, 2025

Checklist

  • The new code additions passed the tests (npm test).
  • The linter ran and found no issues (npm run-script lint).

Description

Add aggressive float parsing option so that when loading a json file that contain explicit floating point number ending in .0, they are understood as FloatValue and not IntValue, leading to errors in games loaded with previous version of inkjs.

@floriancargoet
Copy link
Contributor

JSON.parse can take a reviver function as second parameter. On modern browsers, this reviver has access to the source of the value being parsed.

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)
}

@smwhr
Copy link
Collaborator Author

smwhr commented Mar 18, 2025

That is very interesting !

@smwhr
Copy link
Collaborator Author

smwhr commented Mar 18, 2025

But safari (osx and ios) doesn't support it, that's unfortunate (https://caniuse.com/mdn-javascript_builtins_json_parse_reviver_parameter_context_argument)

@floriancargoet
Copy link
Contributor

floriancargoet commented Mar 19, 2025

Yes, that's why I mentioned testing for support.
It could be used by default where it works and there's always the aggressive option where it doesn't.

export class SimpleJson {
public static TextToDictionary(
text: string,
aggressiveFloatParsing: boolean = false
Copy link
Contributor

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?

Copy link
Collaborator Author

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

Copy link
Collaborator Author

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.

Copy link
Contributor

@floriancargoet floriancargoet Apr 4, 2025

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. 👍

@smwhr smwhr merged commit d2cd6ef into master Apr 7, 2025
1 check passed
@smwhr smwhr deleted the feat/aggressiveFloatParsing branch April 7, 2025 09:33
@floriancargoet
Copy link
Contributor

I think this part of the documentation can be removed or edited:
https://github.com/y-lohse/inkjs/blob/master/docs/compiler-differences.md#float-and-ints

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.

3 participants