All Projects → dejanskledar → Serializedswift

dejanskledar / Serializedswift

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

Programming Languages

swift
15916 projects

Labels

Projects that are alternatives of or similar to Serializedswift

Microblob
Serve millions of JSON documents via HTTP.
Stars: ✭ 45 (-13.46%)
Mutual labels:  json
Ansible Config encoder filters
Ansible role used to deliver the Config Encoder Filters.
Stars: ✭ 48 (-7.69%)
Mutual labels:  json
Flask Restx
Fork of Flask-RESTPlus: Fully featured framework for fast, easy and documented API development with Flask
Stars: ✭ 1,050 (+1919.23%)
Mutual labels:  json
Zeison
Small, fast and easy-to-use JSON library for Scala.
Stars: ✭ 45 (-13.46%)
Mutual labels:  json
Univalue
High performance RAII C++ JSON library and universal value object class
Stars: ✭ 46 (-11.54%)
Mutual labels:  json
Minijson
A lightweight json library (C++)
Stars: ✭ 49 (-5.77%)
Mutual labels:  json
Wheel
关于net nio os cache db rpc json web http udp tcp mq 等多个小工具的自定义实现
Stars: ✭ 45 (-13.46%)
Mutual labels:  json
Templateplugin
A template for creating minecraft plugin from 1.7.10 to 1.16.2
Stars: ✭ 51 (-1.92%)
Mutual labels:  json
Shon
A simple tool to convert json or yaml into a shell-compliant data structure.
Stars: ✭ 47 (-9.62%)
Mutual labels:  json
Jsonapi parameters
Rails-way to consume JSON:API input
Stars: ✭ 50 (-3.85%)
Mutual labels:  json
Jj
JSON Stream Editor (command line utility)
Stars: ✭ 1,033 (+1886.54%)
Mutual labels:  json
Webpub Manifest
📜 A JSON based Web Publication Manifest format used at the core of the Readium project
Stars: ✭ 46 (-11.54%)
Mutual labels:  json
Generator Http Fake Backend
Yeoman generator for building a fake backend by providing the content of JSON files or JavaScript objects through configurable routes.
Stars: ✭ 49 (-5.77%)
Mutual labels:  json
Fiscalberry
[JSON ↔ HW] Connect things using JSON API with the fiscalberry websocket server interact easily with any kind of Hardware. Another IoT solution...
Stars: ✭ 44 (-15.38%)
Mutual labels:  json
Tech1 Benchmarks
Java JMH Benchmarks repository. No Longer Supported.
Stars: ✭ 50 (-3.85%)
Mutual labels:  json
Json Normalizer
📃 Provides generic and vendor-specific normalizers for normalizing JSON documents.
Stars: ✭ 45 (-13.46%)
Mutual labels:  json
Resticprofile
Configuration profiles for restic backup
Stars: ✭ 48 (-7.69%)
Mutual labels:  json
Schemas
All schemas used for validation that are shared between our projects
Stars: ✭ 51 (-1.92%)
Mutual labels:  json
Cucumber Api
API validator in BBD style with Cucumber
Stars: ✭ 50 (-3.85%)
Mutual labels:  json
Decodable
[Probably deprecated] Swift 2/3 JSON unmarshalling done (more) right
Stars: ✭ 1,050 (+1919.23%)
Mutual labels:  json

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].