A very simple way to encode short strings.
Go
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.travis.yml .travis.yml Dec 25, 2017
LICENSE Create LICENSE Dec 23, 2017
README.md New API Feb 16, 2018
encoder.go Messed up ordering Feb 16, 2018
encoder_test.go Messed up ordering Feb 16, 2018
go.mod add go.mod May 23, 2018
sizer.go Messed up ordering Feb 16, 2018
sizer_test.go New API Feb 16, 2018

README.md

stringsizer

travis go report card coverage godocs

A very simple way to exchange keys in a map for a shorter version of the key (1-2 chars). Basically it converts {"some-long-key":"data"} into "a":"data". It keeps track of how the map keys are converted so they can be converted back to the original. Note: The resulting data is not JSON (its missing {}) which makes it a little smaller, and also forces you to transform back to use it.

Why?

I plan on encoding the same set of 10-100 MAC addresses in JSON payloads that are each inserted to a row of a database. This way will be a fast and efficient way to store encode the JSON names across every row so that it reduces the size of the keys (from 17 bytes to 1).

Usage

// make a map
someMap := make(map[string]interface{})
someMap["some-long-key"] = "data"

// Create a new string sizer
ss, _ := New()

// create a shortened string JSON version of the map
shortedJSONOfSomeMap := ss.ShrinkMapToString(someMap)
fmt.Println(shortedJSONOfSomeMap)
// "a":"data"

// Store the string sizer to expand it later
saved := ss.Save()
fmt.Println(saved)
// {"encoding":{"some-long-key":"a"},"current":1}

// reload the string sizer
ss2, _ := New(saved)
// reload the original map from shortened string version
originalMap, _ := ss2.ExpandMapFromString(shortedJSONOfSomeMap)
fmt.Println(originalMap)
// map[some-long-key:data]

License

MIT