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

proposal: encoding/json: add ... json tag to spread #52138

Closed
maru44 opened this issue Apr 4, 2022 · 4 comments
Closed

proposal: encoding/json: add ... json tag to spread #52138

maru44 opened this issue Apr 4, 2022 · 4 comments

Comments

@maru44
Copy link

maru44 commented Apr 4, 2022

I added new json tag.
This can be instead of embedded generics.

As you know, we cannot embed generics to struct.

this is invalid

type bad[T any] struct{
    Name string
    T
}

I want use embed in json, so I added new json tag to spread struct in its parent.

ex)

type good[T any] struct {
    Name string
    Free T `json:"..."`
}

type more struct {
    Age int
    Country string
}

var foo = good[more]{
    Name: "Foo",
    more: more{
        Age: 20,
        Country: "U.S.A",
    },
}

If you encode it in json. It'll be like this.

{"Name": "Foo", "Age": 20, "Country": "U.S.A"}

My PR.

@maru44 maru44 added the Proposal label Apr 4, 2022
@gopherbot gopherbot added this to the Proposal milestone Apr 4, 2022
@icholy
Copy link

icholy commented Apr 4, 2022

The door isn't closed on allowing embedding type parameters #43621

@DeedleFake
Copy link

DeedleFake commented Apr 4, 2022

Anonymous fields can also be manually named, overriding their typical behavior. I think having a way to do the opposite and treat a named field as though it were anonymous makes sense. Even without generics, it would allow you to have a field of an interface type and spread the values to the parent object:

type Example {
  ID int `json:"id"`
  Name string `json:"name"`
  Extra any `json:"..."`
}

@dsnet
Copy link
Member

dsnet commented Apr 4, 2022

This sounds like a duplicate of #6213.

@ianlancetaylor
Copy link
Contributor

Closing as dup. Please comment if you disagree.

@golang golang locked and limited conversation to collaborators Apr 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants