项目作者: glimpseio

项目描述 :
Bric-à-brac: JSON Schema for Swift on Linux, Windows, and Apple Platforms
高级语言: Swift
项目地址: git://github.com/glimpseio/BricBrac.git
创建时间: 2015-09-14T17:49:56Z
项目社区:https://github.com/glimpseio/BricBrac

开源协议:MIT License

下载


Universal

Build Status
Swift Package Manager compatible
Platform

Universal: A tiny zero-dependency cross-platform Swift parser and decoder for JSON, XML, YAML, and property lists.

Usage:

Add the following dependency to your Package.swift:

  1. .package(url: "https://github.com/marcprux/universal.git", from: "5.0.5")

The package provides the modules Either, JSON, XML, YAML, PLIST,
or Universal, which is an umbrella module that re-exports all the other modules.

Example:

  1. import Universal
  2. func testUniversalExample() throws {
  3. // JSON Parsing
  4. let json: JSON = try JSON.parse(Data("""
  5. {"parent": {"child": 1}}
  6. """.utf8))
  7. assert(json["parent"]?["child"] == 1)
  8. assert(json["parent"]?["child"] == JSON.number(1.0)) // JSON's only number is Double
  9. // YAML Parsing
  10. let yaml: YAML = try YAML.parse(Data("""
  11. parent:
  12. child: 1
  13. """.utf8))
  14. assert(yaml["parent"]?["child"] == 1)
  15. assert(yaml["parent"]?["child"] == YAML.integer(1)) // YAML can parse integers
  16. assert(yaml["parent"]?["child"] != 1.0) // not the same as a double
  17. let yamlJSON: JSON = try yaml.json() // convert YAML to JSON struct
  18. assert(yamlJSON == json)
  19. // XML Parsing
  20. let xml: XML = try XML.parse(Data("""
  21. <parent><child>1</child></parent>
  22. """.utf8))
  23. let xmlJSON: JSON = try xml.json() // convert XML to JSON struct
  24. assert(xml["parent"]?["child"] == XML.string("1")) // XML parses everything as strings
  25. // fixup the XML by changing the JSON to match
  26. assert(json["parent"]?["child"] == 1)
  27. var jsonEdit = json
  28. jsonEdit["parent"]?["child"] = JSON.string("1") // update the JSON to match
  29. assert(jsonEdit["parent"]?["child"] == "1") // now the JSON matches
  30. assert(xmlJSON == jsonEdit)
  31. }

Coding

Universal provides the ability to decode from (but not encode to) YAML and XML
through their ability to convert to a JSON struct:

  1. import Universal
  2. struct Coded : Decodable, Equatable {
  3. let person: Person
  4. struct Person : Decodable, Equatable {
  5. let firstName: String
  6. let lastName: String
  7. let astrologicalSign: String
  8. }
  9. }
  10. let decodedFromJSON = try Coded(json: JSON.parse(Data("""
  11. {
  12. "person": {
  13. "firstName": "Marc",
  14. "lastName": "Prud'hommeaux",
  15. "astrologicalSign": "Sagittarius"
  16. }
  17. }
  18. """.utf8)))
  19. let decodedFromYAML = try Coded(json: YAML.parse(Data("""
  20. # A YAML version of a Person
  21. person:
  22. firstName: Marc
  23. lastName: Prud'hommeaux
  24. astrologicalSign: Sagittarius # what's your sign?
  25. """.utf8)).json())
  26. assert(decodedFromJSON == decodedFromYAML)
  27. let decodedFromXML = try Coded(json: XML.parse(Data("""
  28. <!-- An XML version of a Person -->
  29. <person>
  30. <firstName>Marc</firstName>
  31. <!-- escaping and stuff -->
  32. <lastName>Prud'hommeaux</lastName>
  33. <astrologicalSign>Sagittarius</astrologicalSign>
  34. </person>
  35. """.utf8)).json())
  36. assert(decodedFromYAML == decodedFromXML)
  37. let decodedFromPLISTXML = try Coded(json: PLIST.parse(Data("""
  38. <?xml version="1.0" encoding="UTF-8"?>
  39. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  40. <plist version="1.0">
  41. <dict>
  42. <key>person</key>
  43. <dict>
  44. <key>firstName</key>
  45. <string>Marc</string>
  46. <key>lastName</key>
  47. <string>Prud'hommeaux</string>
  48. <key>astrologicalSign</key>
  49. <string>Sagittarius</string>
  50. </dict>
  51. </dict>
  52. </plist>
  53. """.utf8)).json())
  54. assert(decodedFromXML == decodedFromPLISTXML)
  55. let decodedFromPLISTOpenStep = try Coded(json: PLIST.parse(Data("""
  56. {
  57. person = {
  58. firstName = Marc;
  59. lastName = "Prud'hommeaux";
  60. astrologicalSign = Sagittarius;
  61. };
  62. }
  63. """.utf8)).json())
  64. assert(decodedFromPLISTOpenStep == decodedFromPLISTXML)