|
1 | 1 | # orderedmap |
2 | 2 |  |
3 | 3 |
|
4 | | -An alternative generic ordered map in Go with de/serializing from/to JSON. |
| 4 | +A generic ordered map implementation in Go that preserves insertion order and supports JSON serialization/deserialization. |
5 | 5 |
|
6 | | -## How to use |
| 6 | +## Installation |
7 | 7 |
|
8 | | -See the [example code](./example_test.go). |
| 8 | +```bash |
| 9 | +go get github.com/kazamori/orderedmap |
| 10 | +``` |
| 11 | + |
| 12 | +## Features |
| 13 | + |
| 14 | +- Generic type support with `OrderedMap[K, V]` |
| 15 | +- Preserves insertion order of keys |
| 16 | +- JSON marshaling/unmarshaling with order preservation |
| 17 | +- O(1) key lookup |
| 18 | + |
| 19 | +## Quick Start |
| 20 | + |
| 21 | +```go |
| 22 | +package main |
| 23 | + |
| 24 | +import ( |
| 25 | + "encoding/json" |
| 26 | + "fmt" |
| 27 | + |
| 28 | + "github.com/kazamori/orderedmap" |
| 29 | +) |
| 30 | + |
| 31 | +func main() { |
| 32 | + // Create a new ordered map |
| 33 | + m := orderedmap.New[string, any]() |
| 34 | + m.Set("name", "Alice") |
| 35 | + m.Set("age", 30) |
| 36 | + m.Set("active", true) |
| 37 | + |
| 38 | + // Iterate in insertion order |
| 39 | + for _, p := range m.Pairs() { |
| 40 | + fmt.Printf("%s: %v\n", p.Key, p.Value) |
| 41 | + } |
| 42 | + |
| 43 | + // Get a value |
| 44 | + if val, ok := m.Get("name"); ok { |
| 45 | + fmt.Println("Name:", val) |
| 46 | + } |
| 47 | + |
| 48 | + // Serialize to JSON (preserves order) |
| 49 | + data, _ := json.Marshal(m) |
| 50 | + fmt.Println(string(data)) |
| 51 | + // Output: {"name":"Alice","age":30,"active":true} |
| 52 | +} |
| 53 | +``` |
9 | 54 |
|
10 | | -## test CLI |
| 55 | +## JSON Unmarshaling |
11 | 56 |
|
12 | | -For testing, deserialize JSON and then serialize it while preserving the order. |
| 57 | +Parse JSON while preserving the original key order: |
13 | 58 |
|
| 59 | +```go |
| 60 | +jsonData := []byte(`{"z":"last","a":"first","m":"middle"}`) |
| 61 | +m := orderedmap.New[string, any]() |
| 62 | +json.Unmarshal(jsonData, m) |
| 63 | + |
| 64 | +for _, p := range m.Pairs() { |
| 65 | + fmt.Printf("%s: %v\n", p.Key, p.Value) |
| 66 | +} |
| 67 | +// Output: |
| 68 | +// z: last |
| 69 | +// a: first |
| 70 | +// m: middle |
14 | 71 | ``` |
15 | | -$ ./bin/cli -data '{"s":"test","i":3,"a":[{"f":3.14},{"b":true}]}' |
| 72 | + |
| 73 | +## API Reference |
| 74 | + |
| 75 | +| Function/Method | Description | |
| 76 | +|----------------|-------------| |
| 77 | +| `New[K, V]()` | Create a new empty ordered map | |
| 78 | +| `WithCapacity[K, V](size)` | Create with pre-allocated capacity | |
| 79 | +| `NewFromMap[M, K, V](m)` | Create from a standard Go map | |
| 80 | +| `Set(key, value)` | Add or update a key-value pair | |
| 81 | +| `Get(key)` | Retrieve a value by key | |
| 82 | +| `Pairs()` | Get all pairs in insertion order | |
| 83 | +| `String()` | JSON string representation | |
| 84 | +| `ToMap[M, K, V](om)` | Convert to standard Go map | |
| 85 | + |
| 86 | +## Examples |
| 87 | + |
| 88 | +See [example_test.go](./example_test.go) for more usage examples. |
| 89 | + |
| 90 | +## CLI Tool |
| 91 | + |
| 92 | +A CLI tool is included for testing JSON round-trip serialization: |
| 93 | + |
| 94 | +```bash |
| 95 | +make build |
| 96 | +./bin/cli -data '{"s":"test","i":3,"a":[{"f":3.14},{"b":true}]}' |
| 97 | +``` |
| 98 | + |
| 99 | +Output: |
| 100 | +```json |
16 | 101 | { |
17 | 102 | "s": "test", |
18 | 103 | "i": 3, |
|
0 commit comments