项目作者: bluk

项目描述 :
A JSON Feed Swift Codable implementation.
高级语言: Swift
项目地址: git://github.com/bluk/JSONFeed.git
创建时间: 2019-08-27T00:10:13Z
项目社区:https://github.com/bluk/JSONFeed

开源协议:Apache License 2.0

下载


JSON Feed

A Swift Codable model for JSON Feed.

Usage

Swift Package Manager

Add this package to your Package.swift dependencies and target’s dependencies:

  1. import PackageDescription
  2. let package = Package(
  3. name: "Example",
  4. dependencies: [
  5. .package(
  6. url: "https://github.com/bluk/JSONFeed",
  7. from: "0.9.0"
  8. ),
  9. ],
  10. targets: [
  11. .target(
  12. name: "YourProject",
  13. dependencies: ["JSONFeed"]
  14. )
  15. ]
  16. )

Code

  1. import JSONFeed
  2. let jsonFeedData: Data = /* get the JSON feed data */
  3. let jsonDecoder = JSONDecoder()
  4. let feed = try jsonDecoder.decode(JSONFeed.self, from: jsonFeedData)
  5. print(feed.title)

Design Considerations

var vs. let

All of the properties are mutable. The reason is that the JSON Feed model types are merely for
serialization and deserialization of data. There is no business logic, invariants, or
other “interesting” code. If you want to get a feed and then modify the data before passing
the value to your business methods, you are free to do so.

All of the model types are structs and not classes. While perhaps not as efficient
(not benchmarked) in some extreme cases, value types are easier to reason about for these
types of simple data transfer models.

Optional properties

All of the properties are optional. While inconvenient, the reality is that even if
a property is required in a specification, values may be missing in “real” data.
Instead of throwing an error immediately for a missing or invalid required property,
it is up to your application to determine if processing should be stopped.

Raw (String) values

While there are computed properties which return URLs and Dates, the raw string values
are available as a raw(Name) property. In some cases, the feed’s value may be
invalid (e.g. an invalid or uncommon RFC 3339 date) but you can get the original string
value if needed.

RFC 3339

There are several variations for an RFC 3339 date. The current computed Date
properties use one of the more common formats. An issue or code contribution can be
made if other date formats are desired.

JSON Extensions

The JSON Feed Extensions are handled via the use of AnyCodable which is
a type erased wrapper for Codable values. If you have a JSON Feed custom object
extension, it is suggested that you add an extension to JSONFeed (or one of the appropriate
nested types) with a computed property accessing the extensions property.

License

Apache-2.0 License