Custom unmarshaling function to allow package user to delegate the decoding of an Identity and distinguish between an Identity and a Bare. If the given message has a "id" field, it's considered being a proper Identity.
(raw json.RawMessage)
| 11 | // |
| 12 | // If the given message has a "id" field, it's considered being a proper Identity. |
| 13 | func UnmarshalJSON(raw json.RawMessage) (Interface, error) { |
| 14 | aux := &IdentityStub{} |
| 15 | |
| 16 | // First try to decode and load as a normal Identity |
| 17 | err := json.Unmarshal(raw, &aux) |
| 18 | if err == nil && aux.Id() != "" { |
| 19 | return aux, nil |
| 20 | } |
| 21 | |
| 22 | // abort if we have an error other than the wrong type |
| 23 | if _, ok := err.(*json.UnmarshalTypeError); err != nil && !ok { |
| 24 | return nil, err |
| 25 | } |
| 26 | |
| 27 | return nil, fmt.Errorf("unknown identity type") |
| 28 | } |