I have to store a nested struct into datastore. As I'm running into the
datastore: flattening nested structs leads to a slice of slices: field
problem, I'd like to store the JSON object as is (as string?) to the datastore. Is this doable in Go?
You should be able to store the json.RawMessage. See the example in the package docs.
RawMessage is a raw encoded JSON object. It implements Marshaler and Unmarshaler and can be used to delay JSON decoding or precompute a JSON encoding.
It's a slice of byte
, but you can easily convert it to a string if you wish.
Yes its doable in golang
However complex (or nested) your datastore is, it can be converted to json. Just make sure map's key is a string. Also make sure elements of the datastore are public (starting with capital letter). If you don't want to encode a field you can keep it as private (beginning with small letter).
json.Marshal() will return a byte array, which can be saved into a file.
type Complex struct {
Data1 map[string]int
Data2 []byte
TimeStamp time.Time
}
type Datastore struct {
Name string
phones []string
Address map[string]string
noJson string // Wont be encoded as its not public
SomethingComplex map[string]Complex
}