如何在Cerberus架构中添加注释?

时间:2016-10-19 20:41:39

标签: python json comments eve cerberus

我正在使用python-eve,我想在架构文件(.json)中添加一些注释。所以到现在为止我尝试了不同的评论风格:

/*TYPE_OF_REFERENCE_MAPPING = {
    'ABST': 'Abstract',
    'ADVS': 'Audiovisual material',
    'AGGR': 'Aggregated Database',
    'ANCIENT': 'Ancient Text',
    'ART': 'Art Work',
    ...
}*/

//TYPE_OF_REFERENCE_MAPPING = {
//    'ABST': 'Abstract',
//    'ADVS': 'Audiovisual material',
//    'AGGR': 'Aggregated Database',
//    'ANCIENT': 'Ancient Text',
//    'ART': 'Art Work',
//    ...

#TYPE_OF_REFERENCE_MAPPING = {
#    'ABST': 'Abstract',
#    'ADVS': 'Audiovisual material',
#    'AGGR': 'Aggregated Database',
#    'ANCIENT': 'Ancient Text',
#    'ART': 'Art Work',
#    ...
#}

所有产生错误:

/api/settings.py", line 25, in <module>
    cite_schema = json.load(f)
  File "/usr/lib/python2.7/json/__init__.py", line 290, in load
    **kw)
  File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 369, in decode
    raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 233 column 1 - line 238 column 10 (char 3943 - 4111)

2 个答案:

答案 0 :(得分:1)

问题是json.load()不支持注释。这是完整的解决方案。

在我的Eve设置文件中:

import commentjson as json
...
annot_schema = json.load(f)
...
taxonannot_endopoint = {'resource_method': ['GET', 'POST'],
                        'item_methods': ['GET', 'PUT', 'PATCH'],
                        'schema': annot_schema}

现在我的架构文件接受注释行:

# comment
{
  "primary_title": {
          "type": "string",
          "required": true
          },
...
}

答案 1 :(得分:0)

查看documentation中的此示例是否有帮助:

# 'people' schema definition
'schema'= {
    'firstname': {
        'type': 'string',
        'minlength': 1,
        'maxlength': 10,
    },
    'lastname': {
        'type': 'string',
        'minlength': 1,
        'maxlength': 15,
        'required': True,
        'unique': True,
    },
    # 'role' is a list, and can only contain values from 'allowed'.
    'role': {
        'type': 'list',
        'allowed': ["author", "contributor", "copy"],
    },
    # An embedded 'strongly-typed' dictionary.
    'location': {
        'type': 'dict',
        'schema': {
            'address': {'type': 'string'},
            'city': {'type': 'string'}
        },
    },
    'born': {
        'type': 'datetime',
    },
}