JSONSchemaValidation:用Objective-C编写的JSON Schema草案4、6和7解析和验证库

  • K6_970082
    了解作者
  • 205.9KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-03 21:04
    上传日期
DSJSONSchemaValidation 用Objective-C编写的JSON模式草案4,草案6和草案7解析和验证库。 DSJSONSchemaValidation是一个库,提供了一组用于将文档解析为本机Objective-C对象并随后使用它们来验证JSON文档的类。 该库的主要功能是能够将架构“编译”为描述该架构的对象网络,以便可以将其缓存和重用,以一种NSRegularExpression的方式验证多个JSON文档,类似于NSRegularExpression和NSDateFormatter类。 该库的可能用例之一可能是基于应用程序中以JSON Schema形式描述的期望,对从
JSONSchemaValidation-master.zip
内容介绍
# DSJSONSchemaValidation **JSON Schema draft 4, draft 6 and draft 7 parsing and validation library written in Objective-C.** [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) [![CocoaPods](https://img.shields.io/cocoapods/v/DSJSONSchemaValidation.svg?maxAge=604800)]() [![CocoaPods](https://img.shields.io/cocoapods/p/DSJSONSchemaValidation.svg?maxAge=2592000)]() [![CocoaPods](https://img.shields.io/cocoapods/l/DSJSONSchemaValidation.svg?maxAge=2592000)]() `DSJSONSchemaValidation` is a library that provides a set of classes for parsing [JSON Schema](http://json-schema.org/documentation.html) documents into native Objective-C objects and subsequently using them to validate JSON documents. The main feature of the library is an ability to "compile" the schema into a network of objects that describe that schema, so that it could be cached and reused for validation of multiple JSON documents in a performant manner, similar to the way `NSRegularExpression` and `NSDateFormatter` classes are used. One of the possible use cases of this library could be early validation of JSON response received from a web service, based on expectations described within the app in a form of JSON Schema. `DSJSONSchemaValidation` supports all validation keywords of JSON Schema draft 4, 6 and 7. It is also possible to extend the functionality of the library by defining custom keywords to be used with specific metaschema URIs and custom formats for the `format` validation keyword. Note that JSON Schema draft 3 is not supported at the moment. There are also a few important limitations, including usage of external schema references, listed under [Caveats and limitations](#caveats-and-limitations). Based on https://github.com/vlas-voloshin/JSONSchemaValidation ## Requirements `DSJSONSchemaValidation` currently supports building in Xcode 7.0 or later with ARC enabled. Minimum supported target platform versions are iOS 7.0, tvOS 9.0 and OS X 10.9. Library can be linked to Objective-C and Swift targets. ## Installation ### Carthage 1. Add the following line to your `Cartfile`: ``` github "dashevo/JSONSchemaValidation" ``` 2. Follow the instructions outlined in [Carthage documentation](https://github.com/Carthage/Carthage/blob/master/README.md) to build and integrate the library into your app. 3. Import library header in your source files: * Objective-C: `#import <DSJSONSchemaValidation/DSJSONSchemaValidation.h>` * Swift: `import DSJSONSchemaValidation` ### CocoaPods 1. Add the following line to your `Podfile`: ``` pod 'DSJSONSchemaValidation' ``` 2. Import library header in your source files: * Objective-C: `#import <DSJSONSchemaValidation/DSJSONSchema.h>` * Swift: `import DSJSONSchemaValidation` ### Framework (iOS 8.0+, tvOS and OS X) 1. Download and copy the repository source files into your project, or add it as a submodule to your git repository. 2. Drag&drop `DSJSONSchemaValidation.xcodeproj` into your project or workspace in Xcode. 3. In "General" tab of Project Settings → `Your Target`, you might find that Xcode has added a missing framework item in "Embedded Binaries". Delete it for now. 4. Still in "General" tab, add `DSJSONSchemaValidation.framework` from `DSJSONSchemaValidation-iOS`, `DSJSONSchemaValidation-tvOS` or `DSJSONSchemaValidation-OSX` target (depending on your target platform) to "Embedded Binaries". This should also add it to "Linked Frameworks and Libraries". 5. Import library header in your source files: * Objective-C: `#import <DSJSONSchemaValidation/DSJSONSchemaValidation.h>` * Swift: `import DSJSONSchemaValidation` ### Static library (iOS) 1. Download and copy the repository source files into your project, or add it as a submodule to your git repository. 2. Drag&drop `DSJSONSchemaValidation.xcodeproj` into your project or workspace in Xcode. 3. In "General" section of Project Settings → `Your Target`, you might find that Xcode has added a missing framework item in "Embedded Binaries". Delete it for now. 4. Still in "General" tab, add `libDSJSONSchemaValidation.a` to "Linked Frameworks and Libraries". 5. Add project path to `Your Target` → Build Settings → Header Search Paths (e.g. `"$(SRCROOT)/MyAwesomeProject/Vendor/DSJSONSchemaValidation/"`). 6. Add `-ObjC` flag to `Your Target` → Build Settings → Other Linker Flags to ensure that categories defined in the static library are loaded. 7. Import library header in your source files: * Objective-C: `#import <DSJSONSchemaValidation/DSJSONSchema.h>` * Swift: `import DSJSONSchemaValidation` ### Source files 1. Download and copy the repository source files into your project, or add it as a submodule to your git repository. 2. Add the contents of `DSJSONSchemaValidation` directory into your project in Xcode. 3. Import library header: `#import "DSJSONSchema.h"`. ## Usage After importing the library header/module, use `DSJSONSchema` class to construct schema objects from `NSData` instances: ``` objective-c NSData *schemaData = [NSData dataWithContentsOfURL:mySchemaURL]; NSError *error = nil; DSJSONSchema *schema = [DSJSONSchema schemaWithData:schemaData baseURI:nil referenceStorage:nil specification:[DSJSONSchemaSpecification draft4] error:&error]; ``` ``` swift if let schemaData = NSData(contentsOfURL: mySchemaURL) { let schema = try? DSJSONSchema(data: schemaData, baseURI: nil, referenceStorage: nil, specification:DSJSONSchemaSpecification.draft4()) } ``` or from parsed JSON instances: ``` objective-c NSData *schemaData = [NSData dataWithContentsOfURL:mySchemaURL]; // note that this object might be not an NSDictionary if schema JSON is invalid NSDictionary *schemaJSON = [NSJSONSerialization JSONObjectWithData:schemaData options:0 error:NULL]; NSError *error = nil; DSJSONSchema *schema = [DSJSONSchema schemaWithObject:schemaJSON baseURI:nil referenceStorage:nil specification:[DSJSONSchemaSpecification draft4] error:&error]; ``` ``` swift if let schemaData = NSData(contentsOfURL: mySchemaURL), schemaJSON = try? NSJSONSerialization.JSONObjectWithData(schemaData, options: [ ]), schemaDictionary = schemaJSON as? [String : AnyObject] { let schema = try? DSJSONSchema(object: schemaDictionary, baseURI: nil, referenceStorage: nil, specification: DSJSONSchemaSpecification.draft4()) } ``` Optional `baseURI` parameter specifies the base scope resolution URI of the constructed schema. Default scope resolution URI is empty. Optional `referenceStorage` parameter specifies a `DSJSONSchemaStorage` object that should contain "remote" schemas referenced in the instantiated schema. See [Schema storage and external references](#schema-storage-and-external-references) for more details. After constructing a schema object, you can use it to validate JSON instances. Again, these instances could be provided either as `NSData` objects: ``` objective-c NSData *jsonData = [NSData dataWithContentsOfURL:myJSONURL]; NSError *validationError = nil; BOOL success = [schema validateObjectWithData:jsonData error:&validationError]; ``` ``` swift if let jsonData = NSData(contentsOfURL: myJSONURL) { do { try schema.validateObjectWithData(jsonData) // Success } catch let validationError as NSError { // Failure } } ``` or parsed JSON instances: ``` objective-c NSData *jsonData = [NSData dataWithContentsOfURL:myJSONURL]; id json = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:NULL]; NSError *validationError = nil; BOOL success = [schema validateObject:json error:&validationError]; ``` ``` swift if let jsonData = NSData(contentsOfURL: myJSONURL), json = try? NSJSONSerialization.JSONObjectWithData(jsonData, options: [ ]) { do { try schema.validateObject(json) // Success } catch let validationError as NSError { // Failure } } ``` In case of a validation failure, the `NSError` object will contain the
评论
    相关推荐
    • 计算机架构
      计算机架构 该项目包括对各种类型的分支预测变量的分析。 有关详细信息,从启动可执行文件 所有图像均由Alpa D Desai定制,或包括...高速缓存存储器 计算机架构 计算机体系结构硬盘存储 兴趣 高级知识参考: : : 和
    • 课程介绍以及高并发高可用复杂系统中的缓存架构有哪些东西?.rar
      01_课程介绍以及高并发高可用复杂系统中的缓存架构有哪些东西01_课程介绍以及高并发高可用复杂系统中的缓存架构有哪些东西01_课程介绍以及高并发高可用复杂系统中的缓存架构有哪些东西01_课程介绍以及高并发高可用...
    • web缓存
      web缓存
    • 亿级流量电商详情页系统的大型高并发与高可用缓存架构实战-未加密
      亿级流量电商详情页系统的大型高并发与高可用缓存架构实战-未加密
    • 蜜蜂缓存:支持多服务架构下,使用本地缓存
      多服务架构下,依旧使用本地缓存 减少第三方缓存服务的压力 避免网路I / O和资料结构转换的消耗,提高效能 兼容Spring Cacheable 设计 由于缓存通常应用在不重复异动的资料上,所以不应该每次都向向第三方缓存服务...
    • EF架构
      对数据层进行了封装对数据层进行了封装对数据层进行了封装对数据层进行了封装
    • contacts_prototype:基于新架构的Contacts App原型
      渲染缓存 西南 此外,我们正在支持有关新体系结构的其他建议: “固定”网络。 当前,我们有一个应用程序版本,可让我们“固定”“联系人详细信息”视图,而无需依赖“联系人列表”(这真的很酷!)。 您可以在[1]...
    • RocketData:不变模型的缓存和一致性解决方案
      在大多数设置中,后备缓存不需要架构,并且您无需添加迁移逻辑。 规模 Rocket Data可以很好地扩展到大量模型和数据提供者。 由于它几乎在后台线程上完成所有工作,因此您无需担心一项更改会使整个应用程序变慢。 您...
    • VereCached至简缓存架构
      3.启动缓存服务 Window 下 : startup.bat Linux 下 : startup.sh 4.打开 Eclipse 新建一个项目 test 然后新建测试类 com.test.cache.Test package com.test.cache; import com.vere.cache.client.CacheClient;...
    • osCache,缓存
      osCache,缓存精典实例,希望对java开发人员对帮助!