Skip to content

[Variant] Avoid intermediate string allocations in JSON serialization #11396

Description

@haohuaijin

Is your feature request related to a problem or challenge?

Variant::String, Variant::ShortString, and object field names currently use serde_json::to_string to create a temporary String, then copy it into the output writer:

let json_str = serde_json::to_string(s)?;
write!(buffer, "{json_str}")?;

This introduces an unnecessary allocation and copy.

Describe the solution you'd like

Use serde_json::to_writer to serialize directly into the output writer. For string-valued Variants, to_json_string() can return serde_json::to_string directly instead of copying the result into another buffer.

Preserve JSON escaping and I/O error behavior.

Describe alternatives you've considered

No response

Additional context

Related: #10906, #11101.

Activity

  1. added
    enhancement
    on Oct 6, 2026
  2. added a commit that references this issue on Oct 26, 2026
    deeb725
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions