skelpo / CSV

A simple CSV file parser and serializer

Home Page:http://www.skelpo.codes/CSV/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Encoder doesn't escape cell separator

nickasd opened this issue · comments

Take this code:

struct Asd: Codable {
    var asd: String
    var zuio = 0
}

let config = Config(cellSeparator: UInt8(ascii: ","), cellDelimiter: nil)
let encoder = CSVEncoder(configuration: config).sync
let data = try! encoder.encode([Asd(asd: "asd,fgh"), Asd(asd: "bla")])
let string = String(decoding: data, as: UTF8.self)
print(string)
let decoder = CSVDecoder(configuration: config).sync
do {
    let object = try decoder.decode(Asd.self, from: data)
    print(object)
} catch {
    print(error)
}

It fails with error:

"Cannot convert bytes `[102, 103, 104]` to type `Int`

When removing var zuio = 0 from the Asd struct, the encoding doesn't throw any error, but when decoded again, it results in 3 rows intead of 2:

[Asd(asd: "asd"), Asd(asd: "fgh"), Asd(asd: "bla")]

Only when removing the comma in the string passed to the Asd constructor everything goes well.