def get_obj_cls(obj): if not obj or util.is_function(obj): return None if util.is_container(obj): return obj if not util.is_primitive(obj): return type(obj) return None
def _get_flattener(self, obj): if PY2 and isinstance(obj, file): return self._flatten_file if util.is_primitive(obj): return lambda obj: obj if util.is_bytes(obj): return self._flatten_bytestring list_recurse = self._list_recurse if util.is_list(obj): if self._mkref(obj): return list_recurse else: self._push() return self._getref # We handle tuples and sets by encoding them in a "(tuple|set)dict" if util.is_tuple(obj): if not self.unpicklable: return list_recurse return lambda obj: {tags.TUPLE: [self._flatten(v) for v in obj]} if util.is_set(obj): if not self.unpicklable: return list_recurse return lambda obj: {tags.SET: [self._flatten(v) for v in obj]} if util.is_dictionary(obj): return self._flatten_dict_obj if util.is_type(obj): return _mktyperef if util.is_object(obj): return self._ref_obj_instance if util.is_module_function(obj): return self._flatten_function # instance methods, lambdas, old style classes... self._pickle_warning(obj) return None
def _get_flattener(self, obj): if util.is_primitive(obj): return lambda obj: obj list_recurse = self._list_recurse if util.is_list(obj): if self._mkref(obj): return list_recurse else: self._push() return self._getref # We handle tuples and sets by encoding them in a "(tuple|set)dict" if util.is_tuple(obj): if not self.unpicklable: return list_recurse return lambda obj: {tags.TUPLE: [self._flatten(v) for v in obj]} if util.is_set(obj): if not self.unpicklable: return list_recurse return lambda obj: {tags.SET: [self._flatten(v) for v in obj]} if util.is_dictionary(obj): return self._flatten_dict_obj if util.is_type(obj): return _mktyperef if util.is_object(obj): return self._ref_obj_instance # else, what else? (methods, functions, old style classes...) return None
def test_is_primitive_unicode(self): self.assertTrue(is_primitive(unicode('hello'))) self.assertTrue(is_primitive(unicode(''))) self.assertTrue(is_primitive(unicode('hello')))
def test_is_primitive_None(self): self.assertTrue(is_primitive(None))
def test_is_primitive_unicode(self): self.assertTrue(is_primitive(unicode("hello"))) self.assertTrue(is_primitive(unicode(""))) self.assertTrue(is_primitive(unicode("hello")))
def flatten(self, obj): """Takes an object and returns a JSON-safe representation of it. Simply returns any of the basic builtin datatypes >>> p = Pickler() >>> p.flatten('hello world') 'hello world' >>> p.flatten(u'hello world') u'hello world' >>> p.flatten(49) 49 >>> p.flatten(350.0) 350.0 >>> p.flatten(True) True >>> p.flatten(False) False >>> r = p.flatten(None) >>> r is None True >>> p.flatten(False) False >>> p.flatten([1, 2, 3, 4]) [1, 2, 3, 4] >>> p.flatten((1,2,))[tags.TUPLE] [1, 2] >>> p.flatten({'key': 'value'}) {'key': 'value'} """ self._push() if self._depth == self._max_depth: return self._pop(repr(obj)) if util.is_primitive(obj): return self._pop(obj) if util.is_list(obj): return self._pop([self.flatten(v) for v in obj]) # We handle tuples and sets by encoding them in a "(tuple|set)dict" if util.is_tuple(obj): return self._pop({tags.TUPLE: [self.flatten(v) for v in obj]}) if util.is_set(obj): return self._pop({tags.SET: [self.flatten(v) for v in obj]}) if util.is_dictionary(obj): return self._pop(self._flatten_dict_obj(obj, obj.__class__())) if util.is_type(obj): return self._pop(_mktyperef(obj)) if util.is_object(obj): data = {} has_class = hasattr(obj, '__class__') has_dict = hasattr(obj, '__dict__') if self._mkref(obj): if has_class and not util.is_repr(obj): module, name = _getclassdetail(obj) if self.unpicklable is True: data[tags.OBJECT] = '%s.%s' % (module, name) if util.is_repr(obj): if self.unpicklable is True: data[tags.REPR] = '%s/%s' % (obj.__class__.__module__, repr(obj)) else: data = unicode(obj) return self._pop(data) if util.is_dictionary_subclass(obj): return self._pop(self._flatten_dict_obj(obj, data)) if util.is_noncomplex(obj): return self._pop([self.flatten(v) for v in obj]) if has_dict: return self._pop(self._flatten_dict_obj( obj.__dict__, data)) else: # We've seen this object before so place an object # reference tag in the data. This avoids infinite recursion # when processing cyclical objects. return self._pop(self._getref(obj)) return self._pop(data)
def test_is_primitive_bytes(self): self.assertFalse(util.is_primitive(b'hello')) self.assertFalse(util.is_primitive('foo'.encode('utf-8'))) self.assertTrue(util.is_primitive('foo'))
def test_is_primitive_bytes(self): self.assertFalse(util.is_primitive(b'hello')) if PY2: self.assertFalse(util.is_primitive('foo')) else: self.assertTrue(util.is_primitive('foo'))
def test_is_primitive_long(self): self.assertTrue(util.is_primitive(long(3)))
def test_is_primitive_float(self): self.assertTrue(util.is_primitive(0)) self.assertTrue(util.is_primitive(3.5)) self.assertTrue(util.is_primitive(-3.5)) self.assertTrue(util.is_primitive(float(3)))
def test_is_primitive_int(self): self.assertTrue(util.is_primitive(0)) self.assertTrue(util.is_primitive(3)) self.assertTrue(util.is_primitive(-3))
def test_is_primitive_set(self): self.assertFalse(util.is_primitive({1, 3}))
def test_is_primitive_dict(self): self.assertFalse(is_primitive({"key": "value"})) self.assertFalse(is_primitive({}))
def test_is_primitive_dict(self): self.assertFalse(is_primitive({'key':'value'})) self.assertFalse(is_primitive({}))
def test_is_primitive_set(self): self.assertFalse(is_primitive(set([1, 3])))
def test_is_primitive_bool(self): self.assertTrue(util.is_primitive(True)) self.assertTrue(util.is_primitive(False))
def test_is_primitive_None(self): self.assertTrue(util.is_primitive(None))
def test_is_primitive_str(self): self.assertTrue(util.is_primitive('hello')) self.assertTrue(util.is_primitive(''))
def test_is_primitive_long(self): self.assertTrue(util.is_primitive(2 ** 64))
def test_is_primitive_int(self): self.assertTrue(is_primitive(0)) self.assertTrue(is_primitive(3)) self.assertTrue(is_primitive(-3))
def test_is_primitive_unicode(self): self.assertTrue(util.is_primitive(compat.ustr(''))) self.assertTrue(util.is_primitive('hello'))
def test_is_primitive_long(self): self.assertTrue(is_primitive(long(3)))
def test_is_primitive_unicode(self): self.assertTrue(util.is_primitive(unicode('hello'))) self.assertTrue(util.is_primitive(unicode(''))) self.assertTrue(util.is_primitive(unicode('hello')))
def test_is_primitive_str(self): self.assertTrue(is_primitive("hello")) self.assertTrue(is_primitive(""))
def test_is_primitive_float(self): self.assertTrue(is_primitive(0)) self.assertTrue(is_primitive(3.5)) self.assertTrue(is_primitive(-3.5)) self.assertTrue(is_primitive(float(3)))
def test_is_primitive_list(self): self.assertFalse(util.is_primitive([])) self.assertFalse(util.is_primitive([4, 4]))
def test_is_primitive_bool(self): self.assertTrue(is_primitive(True)) self.assertTrue(is_primitive(False))
def test_is_primitive_dict(self): self.assertFalse(util.is_primitive({'key': 'value'})) self.assertFalse(util.is_primitive({}))
def test_is_primitive_str(self): self.assertTrue(is_primitive('hello')) self.assertTrue(is_primitive(''))
def test_is_primitive_tuple(self): self.assertFalse(util.is_primitive((1, 3))) self.assertFalse(util.is_primitive((1, )))
def test_is_primitive_list(self): self.assertFalse(is_primitive([])) self.assertFalse(is_primitive([4, 4]))
def test_is_primitive_set(self): self.assertFalse(util.is_primitive(set([1, 3])))
def test_is_primitive_tuple(self): self.assertFalse(is_primitive((1, 3))) self.assertFalse(is_primitive((1,)))
def test_is_primitive_object(self): self.assertFalse(util.is_primitive(Thing('test')))
def test_is_primitive_object(self): self.assertFalse(is_primitive(Thing('test')))
def flatten(self, obj): """Takes an object and returns a JSON-safe representation of it. Simply returns any of the basic builtin datatypes >>> p = Pickler() >>> p.flatten('hello world') 'hello world' >>> p.flatten(u'hello world') u'hello world' >>> p.flatten(49) 49 >>> p.flatten(350.0) 350.0 >>> p.flatten(True) True >>> p.flatten(False) False >>> r = p.flatten(None) >>> r is None True >>> p.flatten(False) False >>> p.flatten([1, 2, 3, 4]) [1, 2, 3, 4] >>> p.flatten((1,2,))[tags.TUPLE] [1, 2] >>> p.flatten({'key': 'value'}) {'key': 'value'} """ self._push() if self._depth == self._max_depth: return self._pop(repr(obj)) if util.is_primitive(obj): return self._pop(obj) if util.is_list(obj): return self._pop([ self.flatten(v) for v in obj ]) # We handle tuples and sets by encoding them in a "(tuple|set)dict" if util.is_tuple(obj): return self._pop({tags.TUPLE: [ self.flatten(v) for v in obj ]}) if util.is_set(obj): return self._pop({tags.SET: [ self.flatten(v) for v in obj ]}) if util.is_dictionary(obj): return self._pop(self._flatten_dict_obj(obj, obj.__class__())) if util.is_type(obj): return self._pop(_mktyperef(obj)) if util.is_object(obj): data = {} has_class = hasattr(obj, '__class__') has_dict = hasattr(obj, '__dict__') if self._mkref(obj): if has_class and not util.is_repr(obj): module, name = _getclassdetail(obj) if self.unpicklable is True: data[tags.OBJECT] = '%s.%s' % (module, name) if util.is_repr(obj): if self.unpicklable is True: data[tags.REPR] = '%s/%s' % (obj.__class__.__module__, repr(obj)) else: data = unicode(obj) return self._pop(data) if util.is_dictionary_subclass(obj): return self._pop(self._flatten_dict_obj(obj, data)) if util.is_noncomplex(obj): return self._pop([self.flatten(v) for v in obj]) if has_dict: return self._pop(self._flatten_dict_obj(obj.__dict__, data)) else: # We've seen this object before so place an object # reference tag in the data. This avoids infinite recursion # when processing cyclical objects. return self._pop(self._getref(obj)) return self._pop(data)
def flatten(self, obj): """Takes an object and returns a JSON-safe representation of it. Simply returns any of the basic builtin datatypes >>> p = Pickler() >>> p.flatten('hello world') 'hello world' >>> p.flatten(u'hello world') u'hello world' >>> p.flatten(49) 49 >>> p.flatten(350.0) 350.0 >>> p.flatten(True) True >>> p.flatten(False) False >>> r = p.flatten(None) >>> r is None True >>> p.flatten(False) False >>> p.flatten([1, 2, 3, 4]) [1, 2, 3, 4] >>> p.flatten((1,2,))[tags.TUPLE] [1, 2] >>> p.flatten({'key': 'value'}) {'key': 'value'} """ self._push() if self._depth == self._max_depth: return self._pop(repr(obj)) if util.is_primitive(obj): return self._pop(obj) if util.is_list(obj): if self._mkref(obj): return self._pop([self.flatten(v) for v in obj]) else: return self._getref(obj) # We handle tuples and sets by encoding them in a "(tuple|set)dict" if util.is_tuple(obj): if self.unpicklable: return self._pop({tags.TUPLE: [self.flatten(v) for v in obj]}) else: return self._pop([self.flatten(v) for v in obj]) if util.is_set(obj): if self.unpicklable: return self._pop({tags.SET: [self.flatten(v) for v in obj]}) else: return self._pop([self.flatten(v) for v in obj]) if util.is_dictionary(obj): return self._pop(self._flatten_dict_obj(obj, obj.__class__())) if util.is_type(obj): return self._pop(_mktyperef(obj)) if util.is_object(obj): if self._mkref(obj): # We've never seen this object so return its # json representation. return self._pop(self._flatten_obj_instance(obj)) else: # We've seen this object before so place an object # reference tag in the data. This avoids infinite recursion # when processing cyclical objects. return self._pop(self._getref(obj)) # else, what else? (methods, functions, old style classes...) return None
def flatten(self, obj): """Takes an object and returns a JSON-safe representation of it. Simply returns any of the basic builtin datatypes >>> p = Pickler() >>> p.flatten('hello world') 'hello world' >>> p.flatten(u'hello world') u'hello world' >>> p.flatten(49) 49 >>> p.flatten(350.0) 350.0 >>> p.flatten(True) True >>> p.flatten(False) False >>> r = p.flatten(None) >>> r is None True >>> p.flatten(False) False >>> p.flatten([1, 2, 3, 4]) [1, 2, 3, 4] >>> p.flatten((1,2,))[tags.TUPLE] [1, 2] >>> p.flatten({'key': 'value'}) {'key': 'value'} """ self._push() if self._depth == self._max_depth: return self._pop(repr(obj)) if util.is_primitive(obj): return self._pop(obj) if util.is_list(obj): return self._pop([ self.flatten(v) for v in obj ]) # We handle tuples and sets by encoding them in a "(tuple|set)dict" if util.is_tuple(obj): return self._pop({tags.TUPLE: [ self.flatten(v) for v in obj ]}) if util.is_set(obj): return self._pop({tags.SET: [ self.flatten(v) for v in obj ]}) if util.is_dictionary(obj): return self._pop(self._flatten_dict_obj(obj, obj.__class__())) if util.is_type(obj): return self._pop(_mktyperef(obj)) if util.is_object(obj): if self._mkref(obj): # We've never seen this object so return its # json representation. return self._pop(self._flatten_obj_instance(obj)) else: # We've seen this object before so place an object # reference tag in the data. This avoids infinite recursion # when processing cyclical objects. return self._pop(self._getref(obj)) return self._pop(data)