datamodel-code-generator icon indicating copy to clipboard operation
datamodel-code-generator copied to clipboard

Extend parsing of Scientific Notation to JSON capabilites

Open maximilian-tech opened this issue 8 months ago • 5 comments

Solves Issues https://github.com/koxudaxi/datamodel-code-generator/issues/1955#issue-2290971596 and https://github.com/koxudaxi/datamodel-code-generator/issues/1952#issue-2287818185

It boils down to yaml vs. json floating point parsing. The SafeLoader of yaml has to be extended to allow for the same range of float notations like json. See here for more background. I copied the SO-solution and it does work (for me)

This is needed to postprocess Fortran output as it often as a e (e.g. 1e-5)for scientific notation, which yaml does not accept, but json does accept. And because the json schema input is parsed through a yaml loader, this notation is not accepted. This PR extends the yaml loader to accept json notation of scientific data representation.

maximilian-tech avatar Mar 27 '25 16:03 maximilian-tech

@gaborbernat I rebased and added the CLI option --extend-yaml-scientifc-notation.

The current implementation uses a global variable, but this is the best solution I could come up with. Ideally the command line option is gone in the '1.0' release and with it this 'construct' so I think it is ok for now.

maximilian-tech avatar Mar 27 '25 21:03 maximilian-tech

I added one test.

The pre-commit.ci bot does quite a lot of code changes I don't know how to prevent. This should probably be changed.

maximilian-tech avatar Mar 28 '25 20:03 maximilian-tech

Is there anything I can do to aid the progress here? I need this to parse json schema which contains Fortran style scientific notation ( e.g. 1e-5) which is butchered by the yaml loader.

maximilian-tech avatar Apr 14 '25 17:04 maximilian-tech

Yes, because the pre-commit bot breaks the code.

maximilian-tech avatar Apr 25 '25 18:04 maximilian-tech

Yes, because the pre-commit bot breaks the code.

You'll need to address that 🤔 by overriding the ruff checks causing the issue.

gaborbernat avatar Apr 28 '25 13:04 gaborbernat

Seems this stalled, I'll close it for now, but can reopen if you get back to it; there's now some merge conflicts that would also need fixed.

gaborbernat avatar Jul 25 '25 13:07 gaborbernat