Skip to content

fix: keep integral floats as floats when serializing facet_value::Value - #75

Open
theaddonn wants to merge 2 commits into
facet-rs:mainfrom
theaddonn:fix/value-integral-float-serialization
Open

theaddonn wants to merge 2 commits into
facet-rs:mainfrom
theaddonn:fix/value-integral-float-serialization

Conversation

@theaddonn

@theaddonn theaddonn commented Oct 10, 2026 •

Copy link
Copy Markdown

Fixes #68.

What's wrong

If a facet_value::Value holds a float like 1.0, it gets serialized as an integer. So 1.0 turns into 1 after a JSON round trip, and postcard writes it with the I64 tag instead of F64, so it comes back as an integer.

The cause: the serializer asks the value for as_i64() first, then as_u64(), then as_f64(). facet-value happily answers as_i64() with Some(1) for a stored 1.0, so the float case is never reached.

What I changed

  • facet-value: dyn_get_i64 / dyn_get_u64 now only answer for numbers that were stored as integers. Floats only come out through get_f64. VNumber::to_i64() and friends are untouched.
  • facet-format: serialize_scalar_from_dynamic (a dynamic value serialized into an integer-typed field) relied on the old lenient behavior, so it now falls back to as_f64() for whole-number floats. That path behaves the same as before.
  • facet-json: without the fast feature, f64 was printed with Display, which writes 1.0 as 1. It now uses Debug, which prints 1.0, same as the fast path. Without this, the repro from the issue still fails in the default config. This also affects plain f64 fields: x: f64 = 1.0 now comes out as 1.0 either way.

I went with the first option from the issue (the integer getters return None for floats), since the other option would need a change to the dynamic value vtable in facet-core.

Heads up

Anything calling as_i64() / as_u64() on a PeekDynamicValue / PokeDynamicValue backed by a Value now gets None for floats, even whole-number ones. In this repo the only callers are the two serializer paths above.

Tests

New tests in facet-postcard/tests/integration/issue_68.rs (it's the one crate that has both facet-json and facet-value as dev-dependencies). They cover the JSON repro from the issue, integers staying integers, and a postcard round trip keeping the float tag. Three of the four fail on main.

  • cargo nextest run --workspace: 2780 passed
  • cargo nextest run -p facet-postcard -p facet-json -p facet-value --features facet-json/fast: 627 passed
  • just clippy and cargo fmt --check are clean

A Value holding a float with an integral value (1.0) was serialized as an
integer: the dynamic number branch probes as_i64/as_u64 before as_f64, and
facet-value's integer getters answered for F64-tagged numbers. JSON wrote
`1`, and tagged formats emitted the I64 dynamic value tag.

- facet-value: dyn_get_i64/dyn_get_u64 only answer for integer numbers
- facet-format: integer-typed targets still accept integral floats from
  dynamic values, now via as_f64
- facet-json: without the `fast` feature, write f64 with Debug so 1.0 is
  written as `1.0`, matching the zmij output

Fixes facet-rs#68
@codecov

codecov Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@theaddonn

Copy link
Copy Markdown
Author

Hey @fasterthanlime, could you take a look at this when you get a chance? It's been open for a couple of weeks. Happy to change anything. Thanks!

This branch has not been deployed

No deployments
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.

Serializing facet_value::Value writes floats with integral values as integers (1.0 -> 1)

1 participant