JSONDecoder has too few opportunities. need an enhancement.

1. Sometimes it is contently to be able to decode a struct or an object from a Foundation object (either NSArray or NSDictionary) that produce a valid JSON.
For example, if I use a 3d party library that provides NSDictionary to me. Due to JSONDecoder decodes only from Data, in the first place I have to convert NSDictionary to Data in order to use the one so far. But the decoder does the back action first thing.
I created Pull Request<https://github.com/apple/swift/pull/12791&gt;, that was closed.

2. Is it possible to use JSONDecoder when I don’t know the type of a particular property of the required type?
For instance, if I develop a library that allows bi-directional communication between server and client. The library wraps transmitted data into an internal object in a way that one of the object’s properties is the data. So, the type of the property is unknown inside the library. I want to decode the internal object with JSONDecoder, but I can’t specify the type of one property.

Hi Vladimir,

FYI, I also wanted to decode directly from NSDictionary and NSArray, so I
made https://github.com/norio-nomura/ObjectEncoder\.
It is a simpler implementation than JSONEncoder/JSONDecoder because it does
not contain any functions to treat specific type specially.

···

2017-11-10 1:37 GMT+09:00 Vladimir Kushelkov via swift-corelibs-dev < swift-corelibs-dev@swift.org>:

1. Sometimes it is contently to be able to decode a struct or an object
from a Foundation object (either NSArray or NSDictionary) that produce a
valid JSON.
For example, if I use a 3d party library that provides *NSDictionary* to
me. Due to *JSONDecoder* decodes only from *Data*, in the first place I
have to convert *NSDictionary* to *Data* in order to use the one so far.
But the decoder does the back action first thing.
I created Pull Request <https://github.com/apple/swift/pull/12791&gt;, that
was closed.

2. Is it possible to use *JSONDecoder* when I don’t know the type of a
particular property of the required type?
For instance, if I develop a library that allows bi-directional
communication between server and client. The library wraps transmitted data
into an internal object in a way that one of the object’s properties is the data.
So, the type of the property is unknown inside the library. I want to
decode the internal object with JSONDecoder, but I can’t specify the type
of one property.

_______________________________________________
swift-corelibs-dev mailing list
swift-corelibs-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-corelibs-dev

1 Like

Hi Vladimir,

1. Sometimes it is contently to be able to decode a struct or an object from a Foundation object (either NSArray or NSDictionary) that produce a valid JSON.
For example, if I use a 3d party library that provides NSDictionary to me. Due to JSONDecoder decodes only from Data, in the first place I have to convert NSDictionary to Data in order to use the one so far. But the decoder does the back action first thing.
I created Pull Request <https://github.com/apple/swift/pull/12791&gt;, that was closed.

Thanks for putting together this pull request! As Tony mentioned in his response to it, this type of change would need to go through API review, both internal, and external, before we could accept it.
This needs to be squared away with other work on JSONDecoder/PropertyListDecoder — you may have seen a recent email on here from Florent Vilmart about exposing the underlying _JSONEncoder and _JSONDecoder; we’re currently considering exposing the underlying conversion mechanism here in an Encoder/Decoder pair similar to Norio’s ObjectEncoder. There might be a more general solution here than just exposing the behavior on JSONDecoder.

This is being tracked internally, and I hope to have a good answer in the next Swift release. In the meantime, there are workarounds like what Norio has done.

2. Is it possible to use JSONDecoder when I don’t know the type of a particular property of the required type?
For instance, if I develop a library that allows bi-directional communication between server and client. The library wraps transmitted data into an internal object in a way that one of the object’s properties is the data. So, the type of the property is unknown inside the library. I want to decode the internal object with JSONDecoder, but I can’t specify the type of one property.

No, this is not possible. You cannot decode an object without being able to specify its type statically — you’ll need the client to somehow pass along the type, or provide enough information for you to decode on their behalf.

···

On Nov 9, 2017, at 8:36 AM, Vladimir Kushelkov via swift-corelibs-dev <swift-corelibs-dev@swift.org> wrote:

_______________________________________________
swift-corelibs-dev mailing list
swift-corelibs-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-corelibs-dev

Hi Norio, thank you for your answer.

I’ve looked thought you library. So, basically, you took/copied the source code of JSONDecoder/JSONEncoder.
Now I’m using ObjectMapper<https://github.com/Hearst-DD/ObjectMapper&gt;\. I don’t want to replace the one with you library. I wish I could remove this dependency at all.
By the way, your library doesn’t support the decoding strategies (DateDecodingStrategy, DataDecodingStrategy, ect.)

I hope, swift team will extend JSONDecoder in the next swift update.

···

On 10 Nov 2017, at 02:44, Norio Nomura <norio.nomura@gmail.com<mailto:norio.nomura@gmail.com>> wrote:

Hi Vladimir,

FYI, I also wanted to decode directly from NSDictionary and NSArray, so I made https://github.com/norio-nomura/ObjectEncoder\.
It is a simpler implementation than JSONEncoder/JSONDecoder because it does not contain any functions to treat specific type specially.

2017-11-10 1:37 GMT+09:00 Vladimir Kushelkov via swift-corelibs-dev <swift-corelibs-dev@swift.org<mailto:swift-corelibs-dev@swift.org>>:
1. Sometimes it is contently to be able to decode a struct or an object from a Foundation object (either NSArray or NSDictionary) that produce a valid JSON.
For example, if I use a 3d party library that provides NSDictionary to me. Due to JSONDecoder decodes only from Data, in the first place I have to convert NSDictionary to Data in order to use the one so far. But the decoder does the back action first thing.
I created Pull Request<https://github.com/apple/swift/pull/12791&gt;, that was closed.

2. Is it possible to use JSONDecoder when I don’t know the type of a particular property of the required type?
For instance, if I develop a library that allows bi-directional communication between server and client. The library wraps transmitted data into an internal object in a way that one of the object’s properties is the data. So, the type of the property is unknown inside the library. I want to decode the internal object with JSONDecoder, but I can’t specify the type of one property.

_______________________________________________
swift-corelibs-dev mailing list
swift-corelibs-dev@swift.org<mailto:swift-corelibs-dev@swift.org>
https://lists.swift.org/mailman/listinfo/swift-corelibs-dev