Giter Site home page Giter Site logo

paulcote / serializedswift Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dejanskledar/serializedswift

0.0 0.0 0.0 53 KB

A significant enhancement to the current Codable protocol for better and easier Serializing and Deserializing of JSON

License: MIT License

Swift 100.00%

serializedswift's Introduction

SerializedSwift

Swift Package Manager CocoaPods Platforms

A GSON inspired JSON decoding strategy in Swift using @propertyWrappers.

Features:

  • No need to write own init(from decoder: Decoder)
  • No need to write own CodingKeys subclass
  • Works with inheritance and composition out of the box
  • Custom Transformer classes
  • Alternative coding keys
  • Default values if JSON key is missing
struct Foo: Serializable {
    @Serialized
    var bar: String
    
    @Serialized("globalId")
    var id: String?
    
    @Serialized(alternateKey: "mobileNumber")
    var phoneNumber: String?
    
    @Serialized(default: 0)
    var score: Int
}

Installation

Codoapods

Add this line to your Podfile:

pod 'SerializedSwift'

Swift Package manager

If you are using SPM for your dependency manager, add this to the dependencies in your Package.swift file:

dependencies: [
    .package(url: "https://github.com/dejanskledar/SerializedSwift.git")
]

Requirements

  • iOS 10.0+ / macOS 10.12+ / tvOS 10.0+ / watchOS 3.0+
  • Xcode 11+
  • Swift 5.1+

Usage

class User: Serializable {

    @Serialized
    var name: String
    
    @Serialized("globalId")
    var id: String?
    
    @Serialized(alternateKey: "mobileNumber")
    var phoneNumber: String?
    
    @Serialized(default: 0)
    var score: Int
    
    required init() {}
}

Works with inheritance

No additional decoding logic needed

class PowerUser: User {
    @Serialized
    var powerName: String?

    @Serialized(default: 0)
    var credit: Int
}

Works with composition

No additional decoding logic needed

class ChatRoom: Serializable {
    @Serialized
    var admin: PowerUser?

    @Serialized(default: [])
    var users: [User]
}

Custom transformer classes

You can create own custom Transformable classes, for custom transformation logic.

class DateTransformer: Transformable {
    static func transformFromJSON(from value: String?) -> Date? {
        let formatter = DateFormatter()
        return formatter.date(from: value ?? "")
    }
    
    static func transformToJson(from value: Date?) -> String? {
        let formatter = DateFormatter()
        return formatter.string(from: value ?? Date())
    }
}

struct User: Serializable {
    @SerializedTransformable<DateTransformer>
    var birthDate: Date?
}

Features

Serializable

  • typealias-ed from SerializableEncodable & SerializableDecodable
  • Custom decoding and encoding using propertyWrappers (listed below)
  • Use this protocol for your classes and structures in the combination with the property wrappers below

Serialized

  • Serialization propertyWrapper for all properties, optional and non-optionals!
  • Custom decoding Key
  • By default using the propertyName as a Decoding Key
  • Alternative Decoding Key support
  • Optional Default value (if the key is missing). By default, the Default value is nil. For non-optionals, default value is recommended, to avoid crashes
@Serialized("mainKey", alternativeKey: "backupKey", default: "")
var key: String?

SerializedTransformable

  • Custom transforming property wrapper
  • Create own Transformable classes
  • Transforming Decodable objects to own types, like custom String Date format to native Date
 class DateTransformer: Transformable {
     static func transformFromJSON(from value: String?) -> Date? {
         let formatter = DateFormatter()
         return formatter.date(from: value ?? "")
     }
     
     static func transformToJson(from value: Date?) -> String? {
         let formatter = DateFormatter()
         return formatter.string(from: value ?? "")
     }
 }

 // Usage of `SerializedTransformable`
 struct User: Serializable {
     @SerializedTransformable<DateTransformer>
     var birthDate: Date?
 } 

Contribute

This is only a tip of the iceberg of what can one achieve using Property Wrappers and how we can improve Decoding and Encoding JSON in Swift. Feel free to colaborate.

serializedswift's People

Contributors

dejanskledar avatar malted avatar

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.