Giter Site home page Giter Site logo

45qq / gloss Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hkellaway/gloss

0.0 0.0 0.0 1.02 MB

[Deprecated] A shiny JSON parsing library in Swift :sparkles: Loved by many from 2015-2021

Home Page: https://hkellaway.github.io/blog/2020/08/30/tale-of-third-parties

License: MIT License

Ruby 0.36% Objective-C 0.21% Swift 99.43%

gloss's Introduction

Gloss

๐Ÿšจ Deprecation Notice ๐Ÿšจ

Gloss has been deprecated in favor of Swift's Codable framework.

The existing Gloss source is not going away, however updates will only be made to support migration to Codable. Read the MIGRATION GUIDE now to get started.

If you do not yet have any Gloss models in your project yet are considering it for JSON parsing, turn around now! Select Swift's Codable framework instead.

I understand, I'm Using Gloss Anyway

Swift version CocoaPods Carthage compatible SPM CocoaPods Build Status

See the former README.md on instructions for using Gloss pre-Codable migration.

Credits

Gloss was created by Harlan Kellaway

Thank you to all contributors and the Swift community for 5 years of Gloss! ๐Ÿ’–

License License

See the LICENSE file for more info.

Codable Migration Quick Reference

The following is a reference for what your Gloss models and call-sites should look like after preparing to migrate to Codable.

See the MIGRATION GUIDE for more detail.

Version

Use version 3.2.0 or higher to take advantage of migration helpers.

Deserialization

Given a Gloss model that conforms to JSONDecodable, add conformance to Decodable. A model that looks like this:

import Gloss

struct MyModel: JSONDecodable {
    let id: Int?
    
    init?(json: JSON) {
        self.id = "id" <~~ json
    }
}

adds

extension MyModel: Decodable {

    init(from decoder: Swift.Decoder) throws {
        // Proper Decodable definition or throw GlossError.decodableMigrationUnimplemented
        // Remove this method if Codable can synthesize decoding for you
    }

}

Initializing a Model from JSON

Where initializing that model currently looks like:

let myModel = MyModel(json: someJSON)

it becomes:

let myModel: MyModel = .from(decodableJSON: someJSON)

Serialization

Given a Gloss model that conforms to JSONEncodable, add conformance to Encodable. A model that looks like this:

import Gloss

struct MyModel: JSONEncodable {
    let id: Int?
    
    func toJSON() -> JSON? {
        return jsonify(["id" ~~> self.id])
    }
}

adds

extension MyModel: Encodable {

    func encode(to encoder: Swift.Encoder) throws {
        // Proper Encodable defintion or throw GlossError.encodableMigrationUnimplemented
        // Remove this method if Codable can synthesize encoding for you
    }

}

Translating Model Objects to JSON

Where translating to JSON currently looks like this:

let json: JSON? = myModel.toJSON()

it becomes:

let json: JSON? = myModel.toEncodableJSON()

JSON Arrays

Similar usage applies to arrays of Decodable and Encodable models, with from(decodableJSONArray:) and toEncodableJSONArray() respectively.

Configuring JSONDecoder and JSONEncoder

If your Codable definitions are sound but you're encountering Codable errors, make sure your JSONDecoder or JSONEncoder instances are configured properly and pass them at call-sites:

let mySharedJSONDecoder: JSONDecoder = ...
let myModel: MyModel = .from(decodableJSON: someJSON, jsonDecoder: mySharedJSONDecoder)
let mySharedJSONEncoder: JSONEncoder = ...
let json: JSON? = myModel.toEncodableJSON(jsonEncoder: mySharedJSONEncoder)

Using Data Instead of JSON to Create Models

In the places where you've come to rely on Gloss's JSON type, you'll eventually need to pass Data, as that is what Codable uses. To get a jump using decode(:), one option is use the same method Gloss uses to do Data transformation:

import Gloss

let sharedGlossSerializer: GlossJSONSerializer = ...
let json: JSON = ...
if let data: Data? = sharedGlossSerializer.data(from: json, options: nil) {
    let myModel: MyModel = try? myJSONDecoder.decode(MyModel.self, from : data)
    ...
}

Take the opportunity with this migration to pare your models down to the slim amount of code Codable needs to work its magic and detangle your networking code from the details of JSON serialization. Future you will be grateful! ๐Ÿ”ฎ

โœจโœจโœจEOFโœจโœจโœจ

gloss's People

Contributors

hkellaway avatar bojan avatar ejmartin504 avatar morganchen12 avatar rahul0x24 avatar rbukovansky avatar hcanzonetta avatar andrewjmeier avatar bachino90 avatar jkandzi avatar fabb avatar kampro avatar 4ch7ung avatar vkotovv avatar urosk avatar ryanschneider avatar karpelcevs avatar readmecritic avatar orta avatar maximveksler avatar mvdevries avatar jlmendezbonini avatar gerbiljames avatar nuudles avatar wanderingstar avatar andrewsb avatar askielboe 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.