4

I have a strange problem with decodable. The JSON is just simple like this:

{
    "slug" : "marketplace",
    "taxonomy_id" : 1,
    "taxonomy" : "category",
    "parent_id" : 1,
    "shop_id" : "raw",
    "filter_id" : 1,
    "term_id" : 1,
    "description" : "asd",
    "product_id" : "1",
    "product_name" : "Jual Beli Online",
    "term_group" : 1
}

and here is the struct

struct CategoryMenuSubCategory: Decodable {
    let termId: Int
    let productName: String
    let slug: String
    let termGroup: Int
    let taxonomyId: Int
    let taxonomy: String
    let description: String
    let parentId: Int
    let filterId: Int
    let shopId: String
    let productId: String
    
    enum CodingKeys: String, CodingKey {
        case termId = "term_id"
        case productName = "product_name"
        case slug
        case termGroup = "term_group"
        case taxonomyId = "taxonomy_id"
        case taxonomy
        case description
        case parentId = "parent_id"
        case filterId = "filter_id"
        case shopId = "shop_id"
        case productId = "product_id"
    }
}

when I try to decode it in the playground, it always give me error like this:

valueNotFound(__lldb_expr_119.CategoryMenuSubCategory, Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data did not contain a top-level value.", underlyingError: nil))

and sometimes I got this error:

error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=EXC_I386_GPFLT).
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.

I can get rid of the error just by eliminating 1 of the property (any of it).

Here is the full playground code on code 12.2

import UIKit

let jsonData = """
{
  "slug" : "marketplace",
  "term_taxonomy_id" : 2,
  "taxonomy" : "category",
  "parent" : 363,
  "filter" : "raw",
  "count" : 81,
  "term_id" : 2,
  "description" : "asd",
  "term_order" : "1x",
  "name" : "Jual Beli Online",
  "term_group" : 0
}
""".data(using: .utf8)!

do {
    let decoder = JSONDecoder()
    let asd = try decoder.decode(CategoryMenuSubCategory.self, from: jsonData)
    
    dump(asd)
} catch {
    print(error)
}

internal struct CategoryMenuSubCategory: Decodable {
    internal let termId: Int
    internal let name: String
    internal let slug: String
    internal let termGroup: Int
    internal let termTaxonomyId: Int
    internal let taxonomy: String
    internal let description: String
    internal let parent: Int
    internal let count: Int
    internal let filter: String
    internal let termOrder: String
    
    internal enum CodingKeys: String, CodingKey {
        case termId = "term_id"
        case name
        case slug
        case termGroup = "term_group"
        case termTaxonomyId = "term_taxonomy_id"
        case taxonomy
        case description
        case parent
        case count
        case filter
        case termOrder = "term_order"
    }
}

Can someone explain to me why this happens?

Jefferson Setiawan
  • 536
  • 1
  • 5
  • 22

0 Answers0