Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose an optional preserve_order feature that passes through to serde_json #187

Open
michaelkirk opened this issue May 12, 2022 · 1 comment

Comments

@michaelkirk
Copy link
Member

michaelkirk commented May 12, 2022

By default, serde_json uses a BTreeMap to store JSValue's, which means that properties can get unexpectedly re-ordered. serde_json has an optional (disabled by default) preserve_order feature for users who want fields to preserve the order in which they were inserted.

Most of the time reordering is fine, and it's faster, so probably we should maintain the default behavior.

The issue I'm facing right now is while working on geozero, when converting formats back and fourth, we have a notion of field order which is respected for some formats, but not geojson. It'd be convenient to be able to enforce field ordering in geojson for those who want it.

Another small thing, even though JSON equality is order agnostic, I think there is an expected ordering of fields in a geojson for readability, like this:

{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [125.6, 10.1]
  },
  "properties": {
    "name": "Dinagat Islands"
  }
}

Not:

{
  "properties": {
    "name": "Dinagat Islands"
  }
  "geometry": {
    "coordinates": [125.6, 10.1]
    "type": "Point",
  },
  "type": "Feature",
}

Adding the feature to Cargo.toml is pretty easy, but it breaks a lot of tests with hard-coded expectations around property order:

Left:  {"type":"Feature","geometry":{"type":"Point","coordinates":[1.1,2.1]},"properties":{}}
Right: {"geometry":{"coordinates":[1.1,2.1],"type":"Point"},"properties":{},"type":"Feature"}

We could change all these to be checks for structural equality, rather than exact string matching, but I wanted to check in before I spend the hour or so doing that, since there are a dozen or so broken tests.

@urschrei
Copy link
Member

field order which is respected for some formats

Huh, TIL. I think it's def a useful optional feature.

This was referenced May 13, 2022
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

No branches or pull requests

2 participants