Мы разрабатываем публичный сервис, который принимает сообщения JSON для хранения в базе данных.
Сейчас этот сервис, вероятно, первый из многих, и я работаю над способом структурирования схем JSON.У нас большой опыт работы со схемой XML, но схема JSON для нас немного нова.
Одна из идей состоит в том, чтобы включить раздел Header в каждую схему JSON, содержащую имя схемы , основная версия и уникальный идентификатор сообщения
Вот упрощенная версия такой схемы
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://www.example.com/json/01/sampleMessage",
"type": "object",
"title": "Sample Message for stackoverflow",
"description": "version 01.01",
"properties": {
"Header": {
"$ref": "#/definitions/Header"
},
"EanGsrn": {
"description": "Unique identifier of the Headpoint.",
"type": "string",
"pattern": "^[0-9]{18}$"
},
"Sector": {
"description": "Sector for which the Headpoint is valid.",
"type": "string",
"enum": [
"Electricity", "Gas"
]
}
},
"additionalProperties": false,
"required": [
"Header", "EanGsrn", "Sector"
],
"definitions": {
"Header": {
"id": "#Header",
"type": "object",
"description": "Standard header for all messages",
"properties": {
"Schema": {
"description": "$id of the schema of this message",
"type": "string",
"enum": ["http://www.example.com/json/01/sampleMessage"]
},
"Version": {
"description": "The specific version of the shema of this message",
"type": "string",
"pattern": "^01\\.[0-9]{2,3}$"
},
"MessageId": {
"description": "Unique identifier for the Message",
"type": "string",
"pattern": "^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"
}
},
"required": [
"Schema", "Version", "MessageId"
],
"additionalProperties": false
}
}
}
Преимуществами этого должны быть:
- Сообщение о неверной схеме или основной версии будет немедленно отклонено на этапе проверки схемы.
- Сам JSON содержит информацию о своей схемеи версия, облегчающая жизнь людям, нуждающимся в расследовании проблем и т. д. ... позже.
Вопросы
- Это обычно или есть другие лучшие практики вмир JSON для обработки подобных вещей?
- Это хорошая идея, или я что-то упускаю здесь очевидное?