def test_separators(): h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', 'i-vhbjkhnth', {'nifty': 87}, {'field': 'yes', 'morefield': False} ] expect = textwrap.dedent("""\ so so "blorpie" many and so "whoops" many and so many and "d-shtaeou" and "d-nthiouh" and "i-vhbjkhnth" and such "nifty" is 87 wow and such "field" is "yes", "morefield" is no wow many""") d1 = dson.dumps(h) d2 = dson.dumps(h, indent=2, sort_keys=True) h1 = dson.loads(d1) h2 = dson.loads(d2) assert h1 == h assert h2 == h assert d2 == expect
def test_object_pairs_hook(): s = ('such "xkd" is 1, "kcw" is 2, "art" is 3, "hxm" is 4, ' '"qrt" is 5, "pad" is 6, "hoy" is 7 wow') p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4), ("qrt", 5), ("pad", 6), ("hoy", 7)] assert dson.loads(s) == dict(p) assert dson.loads(s, object_pairs_hook=lambda x: x) == p assert dson.load(StringIO(s), object_pairs_hook=lambda x: x) == p od = dson.loads(s, object_pairs_hook=OrderedDict) assert od == OrderedDict(p) assert type(od) == OrderedDict # the object_pairs_hook takes priority over the object_hook assert dson.loads(s, object_pairs_hook=OrderedDict, object_hook=lambda x: None) == OrderedDict(p)
def test_allow_nan(): val = float('nan') out = dson.dumps([val]) res = dson.loads(out) assert len(res) == 1 assert res[0] != res[0] pytest.raises(ValueError, dson.dumps, [val], allow_nan=False)
def get(self, id): key = dson.dumpone(id) value = self.dbm.get(self.name, key) if value is None: return None else: return dson.loads(value)
def test_allow_nan(): val = float('nan') out = dson.dumps([val]) res = dson.loads(out) assert len(res) == 1 assert res[0] != res[0] pytest.raises(ValueError, dson.dumps, [val], allow_nan=False)
def test_separators(): h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', 'i-vhbjkhnth', { 'nifty': 87 }, { 'field': 'yes', 'morefield': False }] expect = textwrap.dedent("""\ so so "blorpie" many and so "whoops" many and so many and "d-shtaeou" and "d-nthiouh" and "i-vhbjkhnth" and such "nifty" is 87 wow and such "field" is "yes", "morefield" is no wow many""") d1 = dson.dumps(h) d2 = dson.dumps(h, indent=2, sort_keys=True) h1 = dson.loads(d1) h2 = dson.loads(d2) assert h1 == h assert h2 == h assert d2 == expect
def test_all(self): value = { 'string': 'I\'m a string', 'unicode': u'h\xf6d\xf6', 'float': 23.32, 'int': 123, 'realy_big_int': 1000000000000000000, 'bool': True, 'bool_false': False, 'none': None, 'datetime': datetime.utcnow().replace(tzinfo=pytz.utc), 'list': [1,2,3,"poodles",{'foo':'bar'},["poodles",4,5,6]] } bytes = dson.dumps(value) deserialized_value = dson.loads(bytes) self.assertEquals(deserialized_value, value)
def render(dson_input, saltenv='base', sls='', **kwargs): ''' Accepts DSON data as a string or as a file object and runs it through the JSON parser. :rtype: A Python data structure ''' if not isinstance(dson_input, six.string_types): dson_input = dson_input.read() log.debug('DSON input = %s', dson_input) if dson_input.startswith('#!'): dson_input = dson_input[(dson_input.find('\n') + 1):] if not dson_input.strip(): return {} return dson.loads(dson_input)
def render(dson_input, saltenv="base", sls="", **kwargs): """ Accepts DSON data as a string or as a file object and runs it through the JSON parser. :rtype: A Python data structure """ if not isinstance(dson_input, str): dson_input = dson_input.read() log.debug("DSON input = %s", dson_input) if dson_input.startswith("#!"): dson_input = dson_input[(dson_input.find("\n") + 1):] if not dson_input.strip(): return {} return dson.loads(dson_input)
def render(dson_input, saltenv="base", sls="", **kwargs): """ Accepts DSON data as a string or as a file object and runs it through the JSON parser. :rtype: A Python data structure """ if not isinstance(dson_input, six.string_types): dson_input = dson_input.read() log.debug("DSON input = %s", dson_input) if dson_input.startswith("#!"): dson_input = dson_input[(dson_input.find("\n") + 1) :] if not dson_input.strip(): return {} return dson.loads(dson_input)
def test_extra_data(): s = 'so 1 and 2 also 3 many 5' with pytest.raises(ValueError) as e: dson.loads(s) assert 'Extra data' in str(e)
def test_empty_object_pairs_hook(): assert dson.loads('such wow', object_pairs_hook=OrderedDict) == OrderedDict() assert dson.loads('such "empty" is such wow wow', object_pairs_hook=OrderedDict) == OrderedDict([('empty', OrderedDict())])
def load(self, data): return dson.loads(data)
def test_empty_objects(): assert dson.loads('such wow') == {} assert dson.loads('so many') == [] assert dson.loads('""') == u"" assert isinstance(dson.loads('""'), unicode)
def test_out_of_range(doc, expected): assert dson.loads(doc) == expected
def test_out_of_range(doc, expected): assert dson.loads(doc) == expected
def test_highly_nested_objects_decoding(): # test that loading highly-nested objects doesn't segfault when C # accelerations are used. See #12017 # str with pytest.raises(RuntimeError): dson.loads('such "a" is ' * 100000 + ' 1 ' + ' wow ' * 100000) with pytest.raises(RuntimeError): dson.loads('such "a" is ' * 100000 + ' so 1 many ' + ' wow ' * 100000) with pytest.raises(RuntimeError): dson.loads('so ' * 100000 + ' 1 ' + ' many ' * 100000) # unicode with pytest.raises(RuntimeError): dson.loads(u'such "a" is ' * 100000 + u' 1 ' + u' wow ' * 100000) with pytest.raises(RuntimeError): dson.loads(u'such "a" is ' * 100000 + u' so 1 many' + u' wow ' * 100000) with pytest.raises(RuntimeError): dson.loads(u'so ' * 100000 + u' 1 ' + u' many ' * 100000)
def test_ints(num): assert dson.dumps(num) == str(num) assert int(dson.dumps(num)) == num assert dson.loads(dson.dumps(num)) == num assert dson.loads(unicode(dson.dumps(num))) == num
def test_ints(num): assert dson.dumps(num) == str(num) assert int(dson.dumps(num)) == num assert dson.loads(dson.dumps(num)) == num assert dson.loads(unicode(dson.dumps(num))) == num
def test_parse(): # test in/out equivalence and parsing res = dson.loads(JSON) out = dson.dumps(res) assert res == dson.loads(out)
def test_highly_nested_objects_decoding(): # test that loading highly-nested objects doesn't segfault when C # accelerations are used. See #12017 # str with pytest.raises(RuntimeError): dson.loads('such "a" is ' * 100000 + ' 1 ' + ' wow ' * 100000) with pytest.raises(RuntimeError): dson.loads('such "a" is ' * 100000 + ' so 1 many ' + ' wow ' * 100000) with pytest.raises(RuntimeError): dson.loads('so ' * 100000 + ' 1 ' + ' many ' * 100000) # unicode with pytest.raises(RuntimeError): dson.loads(u'such "a" is ' * 100000 + u' 1 ' + u' wow ' * 100000) with pytest.raises(RuntimeError): dson.loads(u'such "a" is ' * 100000 + u' so 1 many' + u' wow ' * 100000) with pytest.raises(RuntimeError): dson.loads(u'so ' * 100000 + u' 1 ' + u' many ' * 100000)
def test_floats(num): assert float(dson.dumps(num).replace('very', 'e')) == num assert dson.loads(dson.dumps(num)) == num assert dson.loads(unicode(dson.dumps(num))) == num
def test_invalid_escape(): s = 'so "abc\\y" many' with pytest.raises(ValueError) as e: dson.loads(s) assert 'escape' in str(e)
def test_allow_inf(val): out = dson.dumps([val]) assert dson.loads(out) == [val] pytest.raises(ValueError, dson.dumps, [val], allow_nan=False)
def test_decimal(): rval = dson.loads('1.1', parse_float=decimal.Decimal) assert isinstance(rval, decimal.Decimal) assert rval == decimal.Decimal('1.1')
def test_float(): rval = dson.loads('1', parse_int=float) assert isinstance(rval, float) assert rval == 1.0
def test_decoder_optimizations(): # Several optimizations were made that skip over calls to # the whitespace regex, so this test is designed to try and # exercise the uncommon cases. The array cases are already covered. rval = dson.loads('such "key" is "value" . "k"is"v" wow') assert rval == {"key":"value", "k":"v"}
def test_floats(num): assert float(dson.dumps(num).replace('very', 'e')) == num assert dson.loads(dson.dumps(num)) == num assert dson.loads(unicode(dson.dumps(num))) == num
def test_allow_inf(val): out = dson.dumps([val]) assert dson.loads(out) == [val] pytest.raises(ValueError, dson.dumps, [val], allow_nan=False)