Skip to content

Refactor FilterStructToMap for Deterministic JSON Output #679

@hanXen

Description

@hanXen

Problem

The current FilterStructToMap function returns a standard map[string]any. In Go, map iteration order is non-deterministic, and when marshaled with encoding/json, the keys are sorted alphabetically.

This behavior causes a mismatch: JSON generated directly from a struct preserves the field declaration order, but JSON generated after filtering through FilterStructToMap ends up alphabetically ordered.

While not a breaking issue for most parsers, this inconsistency is a poor user experience and can affect processes that rely on stable and predictable output order.

Proposal

To resolve this, we propose changing the return type of FilterStructToMap from map[string]any to *mapsutil.OrderedMap[string, any].

The OrderedMap type, which is already part of this utils library, is a special map implementation that preserves the insertion order of keys. By returning this type, we can guarantee a deterministic JSON output that matches the original struct field order.

This change will improve the utility for any tool that uses it for JSON serialization tasks.

Metadata

Metadata

Assignees

No one assigned

    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