-
Notifications
You must be signed in to change notification settings - Fork 17.6k
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 "inline" struct tag #6213
Comments
I think this is worthwhile. I have an unfinished proposal in my email drafts that is basically exactly what Gustavo describes in his bson release announcement mail. It should be simple enough that it could be done before the 1.2 cutoff. Any disagreement? Labels changed: added priority-soon, go1.2, feature, removed priority-triage. Owner changed to @adg. Status changed to Accepted. |
https://golang.org/cl/13180043 Status changed to Started. |
This issue was closed by revision 466001d. Status changed to Fixed. |
This issue was closed by revision 27f4166. Status changed to Fixed. |
And that was the undo CL closing it again :/ New CL https://golang.org/cl/13324045/ Status changed to Started. |
Comment 17 by andrew@happyinspector.com: +1 I have something where I have a some common fields (id, path, revision, etc) and I use an anonymous struct that is embedded in other structs. However, I can't implement a custom json marshal on the common struct as then all the other structs will only return the output from the marshalled common fields. |
Comment 18 by andrew@happyinspector.com: +1 I have something where I have some common fields (id, path, revision, etc) and I use an anonymous struct that is embedded in other structs. However, I can't implement a custom json marshal on the common struct as then all the other structs will only return the output from the marshalled common fields. |
This comment was marked as spam.
This comment was marked as spam.
I've read through the comments a few times, and I'm confused. The ticket is still open, is this still an active proposal? Are we waiting for a better suggestion on syntax / naming? |
At this point, I'd be happy to have ANY solution - the work-arounds are killers! |
@dsnet Ping on my question above. What is the current status of this? 9 years later, and this is still an issue for many of us. I would be perfectly happy to work on the issue, but since I am not sure what the current blocking point is, I'm not sure where to go from here. Requiring that the 'inline' struct tag have either a blank name, or one of '-' or '+', plus the 'any' tag, so something like ",any", "-,any", or even "+,any" seems like it would get past all of the issues regarding simply using - or + as the only indicator. Using 'any' seems to make sense, as that is used by the XML encoder, but I'm happy with another keyword if that is preferred. |
If I'm reading the thread correctly, this was implemented by adg earlier in the thread here but was never accepted. The tag added was I think it's a feature worth adding as leveraging embedding (anonymous field) won't always work, as I've encountered recently with generics. |
Hi @n-bruno. As best as I can tell, that change was merged by accident, backed out... And then never revisited, at least as far as I can tell. In my case, what I really want to do is embed a map of additional arbitrary JSON key/value pairs, which, of course, also isn't possible with embedding. |
@dsnet Ping again? |
+1 for adding explicit inlining, in whatever form. I was trying to "concatenate" two structs into one where both structs are only known through some interfaces:
In this case, The approach above doesn't work at the moment because anonymous interface fields, in contrast to anonymous structs, are not inlined. |
There's a prototype implementation of @pohly, I don't know how to efficiently implement inlining of For performance, the "json" package pseudo-statically analyzes a Go struct type to derive a table of how it's supposed to be marshaled. To avoid repeating this expensive work, it caches the computed result. This approach requires approximately O(N) memory, where N is total number of unique Go types. Once you allow inlining of interface types, you run into combinatorial explosion of possible pseudo-types. As a simple example, consider: type S struct {
A any `json:",inline"`
} The |
I see. So even if it could be made to work, for example by not caching such types, performance would be worse than it it is now. We care about performance here in Kubernetes, so this wouldn't help. Ideally what we would need is some kind of "override" feature where the encoder is told to ignore a certain field and instead encode some other value. But that is a different problem than inlining and doesn't belong here. |
This comment was marked as spam.
This comment was marked as spam.
Hi all, we kicked off a discussion for a possible "encoding/json/v2" package that addresses the spirit of this proposal. |
I wonder why I can see |
Because non-standard JSON libraries do support it. |
by bjruth:
Edited by @dsnet on 2020-11-10 to fix formatting.
The text was updated successfully, but these errors were encountered: