本文整理汇总了Python中ujson.encode函数的典型用法代码示例。如果您正苦于以下问题:Python encode函数的具体用法?Python encode怎么用?Python encode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了encode函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: flushcdn
def flushcdn():
urlstype = int(request.query.urlstype)
RawUrls = request.query.urls
UrlsList = RawUrls.split(",")
urls = ""
for url in UrlsList:
urls = urls + url + "|"
urls = urls[:-1]
print("urls is : %s"%urls)
receive_data_dict = CacheFlush.Flush(urls,urlstype)
timestamp = int(time.time())
if receive_data_dict["head"] == "success":
redpipe = redata.pipeline()
url_rid_pairs_list = receive_data_dict["body"]
for item in url_rid_pairs_list:
for url,rid in item.items():
redpipe.zadd("CacheFlushingZSet",ujson.encode({rid:url}),timestamp)
redpipe.execute()
return request.query.jsoncallback + "(" + ujson.encode(receive_data_dict) + ")"
示例2: test_encodeNumericOverflow
def test_encodeNumericOverflow(self):
try:
ujson.encode(12839128391289382193812939)
except OverflowError:
pass
else:
assert False, "expected OverflowError"
示例3: loadFromFilesystem
def loadFromFilesystem(self):
if os.path.isfile(PROFILE_DIR+'UserProfile/UserProfile.json'):
# We already have a JSON file. Load the details from the file at the start.
with open(PROFILE_DIR+'UserProfile/UserProfile.json', 'rb') as f:
self.__settingsAndProfile = ujson.loads(f.read())
# Check for old version.
if 'selectedTopics' in self.__settingsAndProfile:
# This is a 1.1.2 JSON file. needs to be migrated.
migrationResult = self.__migrateFrom112to120(self.__settingsAndProfile)
with open(PROFILE_DIR+'UserProfile/UserProfile.json', 'wb') as f:
f.write(ujson.encode(migrationResult))
self.__settingsAndProfile = ujson.loads(f.read())
else:
# The main
self.__updateBootStatus()
else:
# We don't have a JSON file. This means it's not created yet. Create it.
with open(PROFILE_DIR+'UserProfile/UserProfile.json', 'wb') as f:
# Now, time to set some defaults.
newProfileFile = self.__produceProfileWithDefaults()
newProfileFile['machineDetails']['listeningPort'] = self.__getRandomOpenPort()
f.write(ujson.encode(newProfileFile))
# This is the first load ever.
with open(PROFILE_DIR+'UserProfile/UserProfile.json', 'rb') as f:
self.__settingsAndProfile = ujson.loads(f.read())
示例4: test_encodeDoubleNan
def test_encodeDoubleNan(self):
input = float("nan")
try:
ujson.encode(input)
assert False, "Expected exception!"
except (OverflowError):
return
assert False, "Wrong exception"
示例5: test_encodeDoubleNegInf
def test_encodeDoubleNegInf(self):
input = -float('inf')
try:
ujson.encode(input)
assert False, "Expected exception!"
except(OverflowError):
return
assert False, "Wrong exception"
示例6: test_encodeBigEscape
def test_encodeBigEscape(self):
for x in range(10):
if six.PY3:
base = '\u00e5'.encode('utf-8')
else:
base = "\xc3\xa5"
input = base * 1024 * 1024 * 2
ujson.encode(input)
示例7: test_encodeDictWithUnicodeKeys
def test_encodeDictWithUnicodeKeys(self):
input = { u"key1": u"value1", u"key1": u"value1", u"key1": u"value1", u"key1": u"value1", u"key1": u"value1", u"key1": u"value1" }
output = ujson.encode(input)
input = { u"بن": u"value1", u"بن": u"value1", u"بن": u"value1", u"بن": u"value1", u"بن": u"value1", u"بن": u"value1", u"بن": u"value1" }
output = ujson.encode(input)
pass
示例8: test_sepcial__json__
def test_sepcial__json__(self):
class TestObj(dict):
def __json__(self):
return {'hello_new': 'world_new'}
json = ujson.encode(TestObj())
self.assertEqual(json, '{"hello_new":"world_new"}')
json_list = ujson.encode([TestObj()])
self.assertEqual(json_list, '[{"hello_new":"world_new"}]')
示例9: test_hooks_exceptionHandling
def test_hooks_exceptionHandling(self):
class TestHookException(Exception):
pass
call_counters = {
'pre_encode_hook': 0,
'string_hook': 0,
'object_hook': 0,
}
def pre_encode_hook(_unused_obj):
call_counters['pre_encode_hook'] += 1
raise TestHookException()
def string_hook(_unused_obj):
call_counters['string_hook'] += 1
raise TestHookException()
def object_hook(_unused_obj):
call_counters['object_hook'] += 1
raise TestHookException()
input = {
'foo': 1,
'bar': {
'a': 'a',
'b': 'b',
},
}
json = """
{
"foo": 1,
"bar": {
"a": "a",
"b": "b"
}
}
"""
with self.assertRaises(TestHookException):
ujson.encode(input, pre_encode_hook=pre_encode_hook)
with self.assertRaises(TestHookException):
ujson.decode(json, string_hook=string_hook)
with self.assertRaises(TestHookException):
ujson.decode(json, object_hook=object_hook)
# Test not only that the exception is raised, but also that the hook
# is called only once. I.e. that the low-level code detects the error
# and stops the iteration process after the first call.
self.assertEqual(call_counters['pre_encode_hook'], 1)
self.assertEqual(call_counters['string_hook'], 1)
self.assertEqual(call_counters['object_hook'], 1)
示例10: test_toJson
def test_toJson(self):
d = {u"key": 31337}
json = ujson.encode(d)
class AlreadyJson:
def toJson(self):
return json
o = AlreadyJson()
output = ujson.encode(o)
dec = ujson.decode(output)
self.assertEquals(dec, d)
示例11: connectionMade
def connectionMade(self):
if len(self.pattern):
for job in self.pattern:
if self.byKey.has_key(job) and self.byKey.get(job):
self.transport.write("%s\r\n" % ujson.encode({
'c': 'reg',
'p': {'n': job, 'k': True}
}));
else:
self.transport.write("%s\r\n" % ujson.encode({
'c': 'reg',
'p': {'n': job, 'k': False}
}));
示例12: test_encodeNumericOverflowNested
def test_encodeNumericOverflowNested(self):
for n in xrange(0, 100):
class Nested:
x = 12839128391289382193812939
nested = Nested()
try:
ujson.encode(nested)
except OverflowError:
pass
else:
assert False, "expected OverflowError"
示例13: test_doublePrecisionTest
def test_doublePrecisionTest(self):
input = 30.012345678901234
output = ujson.encode(input, double_precision = 15)
self.assertEqual(input, json.loads(output))
self.assertEqual(input, ujson.decode(output))
output = ujson.encode(input, double_precision = 9)
self.assertEqual(round(input, 9), json.loads(output))
self.assertEqual(round(input, 9), ujson.decode(output))
output = ujson.encode(input, double_precision = 3)
self.assertEqual(round(input, 3), json.loads(output))
self.assertEqual(round(input, 3), ujson.decode(output))
示例14: test_encodeUTF8EncodedString
def test_encodeUTF8EncodedString(self):
unicode_str = u"مرحبا العالم Salam dünya Прывітанне свет Здравей, свят"
utf_str = unicode_str.encode('utf-8')
encoded_from_unicode = ujson.encode(unicode_str)
encoded_from_utf_str = ujson.encode(utf_str)
self.assertEqual(encoded_from_unicode, encoded_from_utf_str)
encoded_from_unicode = ujson.encode(unicode_str, ensure_ascii=False)
encoded_from_utf_str = ujson.encode(utf_str, ensure_ascii=False)
self.assertEqual(encoded_from_unicode, encoded_from_utf_str)
示例15: test_encodeNullCharacter
def test_encodeNullCharacter(self):
input = "31337 \x00 1337"
output = ujson.encode(input)
self.assertEqual(input, json.loads(output))
self.assertEqual(output, json.dumps(input))
self.assertEqual(input, ujson.decode(output))
input = "\x00"
output = ujson.encode(input)
self.assertEqual(input, json.loads(output))
self.assertEqual(output, json.dumps(input))
self.assertEqual(input, ujson.decode(output))
self.assertEqual('" \\u0000\\r\\n "', ujson.dumps(u" \u0000\r\n "))