Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-07-03 Thread Itai Ferber via swift-users
Hi Kevin, You’re right — this is one of the limitations of the box design here. One thing we can do is expose the underlying boxed value as a `KeyedDecodingContainerProtocol` existential value using an accessor on the box so you can down-cast. However, it shouldn’t be necessary to add any

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-07-01 Thread Kevin Wooten via swift-users
Itai, I tried copying JSONEncoder.swift from the swift repo and implementing the `Unevaluated` type in it. It doesn’t appear to be a workable solution due to the fact that the `KeyedEncodingContainer` type erasure box is the only value returned from `container(keyedBy:)`. This means that any

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-29 Thread Itai Ferber via swift-users
Hi Kevin, > On Jun 29, 2017, at 12:30 PM, Kevin Wooten via swift-users > wrote: > >> Hi Jon, >> >> I just joined this mailing list and have tried to catch up on the >> history of this thread, so please excuse me if I’ve missed something. >> >> I’m sorry the Codable

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-29 Thread Kevin Wooten via swift-users
> Hi Jon, > > I just joined this mailing list and have tried to catch up on the > history of this thread, so please excuse me if I’ve missed something. > > I’m sorry the Codable API at the moment does not answer your needs — > you’re clearly not the only one who’s run into this, so let’s see

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-23 Thread Randy Eckenrode via swift-users
I assume this issue affects other implementations of Encoder and Decoder as well. The most obvious way to represent a key with multiple types is an enum, but enums don’t have their conformances to Codable generated automatically. It’s possible to write your own, but it can get a bit ugly (e.g.,

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-23 Thread Jon Shier via swift-users
Itai: No need to apologize, I do appreciate the difficulties of designing this entire feature as quickly and completely as was required. An intermediate JSON type would be a great fix, though most useful if it exists outside of JSONDecoder so it's useful for encoding as well. As long as it

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-23 Thread Itai Ferber via swift-users
Hi Jon, I just joined this mailing list and have tried to catch up on the history of this thread, so please excuse me if I’ve missed something. I’m sorry the Codable API at the moment does not answer your needs — you’re clearly not the only one who’s run into this, so let’s see how we can

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-23 Thread Jon Shier via swift-users
Your gist is extremely interesting to me. I had tried something similar with Argo’s existing JSON enum, but was somewhat stymied by trying to decode it from an unkeyed container. I suppose I still don’t have a good grasp on all of the existing container types. Jon > On Jun 23,

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-23 Thread Jon Shier via swift-users
David: I never called the design silly (though I think it’s inadequate for some important usage and makes some strange decisions), I was referring to the fact that the (apparent) official solution can’t actually decode all of the JSON people use. It’s the same reason I brought up

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-23 Thread Randy Eckenrode via swift-users
> On Jun 17, 2017, at 10:07 PM, Chris Anderson via swift-users > wrote: > > Say I have a JSON object such as: > > { > "id": "4yq6txdpfadhbaqnwp3", > "email": "john@example.com ", > "name":"John Doe", > "metadata": { >

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-23 Thread David Hart via swift-users
> On 23 Jun 2017, at 03:45, Jon Shier via swift-users > wrote: > > I’m sorry, are you complaining about my use of Codable instead of more > precisely referring to the JSON endcode/decode functionality based on it in > Foundation, or are you honestly trying to say

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-22 Thread Jon Shier via swift-users
I’m sorry, are you complaining about my use of Codable instead of more precisely referring to the JSON endcode/decode functionality based on it in Foundation, or are you honestly trying to say that said functionality was never intended to be a general purpose JSON solution? If it’s not

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-22 Thread Greg Parker via swift-users
> On Jun 22, 2017, at 6:00 PM, Jon Shier via swift-users > wrote: > > My main concern here is that, as Swift’s official JSON parsing method, > Codable should be able to handle any JSON representation and use and it > doesn’t. Is this true? Is Codable intended to

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-22 Thread Tony Parker via swift-users
Hi Jon, > On Jun 22, 2017, at 6:00 PM, Jon Shier wrote: > > Tony: > My main concern here is that, as Swift’s official JSON parsing method, > Codable should be able to handle any JSON representation and use and it > doesn’t. In fact, it can’t. If that’s considered okay

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-22 Thread Jon Shier via swift-users
Tony: My main concern here is that, as Swift’s official JSON parsing method, Codable should be able to handle any JSON representation and use and it doesn’t. In fact, it can’t. If that’s considered okay by the designers of the library and Apple itself, then fine. I think it’s silly

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-22 Thread Tony Parker via swift-users
Hi Jon, Usually this boils down to a question of: what are you going to do with the Any? If you intended to cast it to a dictionary and get at its values using string keys, then writing a struct with the properties you care about is the way we recommend doing this. You don’t have to include

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-18 Thread Jon Shier via swift-users
Given that, per his description, “metadata” can be anything, creating a struct doesn’t really help. Jon Shier > On Jun 18, 2017, at 9:00 PM, somu subscribe wrote: > > Create a struct for Metadata and conform to Coding > > Code: > > let string = """ > { > "id":

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-18 Thread somu subscribe via swift-users
Create a struct for Metadata and conform to Coding Code: let string = """ { "id": "4yq6txdpfadhbaqnwp3", "email": "john@example.com", "name":"John Doe", "metadata": { "link_id": "linked-id", "buy_count": 4 } } """ let data = string.data(using: .utf8)! //Force unwrapping

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-18 Thread Jon Shier via swift-users
The more I use Codable, the less suited to networking it becomes. In reading a variety of blog posts about implementing custom Decodable support from JSON, I kept running across the same pattern. Basically, users had started implementing their own decoding protocols which wrap Decodable

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-18 Thread Jon Shier via swift-users
Your issue here is Any, which will likely never be Decodable. You’ll need an actual type to contain the raw JSON. Hilariously, I have to wonder if Argo’s JSON enum could be made Decodable, as it can represent every valid JSON type typically contained in the Any returned by

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-18 Thread Rien via swift-users
Dang, hit send too soon. Sorry. This does not address your question, so please ignore… (foot in mouth)! Regards, Rien Site: http://balancingrock.nl Blog: http://swiftrien.blogspot.com Github: http://github.com/Balancingrock Project: http://swiftfire.nl - An HTTP(S) web server framework in Swift

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-18 Thread Rien via swift-users
Are you looking for a general purpose JSON interpreter / API ? There are many of them around, and in fact I do have my own: https://github.com/Balancingrock/VJson With VJson I would write: let json = VJson.parse(… your json object…) and then access the metadata as: let buyCount = (json |

[swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-17 Thread Chris Anderson via swift-users
Say I have a JSON object such as: { "id": "4yq6txdpfadhbaqnwp3", "email": "john@example.com", "name":"John Doe", "metadata": { "link_id": "linked-id", "buy_count": 4 } } And with a struct of: struct User: Codable { var id: String var email: String