0

I am using python-eve and i want to add some comments in the schema file (.json). So until now i have tried different comment styles:

/*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',
#    ...
#}

All producing an error:

/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)
alemol
  • 8,058
  • 2
  • 24
  • 29
  • 1
    I have commented lines along my schemas using `#` like this [example](http://python-eve.org/config.html#schema-definition). The problem should be the way `json.load` deals with the comments. In my case I have the schema directly declared in `settings.py`. Instead of loading from a file, you could just import the schema from another module. – gcw Oct 20 '16 at 01:28
  • 1
    Is your json file format like the one on [this](http://stackoverflow.com/questions/2835559/parsing-values-from-a-json-file-in-python#2835672) correct answer? In this format you can use `#`. Mind the enclosing `{ }` for the whole file. – gcw Oct 20 '16 at 02:00
  • @gcw is right. I tried using only the first line commented with # and fails too. – alemol Nov 01 '16 at 18:28

2 Answers2

1

See if this example from the documentation helps:

# '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',
    },
}
Nicola Iarocci
  • 6,606
  • 1
  • 20
  • 33
1

The problem was that json.load() does not support comments. Here the complete solution.

In my Eve settings file:

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

Now my schema file accepts commented lines:

# comment
{
  "primary_title": {
          "type": "string",
          "required": true
          },
...
}
alemol
  • 8,058
  • 2
  • 24
  • 29