• Stars
    star
    356
  • Rank 119,446 (Top 3 %)
  • Language
    Go
  • License
    MIT License
  • Created over 7 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

orderedmap is a golang map where the keys keep the order that they're added. It can be de/serialized from/to JSON. It's based closely on the python collections.OrderedDict.

orderedmap

Build Status

A golang data type equivalent to python's collections.OrderedDict

Retains order of keys in maps

Can be JSON serialized / deserialized

Usage

package main

import (
    "encoding/json"
    "github.com/iancoleman/orderedmap"
)

func main() {

    // use New() instead of o := map[string]interface{}{}
    o := orderedmap.New()

    // use SetEscapeHTML() to whether escape problematic HTML characters or not, defaults is true
    o.SetEscapeHTML(false)

    // use Set instead of o["a"] = 1
    o.Set("a", 1)

    // add some value with special characters
    o.Set("b", "\\.<>[]{}_-")

    // use Get instead of i, ok := o["a"]
    val, ok := o.Get("a")

    // use Keys instead of for k, v := range o
    keys := o.Keys()
    for _, k := range keys {
        v, _ := o.Get(k)
    }

    // use o.Delete instead of delete(o, key)
    o.Delete("a")

    // serialize to a json string using encoding/json
    bytes, err := json.Marshal(o)
    prettyBytes, err := json.MarshalIndent(o, "", "  ")

    // deserialize a json string using encoding/json
    // all maps (including nested maps) will be parsed as orderedmaps
    s := `{"a": 1}`
    err := json.Unmarshal([]byte(s), &o)

    // sort the keys
    o.SortKeys(sort.Strings)

    // sort by Pair
    o.Sort(func(a *orderedmap.Pair, b *orderedmap.Pair) bool {
        return a.Value().(float64) < b.Value().(float64)
    })
}

Caveats

  • OrderedMap only takes strings for the key, as per the JSON spec.

Tests

go test

Alternatives

None of the alternatives offer JSON serialization.

More Repositories

1

bip39

A web tool for converting BIP39 mnemonic codes
JavaScript
3,507
star
2

strcase

A golang package for converting to snake_case or CamelCase
Go
1,013
star
3

shamir

Shamir Secret Sharing Scheme in single page which can be used offline.
HTML
221
star
4

cia_world_factbook_api

Converts the CIA World Factbook into a json data structure
Go
198
star
5

shamir39

Split BIP39 mnemonics using Shamir's Secret Sharing Scheme
HTML
189
star
6

jsbip39

Mnemonic code for generating deterministic keys
JavaScript
110
star
7

python-iwlist

Scanner and parser for wireless networks
Python
62
star
8

keycompression

Convert bitcoin keys between compressed and uncompressed format
HTML
61
star
9

slip39

A web tool for SLIP39 mnemonic shares
JavaScript
54
star
10

eip2333-tool

Tool for generating bls12-381 keys using the EIP2333 standard
JavaScript
20
star
11

multisig

A tool to create multisig addresses and transactions
HTML
17
star
12

iancoleman.io

My webpages
HTML
9
star
13

blsttc_ui

UI for blsttc rust library
JavaScript
8
star
14

blocksize_calculator

What is the effect of changing the bitcoin MAX_BLOCK_SIZE
JavaScript
5
star
15

bitcoin-qr-popup

A simple POS-friendly interface to the bitcoin client.
C#
4
star
16

entropy_bias_calculator

A calculator to show bias in entropy generated from base N events, eg dice rolls.
JavaScript
4
star
17

BitcoinArmory-Daemon

A json rpc interface to BitcoinArmory
Python
4
star
18

self_encryption_ui

Browser-based interface for rust library self_encryption
JavaScript
4
star
19

iancoleman.github.io

JavaScript
3
star
20

randchacha-js

The ChaCha random number generator for javascript. Inspired by and compatible with the rust crate rand_chacha
JavaScript
3
star
21

bls_interop

A tool for checking the interoperability of different rust bls12-381 libraries.
Rust
2
star
22

maid_distribution

JavaScript
2
star
23

safe_network_simulations

Simulates some scenarios for vault joining / leaving on the safe network
Go
2
star
24

log_viewer_for_safe_nodes

A way to visually explore multiple log vaults for MaidSafe safe_vault.
JavaScript
1
star
25

wallet_gui_for_sn

An experimental wallet GUI for Safe Network
Rust
1
star
26

everywhereium

A way to give tips to any web page.
JavaScript
1
star
27

harken

Go
1
star
28

insight-api-websocket-test

Simple test for insight-api websocket
1
star
29

codechain_exploration

Exploring the use of git and codechain together
1
star
30

keygen_bruteforce_for_prefix

How long does it take to find a key fitting a specific bit prefix?
Rust
1
star
31

monero-toolkit

Tools for interacting with monero
HTML
1
star
32

ed25519_vs_bls_key_benchmarks

Compare performance of ed25519_dalek and threshold_crypto bls keys.
Rust
1
star
33

bitcoinalafuture

A web service to gamble on the future price of bitcoin. Project has been abandoned for many months now.
JavaScript
1
star
34

perpetual_auction_currency_game

A game to simulate the Perpetual Auction Currency reward algorithm.
JavaScript
1
star
35

youtube_transcript_to_srt

Convert youtube transcript into srt subtitle file
Go
1
star
36

groupsize_calculator

Calculates the network security when changes are made to group size on the SAFE network
HTML
1
star