Skip to content

JSON encoding of floats does not match the documentation #563

Closed
@egormkn

Description

@egormkn

Hello,

The documentation states that:

Starlark float values are encoded using decimal point notation, even if the value is an integer.

This suggests that float(1) should be encoded as 1.0, similar to Python:

>>> import json
>>> json.dumps([1.0])
"[1.0]"

However, Starlark uses %g formatting, which truncates trailing zeros and the decimal point. Hence:

>>> json.encode([1.0])
"[1]"

It appears that "encoding/json" module in Go exhibits the same behavior.

Even a comment in the Starlark source code misleadingly states that a float representation "always contains a decimal point":

fmt.Fprintf(buf, "%g", x) // always contains a decimal point

In my opinion, since Starlark tries to be similar to Python, the JSON serialization should behave the same way and preserve the information about the floating-point type. I don't expect that changing the formatting will break existing code, but even so the current implementation will definitely lead to bugs. However, in case you decide not to change anything here, please update the documentation accordingly.

Thank you!

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions