Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'jsonpickle' in functional components in Python. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
def test_types_put(self):
method = 'test_types_put'
url = '/api/assets/types'
self.logger.info('[%s] URL: %s', method, url)
json_dict = {'session_id': 'test', 'object': self.prepare_new_asset_type()}
new_asset_type_body = jsonpickle.encode(json_dict)
self.logger.info('JSON data: %s', new_asset_type_body)
rv = self.app.delete('/api/assets/types/name/%s?session_id=test' % quote(self.prepare_new_asset_type().theName))
rv = self.app.post(url, content_type='application/json', data=new_asset_type_body)
self.logger.debug('[%s] Response data: %s', method, rv.data)
json_resp = jsonpickle.decode(rv.data)
self.assertIsNotNone(json_resp, 'No results after deserialization')
type_id = json_resp.get('asset_type_id', None)
self.assertIsNotNone(type_id, 'No asset type ID returned')
self.assertGreater(type_id, 0, 'Invalid asset type ID returned [%d]' % type_id)
self.logger.info('[%s] Asset type ID: %d', method, type_id)
type_to_update = self.prepare_new_asset_type()
type_to_update.theName = 'Edited test asset type'
type_to_update.theId = type_id
json_dict = {'session_id': 'test', 'object': type_to_update}
upd_type_body = jsonpickle.encode(json_dict)
rv = self.app.put('/api/assets/types/name/%s?session_id=test' % quote(self.prepare_new_asset_type().theName), data=upd_type_body, content_type='application/json')
self.assertIsNotNone(rv.data, 'No response')
json_resp = jsonpickle.decode(rv.data)
self.assertIsNotNone(json_resp)
self.assertIsInstance(json_resp, dict)
def test_types_get(self):
method = 'test_types_get'
rv = self.app.get('/api/assets/types?session_id=test')
assets = jsonpickle.decode(rv.data)
self.assertIsNotNone(assets, 'No results after deserialization')
self.assertIsInstance(assets, list, 'The result is not a dictionary as expected')
self.assertGreater(len(assets), 0, 'No assets in the dictionary')
self.logger.info('[%s] Asset types found: %d', method, len(assets))
asset_type = assets[0]
self.logger.info('[%s] First asset types: %s [%d]\n', method, asset_type['theName'], asset_type['theId'])
def test_serialize(self):
filename = "dos_sd_test1.atr"
pathname = os.path.join(os.path.dirname(__file__), "../samples", filename)
c = Collection(pathname)
m = c.containers[0].media
m.set_comment_at(100, "at location 100")
m.set_comment_at(1000, "at location 1000")
m.set_comment_at(10000, "at location 10000")
s = {}
c.serialize_session(s)
print(s)
j = jsonpickle.dumps(s)
print(j)
sprime = jsonpickle.loads(j)
jprime = jsonpickle.dumps(sprime)
print(jprime)
assert j == jprime
c2 = Collection(pathname, session=sprime)
s2 = {}
m2 = c2.containers[0].media
assert m2.get_comment_at(100) == "at location 100"
assert m2.get_comment_at(1000) == "at location 1000"
assert m2.get_comment_at(10000) == "at location 10000"
c2.serialize_session(s2)
j2 = jsonpickle.dumps(s2)
assert j == j2
obj_json_unpickable_true = '{"id": 1,' \
' "key1": "key1",' \
' "key2": "key2",' \
' "object1": {"id": 11,' \
' "key1": "keychild1",' \
' "key2": "keychild2",' \
' "py/object": "tests.util.test_json.MyChildClass"},' \
' "object2": {"id": 12,' \
' "key1": "keychild1",' \
' "key2": "keychild2",' \
' "py/object": "tests.util.test_json.MyChildClass"},' \
' "py/object": "tests.util.test_json.MyClass"}'
# Force sorted keys to be able to compare results (Python 3 sorts by default)
jsonpickle.set_encoder_options('simplejson', sort_keys=True)
def test_object_from_json():
assert obj == from_json(obj_json_unpickable_true)
def test_dict_from_json():
assert obj_dict == from_json(obj_json)
def test_object_to_json():
assert obj_json == to_json(obj)
def test_dict_to_json():
assert obj_json == to_json(obj_dict)
def random_string(prefix, maxlen):
symbols = string.ascii_letters + string.digits + string.punctuation + " "*10
return prefix + "".join([random.choice(symbols) for i in range(random.randrange(maxlen))])
testdata = [Group(name="", header="", footer="")] + [
Group(name=random_string("name", 10), header=random_string("header", 7), footer=random_string("footer", 5))
for i in range(n)
# for name in ["", random_string("name", 10)]
# for header in ["", random_string("header", 7)]
# for footer in ["", random_string("footer", 5)]
]
file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", f)
with open(file, "w") as out:
jsonpickle.set_encoder_options("json", indent=2)
out.write(jsonpickle.encode(testdata))
def tearDown(self):
jsonpickle.handlers.unregister(CustomObject)
def test_unpickleable(self):
"""
If 'unpickleable' is set on the Pickler, the date objects should be
simple, human-readable strings.
"""
obj = datetime.datetime.now()
pickler = jsonpickle.pickler.Pickler(unpicklable=False)
flattened = pickler.flatten(obj)
self.assertEqual(str(obj), flattened)
def setUp(self):
self.pickler = jsonpickle.pickler.Pickler()
self.unpickler = jsonpickle.unpickler.Unpickler()
def test_is_dicitonary_sequences(self):
self.assertFalse(util.is_dictionary([]))
self.assertFalse(util.is_dictionary(set()))