项目作者: ra1028

项目描述 :
:alembic: Functional JSON Parser - Linux Ready :penguin:
高级语言: Swift
项目地址: git://github.com/ra1028/Alembic.git
创建时间: 2016-04-02T17:41:26Z
项目社区:https://github.com/ra1028/Alembic

开源协议:MIT License

下载



Alembic


Functional JSON Parser






Swift4
Build Status
CodeBeat






CocoaPods
Carthage
Swift Package Manager






Platform
Lincense


Feature

  • Linux Ready
  • Type-safe JSON parsing
  • Functional value transformation
  • Easy to parse nested value
  • Dependency free
  • No defined custom operators

Requirements

  • Swift4.1 or later
  • OS X 10.9 or later
  • iOS 9.0 or later
  • watchOS 2.0 or later
  • tvOS 9.0 or later
  • Linux

Installation

CocoaPods

Add the following to your Podfile:

  1. use_frameworks!
  2. target 'TargetName' do
  3. pod 'Alembic'
  4. end

Carthage

Add the following to your Cartfile:

  1. github "ra1028/Alembic"

Swift Package Manager

Add the following to your Package.swift:

  1. // swift-tools-version:4.0
  2. let package = Package(
  3. name: "ProjectName",
  4. dependencies : [
  5. .package(url: "https://github.com/ra1028/Alembic.git", .upToNextMajor(from: "3"))
  6. ]
  7. )

Example

In example, parse the following JSON:

  1. {
  2. "teams": [
  3. {
  4. "name": "Team ra1028",
  5. "url": "https://github.com/ra1028",
  6. "members": [
  7. {
  8. "name": "Ryo Aoyama",
  9. "age": 23
  10. },
  11. {
  12. "name": "John Doe",
  13. "age": 30
  14. }
  15. ]
  16. }
  17. ]
  18. }

Make the JSON instance from Any, Data or String type JSON object.

  1. // from `Any` type JSON object
  2. let json = JSON(object)
  1. // from JSON Data
  2. let json = try JSON(data: data)
  1. // from JSON String
  2. let json = try JSON(string: string)

Parse value from JSON:

Parse the values type-safely

  1. let memberName: String = try json.value(for: ["teams", 0, "members", 0, "name"])

Parse nullable value

  1. let missingText: String? = try json.option(for: "missingKey")

Parse value from JSON with transforming:

Transform value using various monadic functions.

  1. let teamUrl: URL = try json.parse(String.self, for: ["teams", 0, "url"])
  2. .filterMap(URL.init(string:))
  3. .value()

Transform nullable value if exist

  1. let missingUrl: URL? = try json.parse(String.self, for: "missingKey")
  2. .filterMap(URL.init(string:))
  3. .option()

Mapping to model from JSON:

All types conforming to Parsable protocol and it’s Array, Dictionary can be parsed.

  1. struct Member: Parsable {
  2. let name: String
  3. let age: Int
  4. static func value(from json: JSON) throws -> Member {
  5. return try .init(
  6. name: json.value(for: "name"),
  7. age: json.value(for: "age")
  8. )
  9. }
  10. }
  11. extension URL: Parsable {
  12. public static func value(from json: JSON) throws -> URL {
  13. guard let url = try URL(string: json.value()) else {
  14. throw JSON.Error.dataCorrupted(value: json.rawValue, description: "The value was not valid url string.")
  15. }
  16. return url
  17. }
  18. }
  19. struct Team: Parsable {
  20. let name: String
  21. let url: URL
  22. let members: [Member]
  23. static func value(from json: JSON) throws -> Team {
  24. return try .init(
  25. name: json.value(for: "name"),
  26. url: json.value(for: "url"),
  27. members: json.value(for: "members")
  28. )
  29. }
  30. }
  1. let team: Team = try json.value(for: ["teams", 0])

Tips

The types conformed to Parsable as default.

  1. JSON
  2. String
  3. Int
  4. UInt
  5. Double
  6. Float
  7. Bool
  8. NSNumber
  9. Int8
  10. UInt8
  11. Int16
  12. UInt16
  13. Int32
  14. UInt32
  15. Int64
  16. UInt64
  17. Decimal
  18. Array where Element: Parsable
  19. Dictionary where Key == String, Value: Parsable
  20. Optional where Wrapped: Parsable

Conform to Parsable with initializer

  1. struct Member: ParseInitializable {
  2. let name: String
  3. let age: Int
  4. init(with json: JSON) throws {
  5. name = try json.value(for: "name")
  6. age = try json.value(for: "age")
  7. }
  8. }

Usage Reference Files

Functional operators for value transforming:

Errors

More Example

See the Test files


Playground

  1. Open Alembic.xcworkspace.
  2. Build the Alembic for Mac.
  3. Open Alembic playground in project navigator.

Contribution

Welcome to fork and submit pull requests!

Before submitting pull request, please ensure you have passed the included tests.
If your pull request including new function, please write test cases for it.


License

Alembic is released under the MIT License.