Serializing and deserializing dictionaries of parameters.
The serialized “mapfiles” should be clear, flat-text strings, and allow easy merging of independent/conflicting changes.
Generate a JSON mapfile content string.
See also
Examples
>>> import sys
>>> sys.stdout.write(generate({}))
{}
>>> sys.stdout.write(generate({'q':'p'}))
{
"q": "p"
}
The blank lines ensure that merging occurs independent of surrounding content. Because the mapfile format is also used by the serve_commands where merging is not important, the amount of context is controllable.
>>> sys.stdout.write(generate({'q':u'Fran\u00e7ais'}, context=0))
{
"q": "Fran\u00e7ais"
}
>>> sys.stdout.write(generate({'q':u'hello'}, context=0))
{
"q": "hello"
}
>>> sys.stdout.write(generate(
... {'p':'really long line\n'*10, 'q': 'the next entry'},
... context=1))
{
"p": "really long line\nreally long line\nreally long line\nreally long line\nreally long line\nreally long line\nreally long line\nreally long line\nreally long line\nreally long line\n",
"q": "the next entry"
}
Parse a JSON mapfile string.
See also
Examples
>>> parse('{"q": "p"}')['q']
u'p'
>>> contents = generate({'a':'b', 'c':'d', 'e':'f'})
>>> dict = parse(contents)
>>> dict['a']
u'b'
>>> dict['c']
u'd'
>>> dict['e']
u'f'
>>> contents = generate({'q':u'Fran\u00e7ais'})
>>> dict = parse(contents)
>>> dict['q']
u'Fran\xe7ais'
>>> dict = parse('a!')
Traceback (most recent call last):
...
InvalidMapfileContents: Invalid JSON contents