Closed naoya-ashizawa closed 2 years ago
Hi @naoya-ashizawa,
The way to decode JSON from an ByteBuffer
is to use NIOFoundationCompat
from swift-nio
.
If you not already have you should add swift-nio
as a dependency to your Package.swift
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", from: "2.40.0"),
]
and afterwards add NIOFoundationCompat
as a dependency of your target e.g:
.target(
name: "NameOfYourTarget",
dependencies: [
...
.product(name: "NIOFoundationCompat", package: "swift-nio"),
],
),
NIOFoundationCompat
adds a method overload to JSONDecoder.deocde(_:from)
which takes a ByteBuffer
instead of Data
and your code will just work as is if you now import NIOFoundationCompat
.
We also have complete example but which uses async/await in the /Examples/GetJSON
folder of the repository. You can clone or download this repository and play around with the Example by opening the /Examples/Package.swift
.
Expected behavior
When I tried to get data from the API with AsyncHTTPClient and parse it into Codable, I had a problem.
Actual behavior
I got an error when compiling the following code.
swift code
error message
Steps to reproduce
and add async-http-client on dependencies
Dockerfile
docker-compose.yml
If possible, minimal yet complete reproducer code (or URL to code)
SwiftNIO version/commit hash