orlandos-nl / BSON

Native Swift library for BSON (http://bsonspec.org)
https://orlandos.nl/docs/mongokitten/articles/bson
MIT License
108 stars 36 forks source link

All properties, in the decoded document, are nil. #79

Closed goors closed 1 year ago

goors commented 1 year ago

I have simple struct

public struct Test: Codable {
    public var id: String?
    public var createdAt: Date?
    public var title: String?
}

and json to go with it:

{
   "id": "7e447dd1-cabe-4539-8018-bc6ee5c26213"
   "title":"Some title",
   "createdAt": "2023-06-02 20:43:48"

}
let decoder = BSONDecoder()
if let data = response.data {
    let res = try decoder.decode(Test.self, from: Document(data: data))
    success(res) // this give me all nil for all properties
}

response.data is Alamofire response.

Joannis commented 1 year ago

Hey @goors , I'm not entirely sure what your setup is, but please note that BSON is not JSON. It's a binary format - primarily used for/with MongoDB. While it's certainly an optimisation over JSON in many cases, it's not compatible with JSON.

While BSON does tend to be represented as JSON in many libraries, it does need to be converted by those as well.