schollz / stringsizer

A very simple way to encode short strings.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

A very simple way to encode short strings.

License:MIT License


Languages

Language:Go 100.0%