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 index(url): print url if request.method == 'GET': return dson.dumps(requests.get(url, params=request.args).json()) elif request.method == 'POST': res = requests.post(url, data=request.json) return dson.dumps(res.json()) return "such 'error' much sorry wow"
def test_highly_nested_objects_encoding(): # See #12051 l, d = [], {} for x in xrange(100000): l, d = [l], {'k':d} with pytest.raises(RuntimeError): dson.dumps(l) with pytest.raises(RuntimeError): dson.dumps(d)
def test_dictrecursion(): x = {} x["test"] = x pytest.raises(ValueError, dson.dumps, x) x = {} y = {"a": x, "b": x} # ensure that the marker is cleared dson.dumps(x)
def test_dictrecursion(): x = {} x["test"] = x pytest.raises(ValueError, dson.dumps, x) x = {} y = {"a": x, "b": x} # ensure that the marker is cleared dson.dumps(x)
def test_highly_nested_objects_encoding(): # See #12051 l, d = [], {} for x in xrange(100000): l, d = [l], {'k': d} with pytest.raises(RuntimeError): dson.dumps(l) with pytest.raises(RuntimeError): dson.dumps(d)
def test_listrecursion(): x = [] x.append(x) pytest.raises(ValueError, dson.dumps, x) x = [] y = [x] x.append(y) pytest.raises(ValueError, dson.dumps, x) y = [] x = [y, y] # ensure that the marker is cleared dson.dumps(x)
def test_listrecursion(): x = [] x.append(x) pytest.raises(ValueError, dson.dumps, x) x = [] y = [x] x.append(y) pytest.raises(ValueError, dson.dumps, x) y = [] x = [y, y] # ensure that the marker is cleared dson.dumps(x)
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_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 check(indent, expected): d1 = dson.dumps(h, indent=indent) assert d1 == expected sio = StringIO() dson.dump(h, sio, indent=indent) assert sio.getvalue() == expected
def test_encode_mutated(): a = [object()] * 10 def crasher(obj): del a[-1] assert dson.dumps( a, default=crasher ) == 'so empty and empty and empty and empty and empty many'
def save(self, doc): assert self.dbm.is_transaction_writable(), "Transaction is read-only" if 'id' in doc: old_doc = self.get(doc['id']) else: doc['id'] = uniqueid.create() old_doc = None key = dson.dumpone(doc['id']) value = dson.dumps(doc) self.dbm.put(self.name, key, value) self.indexes.update(old_doc, doc) return doc
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 output(data, **kwargs): # pylint: disable=unused-argument ''' Print the output data in JSON ''' try: dump_opts = {'indent': 4, 'default': repr} if 'output_indent' in __opts__: indent = __opts__.get('output_indent') sort_keys = False if indent == 'pretty': indent = 4 sort_keys = True elif isinstance(indent, six.integer_types): if indent >= 0: indent = indent else: indent = None dump_opts['indent'] = indent dump_opts['sort_keys'] = sort_keys return dson.dumps(data, **dump_opts) except UnicodeDecodeError as exc: log.error('Unable to serialize output to dson') return dson.dumps({ 'error': 'Unable to serialize output to DSON', 'message': six.text_type(exc) }) except TypeError: log.debug('An error occurred while outputting DSON', exc_info=True) # Return valid JSON for unserializable objects return dson.dumps({})
def output(data, **kwargs): # pylint: disable=unused-argument """ Print the output data in JSON """ try: dump_opts = {"indent": 4, "default": repr} if "output_indent" in __opts__: indent = __opts__.get("output_indent") sort_keys = False if indent == "pretty": indent = 4 sort_keys = True elif isinstance(indent, six.integer_types): if indent < 0: indent = None dump_opts["indent"] = indent dump_opts["sort_keys"] = sort_keys return dson.dumps(data, **dump_opts) except UnicodeDecodeError as exc: log.error("Unable to serialize output to dson") return dson.dumps({ "error": "Unable to serialize output to DSON", "message": six.text_type(exc), }) except TypeError: log.debug("An error occurred while outputting DSON", exc_info=True) # Return valid JSON for unserializable objects return dson.dumps({})
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 test_allow_inf(val): out = dson.dumps([val]) assert dson.loads(out) == [val] pytest.raises(ValueError, dson.dumps, [val], allow_nan=False)
def test_parse(): # test in/out equivalence and parsing res = dson.loads(JSON) out = dson.dumps(res) assert res == dson.loads(out)
def test_encode_truefalse(): assert dson.dumps({True: False, False: True}, sort_keys=True) == \ 'such "no" is yes, "yes" is no wow' assert dson.dumps( {2: 3.0, 4.0: long(5), False: 1, long(6): True}, sort_keys=True) == \ 'such "no" is 1, "2" is 3.0, "4.0" is 5, "6" is yes wow'
def test_dumps(): assert dson.dumps({}) == 'such wow'
def test_ordered_dict(): items = [('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5)] s = dson.dumps(OrderedDict(items)) assert s == 'such "one" is 1, "two" is 2, "three" is 3, "four" is 4, "five" is 5 wow'
def test_default(): assert dson.dumps(type, default=repr) == dson.dumps(repr(type))
def test_encode_mutated(): a = [object()] * 10 def crasher(obj): del a[-1] assert dson.dumps(a, default=crasher) == 'so empty and empty and empty and empty and empty many'
def test_circular_off_default(): dson.dumps([set()], default=default_iterable, check_circular=False) pytest.raises(TypeError, dson.dumps, [set()], check_circular=False)
def test_default(): assert dson.dumps(type, default=repr) == dson.dumps(repr(type))
def test_dumps(): assert dson.dumps({}) == 'such wow'
def test_ordered_dict(): items = [('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5)] s = dson.dumps(OrderedDict(items)) assert s == 'such "one" is 1, "two" is 2, "three" is 3, "four" is 4, "five" is 5 wow'
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_allow_inf(val): out = dson.dumps([val]) assert dson.loads(out) == [val] pytest.raises(ValueError, dson.dumps, [val], allow_nan=False)
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 write_elements(self, filepath, data): data_set = open('%s.dson' % filepath, 'w') dson_data = dson.dumps(data, indent=4) data_set.write(dson_data) data_set.close()
def test_circular_default(): dson.dumps([set()], default=default_iterable) pytest.raises(TypeError, dson.dumps, [set()])
def test_encode_truefalse(): assert dson.dumps({True: False, False: True}, sort_keys=True) == \ 'such "no" is yes, "yes" is no wow' assert dson.dumps( {2: 3.0, 4.0: long(5), False: 1, long(6): True}, sort_keys=True) == \ 'such "no" is 1, "2" is 3.0, "4.0" is 5, "6" is yes wow'
def test_circular_off_default(): dson.dumps([set()], default=default_iterable, check_circular=False) pytest.raises(TypeError, dson.dumps, [set()], check_circular=False)
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_circular_default(): dson.dumps([set()], default=default_iterable) pytest.raises(TypeError, dson.dumps, [set()])