Closed eddie-velez closed 5 years ago
Hi,
You have 2 options, either use the builtin SliceString this way:
func (m *MyStruct) UnmarshalJSONObject(dec *gojay.Decoder, k string) error {
switch k {
case "fieldArray":
m.FieldArray = make([]string, 0, 5)
return dec.SliceString(&m.FieldArray)
}
}
Or use a custom type for your slice of strings implementing the UnmarshalerJSONArray interface:
type MyStringSlice []string
func (ms *MyStringSlice) UnmarshalJSONArray(dec *gojay.Decoder) error {}
type MyStruct struct {
Field1 string
Field2 string
FieldArray MyStringSlice
}
More doc about that here: https://github.com/francoispqt/gojay#arrays-slices-and-channels
Let me know if you have any other question.
Or use a custom type for your slice of strings implementing
@francoispqt How should it look like in func (m *MyStruct) UnmarshalJSONObject
function?
There are no examples of how to encode/decode []string when it's nested in a struct. For example:
type MyStruct struct { Field1 string Field2 string FieldArray []string }
If I implement the interface for marshaling and object and encoding each field within that implementation it gives me an error when I try to use: enc.ArrayKey or AddArrayKey. Is there an example which could be provided as I'm a bit stuck at the moment