Giter Site home page Giter Site logo

Sort keys of objects about jql HOT 9 OPEN

yamafaktory avatar yamafaktory commented on May 16, 2024 2
Sort keys of objects

from jql.

Comments (9)

yamafaktory avatar yamafaktory commented on May 16, 2024

Hi @jameschenjav and thanks for the positive feedback!

Basically jql keeps the insertion order because the preserve_order feature flag was introduced to avoid the default behavior of serde_json (#23, https://github.com/yamafaktory/jql/blob/main/Cargo.toml#L41).

Nobody complained about it so far... and I was the one introducing this (no issue).

So basically, we have two options:

  1. remove the flag and then everything will be sorted auto-magically
  2. introduce a sort flag

from jql.

jameschenjav avatar jameschenjav commented on May 16, 2024

Hi @yamafaktory

Thanks for your response.

It makes sense to keep original order by default. As it will be a breaking change, if someone had already built test-cases without proper comparison, the tests will break.

So imo better to have a new flag.

from jql.

yamafaktory avatar yamafaktory commented on May 16, 2024

A follow-up on this topic.

The current issue is that, as already mentioned, serde_json uses a flag to either internally process the data as a BTreeMap or an IndexMap (https://github.com/serde-rs/json/blob/master/src/map.rs#L30-L33). I haven't seen so far a simple way to bypass the flag limitation - basically being to switch from one to another data-structure. Also, to implement a custom sorting from scratch sounds like a poor idea as data can be deeply nested and performance will be pretty bad.

Still trying to investigate a bit. Let's keep this open for now.

from jql.

kmaasrud avatar kmaasrud commented on May 16, 2024

I've been investigating this a bit myself, and as you say, there is no way to conditionally toggle a crate feature, since features get applied at build-time.

However, I think Cargo can handle compiling two different versions of serde_json - one with the preserve_order feature and one without (and then simply naming them differently.) I can try this out in a PR if you want, and then we can test if there are any performance or bloat implications.

I personally think the best way is to put optional sorting behind a feature flag in jql. Feels less bloated. Though perhaps we can even hide the sort-flag double-dependency shenanigans behind a feature-flag as well?

from jql.

yamafaktory avatar yamafaktory commented on May 16, 2024

@kmaasrud I thought about that too but that would mean two different versions since this is a binary (this can works fine for a the library part of course). Folks that will install it will still get the default one.

from jql.

kmaasrud avatar kmaasrud commented on May 16, 2024

@yamafaktory, yes it seems like my idea is not possible.

The thought was to have two versions of serde_json compiled into the binary - one with the preserve_order feature and one without. That would allow us to do e.g. serde_json::from_str::<Value>(json_content) and serde_json_ordered::from_str::<Value>(json_content) based on a condition, where serde_json and serde_json_ordered are the same crate with different features. However, Cargo only allows multiple versions of the same crate if they are indeed DIFFERENT VERSIONS (e.g. 1.0.79 and 1.0.78.) That would be less than ideal - to put it mildly.

I say document the sorting behavior and then people can install their preferred version of jql with cargo install jql --features sorting, for example.

from jql.

yamafaktory avatar yamafaktory commented on May 16, 2024

That's an option indeed. My main concern is that it feels a bit awkward and might be even more weird regarding the packages distributed on different OS (not maintained here).

from jql.

yamafaktory avatar yamafaktory commented on May 16, 2024

Let's maybe keep this open for a bit longer and see if some trick might help fixing that.

from jql.

kmaasrud avatar kmaasrud commented on May 16, 2024

Let's maybe keep this open for a bit longer and see if some trick might help fixing that.

Sounds like a good idea 👍

from jql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.