-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.go
More file actions
38 lines (32 loc) · 1.01 KB
/
data.go
File metadata and controls
38 lines (32 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
// Export/Import of data in the form of string arrays.
// This can be used to unload the conditions of a problem (input)
// in a compressed form into the debug console and unpack it in the IDE.
import (
"bytes"
"compress/gzip"
"encoding/base64"
"encoding/json"
)
// DataExport serializes and compresses a slice of strings,
// returning a base64 encoded string.
func DataExport(data []string) string {
jsonData, _ := json.Marshal(data)
var gzBuf bytes.Buffer
gz := gzip.NewWriter(&gzBuf)
_, _ = gz.Write(jsonData)
_ = gz.Close()
return base64.StdEncoding.EncodeToString(gzBuf.Bytes())
}
// DataImport decodes a base64 string, decompresses it,
// and deserializes the JSON data into a slice of strings.
func DataImport(encodedData string) []string {
gzData, _ := base64.StdEncoding.DecodeString(encodedData)
gz, _ := gzip.NewReader(bytes.NewBuffer(gzData))
_ = gz.Close()
var jsonData bytes.Buffer
_, _ = jsonData.ReadFrom(gz)
var data []string
_ = json.Unmarshal(jsonData.Bytes(), &data)
return data
}