json-schema-php
- 授权协议: GPL
- 开发语言: PHP
- 操作系统: 跨平台
- 软件首页: http://code.google.com/p/json-schema-php/
- 软件文档: http://code.google.com/p/json-schema-php/
软件介绍
JSON Schema 用于描述JSON数据的结构和类型。如同DTD与XML的关系。
本实现用于使用 PHP 调用 JSON Schema 对 JSON 数据进行验证。
生成 JSON Schema
由JSON生成一个全格式的Schema,方便编辑修改(勿随便直接使用在实践中)。
$value = new stdClass(); $value->name = 'a name'; $value->age = 23; $value->height = 183.5; $jsonSchema = new JsonSchema(json_encode($value)); echo $jsonSchema->getSchema();
结果(真实结果格式化后)
{
"type":"object",
"properties":{
"name":{
"type":"string",
"format":"regex",
"pattern":"\/^[a-z0-9]+$\/i",
"minLength":0,
"maxLength":2147483647
},
"age":{
"type":"integer",
"default":0,
"minimum":0,
"maximum":2147483647,
"exclusiveMinimum":0,
"exclusiveMaximum":2147483647
},
"height":{
"type":"number",
"default":0,
"minimum":0,
"maximum":2147483647,
"exclusiveMinimum":0,
"exclusiveMaximum":2147483647
}
}
}
使用 JSON Schema 验证 JSON
$userType = '
{
"id": "user",
"description": "user info",
"type": "object",
"properties": {
"account": {"type": "string"},
"email": {"type": "string", "required": true},
"noexist": {"type": "string", "required": false}
}
}';
$type = array();
$type['users'][] = array('account' => 'userA', 'email' => 'userA@example.com');
$type['users'][] = array('account' => 'userB', 'email' => 'userB@example.com');
$type['users'][] = array('account' => 'userC', 'email' => 'userC@example.com');
$jsonSchema = new JsonSchema(json_encode($type));
$jsonSchema->addTypes($userType);
$jsonSchema->validate('
{
"type":"object",
"properties":{
"users":{
"type":"array",
"items":{
"$ref":"user"
}
}
}
}');
Web API的设计与开发
[日] 水野贵明 / 盛荣 / 人民邮电出版社 / 2017-6 / 52.00元
本书结合丰富的实例,详细讲解了Web API的设计、开发与运维相关的知识。第1章介绍Web API的概要;第2章详述端点的设计与请求的形式;第3章介绍响应数据的设计;第4章介绍如何充分利用HTTP协议规范;第5章介绍如何开发方便更改设计的Web API;第6章介绍如何开发牢固的Web API。 本书不仅适合在工作中需要设计、开发或修改Web API的技术人员阅读,对想了解技术细节的产品经理、运维人......一起来看看 《Web API的设计与开发》 这本书的介绍吧!
