go - Decode any JSON value to string in Golang -
is there way in golang decode json value string. similar json.number why isn't there json.string?
for example, following decoded indicated
{"number": 123} => "123" {"string": "123"} => "123" {"float" : 123.45} => "123.45" {"bool" : true} => "true" {"empty" : ""} => "" {"null" : null} => ""
inspired post created jsonstring type. decode string, number, boolean or null values string.
https://play.golang.org/p/ucaxwril2k
type jsonstring string type jsonstring jsonstring func (st *jsonstring) unmarshaljson(barr []byte) (err error) { j, n, f, b := jsonstring(""), uint64(0), float64(0), bool(false) if err = json.unmarshal(barr, &j); err == nil { *st = jsonstring(j) return } if err = json.unmarshal(barr, &n); err == nil { *st = jsonstring(string(barr[:])) return } if err = json.unmarshal(barr, &f); err == nil { *st = jsonstring(string(barr[:])) return } if err = json.unmarshal(barr, &b); err == nil { *st = jsonstring(string(barr[:])) return } return }
Comments
Post a Comment