Re: [swift-corelibs-dev] Adding type conversion capabilities to JSON encode/decode

2017-08-30 Thread Sneed, Brandon via swift-corelibs-dev
Just a quick follow up on that last reply… As dumpy of an excuse as that is (doing it because the other 2 big platforms do it) when we’re talking about some tech that is very cross platform, I think it’s worth considering. Brandon Sneed From: on behalf

Re: [swift-corelibs-dev] Adding type conversion capabilities to JSON encode/decode

2017-08-30 Thread Sneed, Brandon via swift-corelibs-dev
Yeah, I don’t know about the “many types” part. But essentially, if you’re a service developer, you’ll probably have at least 3 clients. Web, Android and iOS. Javascript does this conversion transparently, GSON also does this conversion. iOS ends up being the odd man out in this case. This

Re: [swift-corelibs-dev] Adding type conversion capabilities to JSON encode/decode

2017-08-30 Thread Sneed, Brandon via swift-corelibs-dev
Hi Youming, No worries. Some of the verbage overlaps enough that it makes the conversation a little difficult to follow. The jist is basically that if I type my struct with an Int inside, the JSON payload contains a string, I want an Int back if at all possible. If my struct has a string,

Re: [swift-corelibs-dev] Adding type conversion capabilities to JSON encode/decode

2017-08-30 Thread Tony Parker via swift-corelibs-dev
> On Aug 30, 2017, at 3:12 PM, Sneed, Brandon wrote: > > Thanks Tony, > > Ah. That should work, and covers benefit #2 I mentioned very nicely. Only > downside is that as a developer on an app I may not expect that type to > change on the server side so I wouldn’t do it

Re: [swift-corelibs-dev] Adding type conversion capabilities to JSON encode/decode

2017-08-30 Thread Youming Lin via swift-corelibs-dev
Brandon Sorry, I didn't realize you want it on the String type since you used other string ivars in your example as well. I'm not sure that forcing all numbers to be valid decoded strings is a good idea, because this behavior gets applied to other string ivars that you may not want to be

Re: [swift-corelibs-dev] Adding type conversion capabilities to JSON encode/decode

2017-08-30 Thread Sneed, Brandon via swift-corelibs-dev
Thanks Tony, Ah. That should work, and covers benefit #2 I mentioned very nicely. Only downside is that as a developer on an app I may not expect that type to change on the server side so I wouldn’t do it by default, and when it does happen, I then need to apply it to each of its siblings

Re: [swift-corelibs-dev] Adding type conversion capabilities to JSON encode/decode

2017-08-30 Thread Tony Parker via swift-corelibs-dev
I haven’t tried this myself yet, but you could imagine creating a type which represents an ABV and decodes using a single value container. Then, your custom behavior goes into the Codable implementation of this ABV type. Struct Beer then looks like: struct Beer: Codable { let name: String

Re: [swift-corelibs-dev] Adding type conversion capabilities to JSON encode/decode

2017-08-30 Thread Sneed, Brandon via swift-corelibs-dev
Thanks Youming, That’s not quite what I meant. I may have misinterpreted what Tony was saying though. I wanted to do conversion on String, not the containing type. The problem of doing it on the containing type is that as soon as you need one field to be custom, you’re roped into handling

Re: [swift-corelibs-dev] Adding type conversion capabilities to JSON encode/decode

2017-08-30 Thread Youming Lin via swift-corelibs-dev
Brandon I cooked up a simple example and it works as expected. ylin@youming-mbpr:~/Swift/Configuration$ swift Welcome to Apple Swift version 4.0-dev (LLVM 2dedb62a0b, Clang b9d76a314c, Swift 0899bd328a). Type :help for assistance. 1> import Foundation 2> struct A: Codable { 3. var

Re: [swift-corelibs-dev] Adding type conversion capabilities to JSON encode/decode

2017-08-30 Thread Sneed, Brandon via swift-corelibs-dev
Thanks Youming, Ok, thanks! I did try that, but I can’t seem to figure out how to make it actually get used in the decoding process. That being my preferred way, I tried it first. I chalked it not working up to Swift not knowing which of the 2 init(from decoder:) functions to call, mine or

Re: [swift-corelibs-dev] Adding type conversion capabilities to JSON encode/decode

2017-08-30 Thread Youming Lin via swift-corelibs-dev
Brandon >JSON’s types effectively end up matching specifically to primitives, of which there is no mechanism to override the behavior of how a String gets decoded for instance. You can override the default behavior with your own custom init(from:) implementation for your Codable struct:

Re: [swift-corelibs-dev] Adding type conversion capabilities to JSON encode/decode

2017-08-30 Thread Sneed, Brandon via swift-corelibs-dev
Hi Tony, I like the idea that the type itself is responsible for the conversion. My own json encode/decode library worked this way and it was really great, however in trying to leverage Swift4 into it, or to replace it, I just don’t see how that’s possible given how it’s currently structured.

Re: [swift-corelibs-dev] Adding type conversion capabilities to JSON encode/decode

2017-08-30 Thread Sneed, Brandon via swift-corelibs-dev
Hi Itai, No problem! Thanks for the heads up. Is there any way I could be involved? Happy to do the work to whatever guidance your team might have. I’m mostly just interested in it being there soon, hence volunteering. Thanks! Brandon Sneed From: on behalf of Itai

Re: [swift-corelibs-dev] Adding type conversion capabilities to JSON encode/decode

2017-08-30 Thread Itai Ferber via swift-corelibs-dev
Hi Brandon, Thanks for looking at this! We’ve got plans internally to potentially add a strategy to `JSONEncoder`/`JSONDecoder` to allow lenient conversions like this — i.e. implicitly stringify numbers (or parse them from string input), among some others. This would be opt-in for consumers

Re: [swift-corelibs-dev] Adding type conversion capabilities to JSONencode/decode

2017-08-30 Thread Youming Lin via swift-corelibs-dev
One downside I see is that the encoder wouldn't know if this specific field was originally a number instead of a string. If the same Codable struct is used for GET and POST, for example, the post-encode JSON string could be invalid because the server expects a number instead of a string for that

[swift-corelibs-dev] Adding type conversion capabilities to JSON encode/decode

2017-08-30 Thread Sneed, Brandon via swift-corelibs-dev
Hi everyone, Just throwing this out to see if anyone else is working on this, or has opinions/suggestions on how it’s implemented. I’d like to add this to the Codable/JSONDecoder/JSONEncoder system if no one else is working on it. Type type conversion, I mean given this JSON payload: {