JSONTools 1.0.5

JSONTools 1.0.5

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
发布最新版本2015年2月

Greg Combs维护。



JSONTools 1.0.5

  • Greg Combs

JSON 工具 (Objective-C)

Gregory Combs
(MIT 许可证 - 2014)

Objective-C 中的 JSON Patch、JSON Pointer 和 JSON Schema 验证

这个 Objective-C 库是一系列类和分类的集合,实现了三个强大的新功能(JSON Patch、JSON Pointer、JSON Schema),可以处理 JSON 数据(在 Objective-C 中由 NSDictionaries 和 NSArrays 表示)。每个组件都包含单元测试。

运行测试

要构建测试项目,请确保做以下事情:

  1. 如果您还没有,请安装 CocoaPods(使用 Homebrew)。
  2. 从命令行运行 pod install
  3. 打开新创建的 JSONToolsTests.xcworkspace 文档,而不是 JSONToolsTests.xcodeproj 文档。
  4. 按 Command-U 运行测试。

功能

  • JSON Patch - IETF RFC6902:创建并应用操作补丁(添加、删除、复制、移动、测试、_get)以顺序转换 JSON 数据。 此功能受到了 Joachim WesterJavaScript JSON Patch 实现 的启发。

    • 示例补丁复制

          #import "JSONTools.h"
          #import "JSONDeeplyMutable.h"
      
          - (void)examplePatchCopy
          {
               NSMutableDictionary *obj = nil;
               NSMutableDictionary *expected = nil;
               NSDictionary *patch = nil;
      
               obj = [@{@"foo": @1,
                        @"baz": @[@{@"qux": @"hello"}]} copyAsDeeplyMutableJSON];
      
               patch = @{@"op": @"copy",
                         @"from": @"/foo",
                         @"path": @"/bar"};
      
               [JSONPatch applyPatches:@[patch] toCollection:obj];
      
               expected = [@{@"foo": @1,
                             @"baz": @[@{@"qux": @"hello"}],
                             @"bar": @1} copyAsDeeplyMutableJSON];
          }
      
    • 示例补丁生成 (JSON 差分)

          #import "JSONTools.h"
      
          - (void)examplePatchGeneration
          {
              NSDictionary *objA = nil;
              NSDictionary *objB = nil;
              NSArray *patches = nil;
              NSArray *expected = nil;
      
              objA = @{@"user": @{@"firstName": @"Albert",
                                  @"lastName": @"Einstein"}};
      
              objB = @{@"user": @{@"firstName": @"Albert"}};
      
              patches = [JSONPatch createPatchesComparingCollectionsOld:objA toNew:objB];
      
              expected = @[@{@"op": @"remove",
                             @"path": @"/user/lastName"}];
      
          }
      
  • JSON Pointer - IETF RFC6901:使用简洁的路径模式表示法引用和访问层次化 JSON 结构中的值和对象。 此功能基于 Jonathan DringNSDictionary-CWJSONPointer

    • 示例

          #import "JSONTools.h"
      
          - (void)exampleJSONPointer
          {
              NSDictionary *obj = @{
                  @"data": @{
                      @"foo": @[@"bar", @"baz"],
                      @"bork": @{
                          @"crud": @"stuff",
                          @"guts": @"and things"
                      }
                  }
              };
      
              NSString *result1 = [_obj valueForJSONPointer: @"/data/foo/1" ];
              // Yields -> "baz"
      
              NSString *result2 = [_obj valueForJSONPointer: @"/data/bork/guts"];
              // Yields -> "and things"
      
              NSDictionary *result3 = [_obj valueForJSONPointer: @"/data/bork"];
              // Yields -> {"crud": "stuff","guts": "and things"}
          }
      
  • JSON Schema With Validation - IETF 草案 v4,2013。 此功能基于 Sam DukeKiteJSONValidator,但增加了对 JSON-Schema format 参数的额外验证和测试。

    • 示例 #1

          {
             "schema": {
                 "type": "array",
                 "items": { "$ref": "#/definitions/positiveInteger" },
                 "definitions": {
                     "positiveInteger": {
                         "type": "integer",
                         "minimum": 0,
                         "exclusiveMinimum": true
                     }
                 }
             },
             "validData": [0, 1, 2, 3, 4, 5],
             "invalidData": [-12, "Abysmal", null, -141]
          }
          /* 
             Assuming that variables are assigned using JSON above: 
               schema is an NSDictionary
               validData and invalidData are NSArrays
           */
      
          BOOL success = NO;        
          JSONSchemaValidator *validator = [JSONSchemaValidator new];
      
          success = [validator validateJSONInstance:validData withSchema:schema];
          // success == YES, All validData values are positive integers.
      
          success = [validator validateJSONInstance:invalidData withSchema:schema];
          // success == NO, invalidData array isn't comprised of positive integers.
      
    • 示例 #2

          NSDictionary *schema = nil;
          id testData = nil;
          BOOL success = NO;
      
          JSONSchemaValidator *validator = [JSONSchemaValidator new];
          validator.formatValidationEnabled = YES;
      
          schema = @{@"format": @"date-time"};
          testData = @"2000-02-29T08:30:06.283185Z";
          success = [validator validateJSONInstance:testData withSchema:schema];
          // success == YES, February 2000 had 29 days.
      
          schema = @{@"format": @"ipv6"};
          testData = @"12345::";
          success = [validator validateJSONInstance:testData withSchema:schema];
          // success == NO, the IPv6 address has out-of-range values.