Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to sort JSON in rust?

Is it possible to sort JSON in rust language? If it is possible then how?

Like this one:


const headers = {
  'checkout-account': '1234',
  'checkout-algorithm': 'sha256',
  'checkout-method': 'POST',
  'checkout-nonce': '564635208570151',
  'checkout-timestamp': '2018-07-06T10:01:31.904Z',
};

const calculateHmac = (body=false, params) => {
  const hmacPayload = Object.keys(params)
    .sort()
    .map((key) => [key, params[key]].join(':'))
    .concat(body ? JSON.stringify(body) : '')
    .join('\n');
};

calculateHmac(false, headers);

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

More or less the same code in Rust:

use itertools::Itertools;
use std::collections::HashMap;

fn main() {
    let headers = HashMap::from([
        ("checkout-account", "1234"),
        ("checkout-algorithm", "sha256"),
        ("checkout-method", "POST"),
        ("checkout-nonce", "564635208570151"),
        ("checkout-timestamp", "2018-07-06T10,01,31.904Z"),
    ]);

    let hmac_payload = headers
        .keys()
        .sorted()
        .map(|key| format!("{}:{}", key, headers[key]))
        .join("\n");

    println!("{hmac_payload}");
}

Plaground link

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading