i writing function parses json object. emit structured error messages indicate specific fields have errors in them.
originally checked if error type *json.unmarshaltypeerror
, retrieved json tag name field
property. unfortunately fails if struct i'm unmarshalling json has custom types implement own unmarshaljson
functions. errors return custom errors , there no way determine field struct came from.
playground contrasting built in vs custom error: https://play.golang.org/p/auh3pe7j5h
at point i'm considering changing using reflection, unmarshalling object map of json.rawmessage
, unmarshalling 1 field @ time can identify problematic field(s). there simpler way? require me duplicate internal json package logic analyze json tags figure out field unmarshal each raw message into.
json.unmarshaltypeerror
exported, of fields. there's no reason can't return error type custom marshalers. in fact, i'd venture intended use of type!
func (third *second) unmarshaljson(data []byte) error { return &json.unmarshaltypeerror{ // ... } }
Comments
Post a Comment