Git Product home page Git Product logo

Comments (4)

faimin avatar faimin commented on August 17, 2024 1

@dgrzeszczak amazing, It fulfills my needs, thanks.

from keyedcodable.

dgrzeszczak avatar dgrzeszczak commented on August 17, 2024

Hey, do you have any issue I can replicate ?

from keyedcodable.

faimin avatar faimin commented on August 17, 2024

yes, keyedCoable seem to don't support inheritance of class

demo code

class SuperClass {
    var name: String?
    var sex: String?
    var age: Int?
}

class SubClass: SuperClass, Codable {
    var a: String?
    var b: Int?
    
    enum CodingKeys: String, KeyedKey {
        case a, b
    }
}

private let jsonString = """
{
    "name": "John",
    "age": 18,
    "sex": "F",
    "a": "aa",
    "b": 100,
    "qqqq": {
        ".greeting": "Hallo world",
        "details": {
            "description": "Its nice here"
        }
    },
    "longitude": 3.2,
    "latitude": 3.4
}
"""

func testCodable() throws {
        let jsonData = jsonString.data(using: .utf8)!
        
        let model = try SubClass.keyed.fromJSON(jsonData)
        
        print(model) 
}

result

(CodableTests.SubClass) model = 0x00006000003a4180 {
  CodableTests.SuperClass = {
    name = nil
    sex = nil
    age = nil
  }
  a = "aa"
  b = 100
}

iShot2021-03-02 10.15.00.png

from keyedcodable.

dgrzeszczak avatar dgrzeszczak commented on August 17, 2024

Thank you for detailed description. I'll look on that but it may be hard to support - it's known issue of Codables and it's not related with KeyedCodables.

What do you think to use composition instead of inheritance? Using @Flat you will use the same JSON format. Your example will look like that.

class SuperClass: Codable {
    var name: String?
    var sex: String?
    var age: Int?
}

class SubClass: Codable {
    var a: String?
    var b: Int?

    @Flat var c: SuperClass // may be optional as well @Flat var c: SuperClass?
}

from keyedcodable.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.