Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/json: Go's json decoder should support '*' spill over #17079

Closed
liquidgecka opened this issue Sep 12, 2016 · 2 comments

Comments

Projects
None yet
3 participants
@liquidgecka
Copy link

commented Sep 12, 2016

If the process decoding JSON wants to detect unsupported fields its very difficult. Either the json has to be decoded into a map[string]json.RawMessage then parse out each field into sub object, etc. But this could be codified into the decoder very easily. For example I would love to see this work:

What I would love is to be able to write this as code:

type Object struct {
    Size int64 `json:"size"`
    Name string`json:"name"`
    Misc map[string]interface{} `json:"*"` 
}

func ParseObject() error {
    input := []byte(`{
        "size": 1,
        "name": "user_name",
        "unsupported_field": 10
    }`)

    obj := new(Object)
    if err := json.Unmarshal(input, obj); err != nil {
       return err
    }
    if len(obj.Misc) != 0 {
        return fmt.Errorf("Unsupported fields: %s", MakeKeysAList(obj.Misc))
    }

    return nil
}

In order to work json:"*" should only be used on a map[string]interface{} or a map[string]json.RawMessage. That is the one oddity to this that I can see outright.

@cespare

This comment has been minimized.

Copy link
Contributor

commented Sep 12, 2016

Possibly a duplicate of #6213; see also some relevant discussion in #15314.

@quentinmit quentinmit changed the title Go's json decoder should support '*' spill over encoding/json: Go's json decoder should support '*' spill over Sep 12, 2016

@liquidgecka

This comment has been minimized.

Copy link
Author

commented Sep 12, 2016

Ahh yea sure enough! That looks like exactly what I am suggesting.. I will close this out. =)

Thanks! I didn't see that in my initial searches.

@golang golang locked and limited conversation to collaborators Sep 12, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.