Giter Club home page Giter Club logo

verijson's Introduction

#clone from: https://bitbucket.org/dcutting/verijson

to-do list

  1. 支持异步解析
  2. 支持友好的出错提示
  3. 在数组的情况下,支持脏数据item跳过
  4. 支持数据清洗
  5. 支持错误模式文件的验证,提取错误信息

VeriJSON

An Objective-C library for verifying JSON against a pattern-based schema.

VeriJSON is designed to verify that the JSON you've received from a web service has the structure you expect before you try to extract information from it.

Patterns

Michael Sprindzuikate suggested the pattern approach for VeriJSON.

A pattern describes the expected content of a JSON document. Patterns are themselves valid JSON, and mirror the structure of a matching JSON document by including property names, arrays and objects. Instead of values, a pattern specifies the required type of values.

Allowed types are "number", "string" and "bool". Arrays and objects are represented structurally.

A sample pattern:

{
    "id": "number",
    "name": "string",
    "price": "number",
    "discounted": "bool",
    "manufacturer": {
        "name": "string"
    },
    "properties": [
        {
            "name": "string",
            "value": "string",
            "tags": [ "string" ]
        }
    ]
}

A JSON document that successfully matches the pattern:

{
    "id": 1,
    "price": 12.50,
    "name": "A green door",
    "manufacturer": {
        "name": "ACME"
    },
    "discounted": true,
    "properties": [
        {
            "name": "blah",
            "value": "whatever",
            "tags": [ "great" ]
        },
        {
            "name": "foo",
            "value": "bar",
            "tags": [ ]
        }
    ]
}

By default, all properties in the pattern are mandatory. JSON documents omitting the properties are not considered to match. However, properties appearing in the JSON document that are not part of the pattern are ignored and considered to match.

Optional properties

If a property within an object is optional, suffix its name in the pattern with a question mark '?'.

For example:

{
    "id": "number",
    "name": "string",
    "discounted?": "bool"
}

All of the following JSON documents successfully match this pattern:

{
    "id": 4,
    "name": "A green door",
    "discounted": true
}

{
    "id": 4,
    "name": "A green door"
}

{
    "id": 4,
    "name": "A green door",
    "discounted": null
}

Note that an optional property may also have a "null" value. This is not true of non-optional properties.

Regular expressions

The string type may also include an optional regular expression to which a value must conform.

{
    "date": "string:\\d{8}"
}

Matches:

{
    "date": "20130101"
}

Does not match:

{
    "date": "130101"
}

URL types

Links are often expressed in JSON. To verify URLs, use the "url" type.

{
    "self": "url"
}

Any non-empty (and non-whitespace) string that can be converted to an NSURL object is considered a match.

Note that this permits URLs other than HTTP URLs, such as FTP URLs, and also relative URLs.

To limit matches to absolute HTTP and HTTPS URLs, append the "http" modifier to the "url" type.

{
    "self": "url:http"
}

Usage

VeriJSON expects the JSON object/array and pattern to be represented as Objective-C objects (as opposed to strings).

To do this, use the built-in NSJSONSerialization class to convert the raw data from a request or string into a JSON object. Do the same for your pattern JSON (which would typically be stored in your app bundle but may also be requested over a network).

// Deserialise the JSON data from a request into an NSDictionary or NSArray.
NSData *jsonData = request.data;
id json = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:NULL];

// Deserialise the pattern data from your app bundle into an NSDictionary or NSArray.
NSString *patternPath = [[NSBundle mainBundle] pathForResource:@"Pattern" ofType:@"json"];
NSData *patternData = [NSData dataWithContentsOfFile:patternPath];
id pattern = [NSJSONSerialization JSONObjectWithData:patternData options:0 error:NULL];

Then simply pass the Objective-C JSON object and pattern object into VeriJSON.

// Verify the JSON against the pattern.
BOOL valid = [[VeriJSON new] verifyJSON:json pattern:pattern];

Installing

CocoaPods is the easiest way to use VeriJSON. Note that you may need to specify the iOS version you're targeting.

platform :ios, '5.1'
pod 'VeriJSON'

If you'd rather not use CocoaPods, just copy the directory VeriJSON/VeriJSON into your project.

Import VeriJSON in any places you'd like to use it:

#import "VeriJSON.h"

Related work

JSON Schema is another JSON based format for describing JSON data. It is more explicit in the way it models documents, and is able to describe more complicated structures. There are also a number of existing implementations.

By comparison, VeriJSON trades expressiveness for clarity.

verijson's People

Contributors

xdream86 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.