Skip to content

feat(parse json): add arbitrary_precision for serde_json#1656

Open
YJDoc2 wants to merge 1 commit intovectordotdev:mainfrom
YJDoc2:feat/serde_arbitrary_precision
Open

feat(parse json): add arbitrary_precision for serde_json#1656
YJDoc2 wants to merge 1 commit intovectordotdev:mainfrom
YJDoc2:feat/serde_arbitrary_precision

Conversation

@YJDoc2
Copy link

@YJDoc2 YJDoc2 commented Feb 24, 2026

Summary

This PR adds a non-default feature called arbitrary_precision in this crate, which enabls the same in the serde_json dependency. Along with that, it also adds the correct(?) handling for serde_json's internal in-band representation of certain numbers. Also see this specifically the header TLS Shenanigans for more info on this that I referred.

As the feature is non-default, by default it should not cause any penalty to users who do not enable it. For those who do, I have tried to make it minimally impacting.

I am opening this PR first to get initial review on if I have done this in the correct place, any discussion about motivation of the change, and if this change is something of wont-fix category etc.

Notes to reviewers -

  1. I will add corresponding in-code tests and any code comments after initial review
  2. The reason I think the issue with floating numbers is caused (see test pr section) is because vrl "wraps" the serde_json type in its own KeyString and Value structs, so serde_json cannot perform its own checks for in-band values like normal. Hence we also need to add the same checks in this crate
  3. I'd have preferred using the string from serde_json crate itself see here but the const is limited to crate visibility in serde_json, so cannot be used in this crate.

Change Type

  • Bug fix
  • New feature
  • Non-functional (chore, refactoring, docs)
  • Performance

Is this a breaking change?

  • Yes
  • No

How did you test this PR?

For a minimal reproduction and fix check of this issue, use the following program :

  1. Cargo.toml
[package]
name = "temp"
version = "0.1.0"
edition = "2024"

[dependencies]
serde_json = {version = "1.0.149", features = ["arbitrary_precision"]}
vrl = {path="..."} # for main branch
# vrl = {path="..." , features = ["arbitrary_precision"]} # for this pr
  1. src/main.rs
use std::collections::BTreeMap;
use vrl::{
    compiler::{Context, TargetValue, TimeZone, state::RuntimeState},
    value,
    value::{Secrets, Value},
};

fn main() {
    let src = ".body,_ = parse_json(.body)\n.";
    let fns = vrl::stdlib::all();
    let result = vrl::compiler::compile(src, &fns).unwrap();
    let mut target = TargetValue {
        value: value!({body: "{\"v1\":\"abc\",\"v2\":123456.123}"}),
        metadata: Value::Object(BTreeMap::new()),
        secrets: Secrets::default(),
    };
    let mut state = RuntimeState::default();
    let timezone = TimeZone::default();
    let mut ctx = Context::new(&mut target, &mut state, &timezone);
    let value = result.program.resolve(&mut ctx).unwrap();
    let v = serde_json::to_value(&value).unwrap();
    println!("{:?}",v.to_string());
}

Note here that body itself is a json string, and v2is a number. Now if we run it without the arbitrary precision feature (i.e. main branch) , we get output as

"{\"body\":{\"v1\":\"abc\",\"v2\":{\"$serde_json::private::Number\":\"123456.123\"}}}"

If we run with the feature enabled, we get the output as

"{\"body\":{\"v1\":\"abc\",\"v2\":123456.123}}"

I believe second is the correct one. Also this is specific to floats, if we make the number just 123456 , it works as expected on main branch as well.

Does this PR include user facing changes?

  • Yes. Please add a changelog fragment based on
    our guidelines. : Will do it after first review.
  • No. A maintainer will apply the "no-changelog" label to this PR.

Checklist

  • Our CONTRIBUTING.md is a good starting place.
  • If this PR introduces changes to LICENSE-3rdparty.csv, please
    run dd-rust-license-tool write and commit the changes. More details here. : N/A
  • For new VRL functions, please also create a sibling PR in Vector to document the new function. : N/A

References

Signed-off-by: Yashodhan Joshi <yjdoc2@gmail.com>
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.

Provide arbitrary_precision option for serde_json float conversions parse_json - differs parsing floats

1 participant