Add registry.Json.

This commit is contained in:
Valentin Lorentz 2012-12-08 20:59:36 +01:00
parent 3063340ce3
commit d8bd1f6f92
2 changed files with 16 additions and 0 deletions

View File

@ -32,6 +32,7 @@ import re
import os
import sys
import time
import json
import codecs
import string
import textwrap
@ -678,4 +679,11 @@ class TemplatedString(String):
else:
self.error()
class Json(String):
# Json-serializable data
def setValue(self, v):
super(Json, self).setValue(json.dumps(v))
def __call__(self):
return json.loads(super(Json, self).__call__())
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:

View File

@ -130,6 +130,14 @@ class ValuesTestCase(SupyTestCase):
v.set('"xyzzy')
self.assertEqual(v(), '"xyzzy')
def testJson(self):
data = {'foo': ['bar', 'baz', 5], 'qux': None}
v = registry.Json('foo', 'help')
self.assertEqual(v(), 'foo')
v.setValue(data)
self.assertEqual(v(), data)
self.assertIsNot(v(), data)
def testNormalizedString(self):
v = registry.NormalizedString("""foo
bar baz