-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
Add useSnakeCasedKeys to JSONDecoder.KeyDecodingStrategy #14039
Conversation
Hi Norio, thanks for putting this together! Unfortunately, this would have to go through internal API review for additional review before we could accept it. @parkera What do you think about this? SR-6629 is the problem where you can't faithfully round-trip something like "myHTTPLink" → "my_http_link" → "myHttpLink". Right now, the approach on decode is to look at the JSON payload and apply the transformation of (We could add API to do this, like Norio is suggesting, or potentially change the semantics of |
@itaiferber For what's its worth, I tried using Edit: The fact that a change in key decoding strategy can break round-trip conversion feels like a serious issue to me. I would go as far as "correcting" the |
@hartbit @norio-nomura I forgot to update this PR (and the SR) with some additional discussion and notes for thought. Even if we decide to special-case Consider Right now, we do this by having two conversions — We could decide to "fix" this but still leave And at that, this is a special case for One thing to keep in mind, I think, is that these conversions are by nature lossy. There are always going to be edge cases where we've lost too much information to round-trip the keys. The question is where is the balance between preserving more cases and being consistent across the board. I currently favor being consistent here. |
Seems like a loose-loose situation where there is not great solution.
Couldn't How about special casing the |
Yes, caching is possible, though unfortunately this doesn't solve the problem for keys which are not iterable. Recognizing special words is certainly an option (and can be done transparently without updated API); the question is — what abbreviations are important enough to make it into that list? 🙂 All the ones you listed, I agree with. If we're doing (I'm not trying to say that just because there isn't a "best" solution we shouldn't do anything; it's just about trying to figure out the best way going forward.) Another solution is simply exposing |
I noticed another problem as a consequence of the current behaviour: resolving the round-trip issue in @norio-nomura's example requires giving the The more I think about it the more I think that @norio-nomura's |
No, we can't do that, unfortunately. Perhaps we should take this to the Swift Forums for discussion on possible ways forward. At this point, we're not going to be able to introduce new API into Swift 4.2, but maybe there are other directions we can take this. |
I started a thread on the Swift forums to discuss this in a more public setting. :) |
Thx @itaiferber! Would it worth moving the discussion to Swift Evolution? |
I think it would be good to gather feedback first about potential directions before suggesting API to Swift Evolution, but eventually, yes |
Was any conclusion on the issue reached through the discussion? |
There were three responses to the thread, all supportive of this approach; if we consider that to be enough signal to go ahead with this, we'll need to figure out how we want to review and schedule this API change. Given the amount of things going on at the moment, I'm not sure this will make the 4.2 release. @parkera Any thoughts here? |
Let's aim for 5.0. master is still the branch for that, but since we need time to do additional API review here, we will leave this open for a bit longer. |
Hi there. I hope that it is ok that I bump this thread. |
Any updates on this? Could we merge this and cherry pick into 5.1 perhaps? Seems like there's a lot of support for this so it would be a shame if this gets abandoned. |
Any changes made here would need to go through API review, which won't make it into Swift 5.1, unfortunately. We will try to push on this internally, given bandwidth. |
Okay! I hope it makes into Swift 5.2 then :) |
FYI, I opened jpsim/Yams#200 that can become a PoC improving this PR. |
It's been a while, so let's get this moving again. Because this change is API and ABI breaking (not that I anticipate there are many clients switching over this enum), I'm going to tag this as pending evolution discussion. |
I have checked, and I stand corrected. The overlays are Foundation’s and are not subject to evolution. Hopefully @parkera can get this reviewed in time. |
cc @bendjones on this one |
Hi there, I am certain that the issue is very well understood, but I think that the current state causes a lot of confusion - even for experienced developers. The intention was to keep the key conversion in the encoder and decoder, but the current state is that this often 'leaks' into the
As mentioned elsewhere, the big issue is that the 'imageUrl' representation belongs neither to the source or the destination domains but must be added for the sake of the key coding strategy. |
Adds
JSONDecoder.KeyDecodingStrategy.useSnakeCasedKeys
.This will make following code works.
/cc: @itaiferber
Resolves SR-6629.