diff --git a/src/registry.py b/src/registry.py index 1981fca42..abd074176 100644 --- a/src/registry.py +++ b/src/registry.py @@ -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: diff --git a/test/test_registry.py b/test/test_registry.py index 9a8826ace..9e8bafb85 100644 --- a/test/test_registry.py +++ b/test/test_registry.py @@ -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