0

I am using a GOlang library with mobile bindings. One of the models from GO library takes a data as a parameter. If should be utf8 encoded JSON string. When I am encoding that JSON in objc everything works fine but I cannot instantiate that model with JSON encoded in swift. What can cause that?

here is objc code:

NSString *invitation = @"{ \"@id\": \"242304ff-7028-4991-b56e-842a85f345dd\", \"recipientKeys\": [ \"EXrC5FVVBVxRf2DZ8g638d5BA9fzNQNbLFQFXn8nzQHL\" ], \"serviceEndpoint\": \"ws://aws.xsx.com:8002\"}";
NSData *data = [invitation dataUsingEncoding:NSUTF8StringEncoding];

I've tried this in swift but doesn't work:

let data = """
{
    "serviceEndpoint": "ws://aws.xsx.com:8002",
    "recipientKeys": [
        "2VPFQH6kBRJbtve3xmpVbLbh7YqfiB9gE8mHr8rbmznS"
    ],
    "@id": "503509ff-4014-41f6-a43b-6eb6e7d5141d"
}
""".data(using: .utf8)

and I've also tried to encode that JSON using JSONEncoder

let data = try! JSONEncoder().encode(invitation) where invitation is Codable struct:

struct Invitation: Codable {
    enum CodingKeys: String, CodingKey {
        case serviceEndpoint = "serviceEndpoint"
        case recipientKeys = "recipientKeys"
        case id = "@id"
    }
    let serviceEndpoint: String
    let recipientKeys: [String]
    let id: String
}

I am always getting that error:

invalid character '\x00' looking for beginning of value

Witek Bobrowski
  • 3,749
  • 1
  • 20
  • 34
barola_mes
  • 1,532
  • 2
  • 13
  • 16

0 Answers0