unmarshalling

JSON unmarshal integer field into a string

梦想的初衷 提交于 2020-08-21 06:47:29
问题 I am struggling with deserializing a integer into a string struct field. The struct field is a string and is expected to be assignable from users of my library. That's why I want it to be a string, since for the purpose of writing it to the database I actually don't care about the value inside. The users can supply text, but some just assign integers. Consider this struct: type Test struct { Foo string } Sometimes I end up with a JSON value that is valid but won't deserialize into the struct

JSON unmarshal integer field into a string

对着背影说爱祢 提交于 2020-08-21 06:46:34
问题 I am struggling with deserializing a integer into a string struct field. The struct field is a string and is expected to be assignable from users of my library. That's why I want it to be a string, since for the purpose of writing it to the database I actually don't care about the value inside. The users can supply text, but some just assign integers. Consider this struct: type Test struct { Foo string } Sometimes I end up with a JSON value that is valid but won't deserialize into the struct

cannot unmarshal string into Go struct field

痴心易碎 提交于 2020-08-20 04:40:32
问题 I have the following (incomplete type) (which is a response from docker API from manifests endpoint v2 schema 1 https://docs.docker.com/registry/spec/manifest-v2-1/) type ManifestResponse struct { Name string `json:"name"` Tag string `json:"tag"` Architecture string `json:"architecture"` FsLayers []struct { BlobSum string `json:"blobSum"` } `json:"fsLayers"` History []struct { V1Compatibility struct { ID string `json:"id"` Parent string `json:"parent"` Created string `json:"created"` } `json:

Unmarshal to struct with slice returns null value instead of empty slice

杀马特。学长 韩版系。学妹 提交于 2020-07-23 08:09:18
问题 If I create a "photo" without any tags it is stored in dynamodb as "tags": { "NULL": true }, But when I query and unmarshal the record I would expect that it converts this into an empty slice instead I get this: [{"photo_id":"bmpuh3jg","tags":null}] Is it possible to have it convert it into an empty slice instead? e.g. [{"photo_id":"bmpuh3jg","tags":[]}] CODE EXAMPLE My struct type Photo struct { Id string `json:"photo_id"` Tags []string `json:"tags"` } Query photo := &Photo{} input :=

Unmarshal to struct with slice returns null value instead of empty slice

允我心安 提交于 2020-07-23 08:08:10
问题 If I create a "photo" without any tags it is stored in dynamodb as "tags": { "NULL": true }, But when I query and unmarshal the record I would expect that it converts this into an empty slice instead I get this: [{"photo_id":"bmpuh3jg","tags":null}] Is it possible to have it convert it into an empty slice instead? e.g. [{"photo_id":"bmpuh3jg","tags":[]}] CODE EXAMPLE My struct type Photo struct { Id string `json:"photo_id"` Tags []string `json:"tags"` } Query photo := &Photo{} input :=

Unmarshal to struct with slice returns null value instead of empty slice

折月煮酒 提交于 2020-07-23 08:07:11
问题 If I create a "photo" without any tags it is stored in dynamodb as "tags": { "NULL": true }, But when I query and unmarshal the record I would expect that it converts this into an empty slice instead I get this: [{"photo_id":"bmpuh3jg","tags":null}] Is it possible to have it convert it into an empty slice instead? e.g. [{"photo_id":"bmpuh3jg","tags":[]}] CODE EXAMPLE My struct type Photo struct { Id string `json:"photo_id"` Tags []string `json:"tags"` } Query photo := &Photo{} input :=