本文整理汇总了Python中cjson.encode方法的典型用法代码示例。如果您正苦于以下问题:Python cjson.encode方法的具体用法?Python cjson.encode怎么用?Python cjson.encode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cjson
的用法示例。
在下文中一共展示了cjson.encode方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: encode
# 需要导入模块: import cjson [as 别名]
# 或者: from cjson import encode [as 别名]
def encode(value):
from bson import json_util
value = json_encode(value, ensure_ascii=False, default=json_util.default)
if sys.version < '3':
return unicode(value)
return value
示例2: json_encode
# 需要导入模块: import cjson [as 别名]
# 或者: from cjson import encode [as 别名]
def json_encode(data):
return _json_encode(
OrderedDict(sorted(data.items(), key=lambda item: item[0]))
)
示例3: serialize
# 需要导入模块: import cjson [as 别名]
# 或者: from cjson import encode [as 别名]
def serialize(cls, data):
if six.PY3:
if isinstance(data, bytes):
return json.dumps(data.decode('utf-8'), cls=JsonEncoder,ensure_ascii = False).encode('utf-8')
else:
return json.dumps(data, cls=JsonEncoder,ensure_ascii = False).encode('utf-8')
else:
return json.dumps(data, cls=JsonEncoder,ensure_ascii = False).encode('utf-8')
示例4: setUp
# 需要导入模块: import cjson [as 别名]
# 或者: from cjson import encode [as 别名]
def setUp(self):
self.query('DROP SCHEMA t1 CASCADE', ignore_errors=True)
self.query('CREATE SCHEMA t1')
self.query(dedent('''\
CREATE EXTERNAL SCALAR SCRIPT
cjson_decode(json VARCHAR(10000))
RETURNS VARCHAR(10000) AS
# redirector @@redirector_url@@
import json
import cjson
def run(ctx):
return json.dumps(cjson.decode(ctx.json))
'''))
self.query(dedent('''\
CREATE EXTERNAL SCALAR SCRIPT
cjson_encode(json VARCHAR(10000))
RETURNS VARCHAR(10000) AS
# redirector @@redirector_url@@
import json
import cjson
def run(ctx):
return cjson.encode(json.loads(ctx.json))
'''))
示例5: setUp
# 需要导入模块: import cjson [as 别名]
# 或者: from cjson import encode [as 别名]
def setUp(self):
self.query('DROP SCHEMA t1 CASCADE', ignore_errors=True)
self.query('CREATE SCHEMA t1')
self.query(dedent('''\
CREATE python SCALAR SCRIPT
cjson_decode(json VARCHAR(10000))
RETURNS VARCHAR(10000) AS
import json
import cjson
def run(ctx):
return json.dumps(cjson.decode(ctx.json))
'''))
self.query(dedent('''\
CREATE python SCALAR SCRIPT
cjson_encode(json VARCHAR(10000))
RETURNS VARCHAR(10000) AS
import json
import cjson
def run(ctx):
return cjson.encode(json.loads(ctx.json))
'''))
示例6: dumps
# 需要导入模块: import cjson [as 别名]
# 或者: from cjson import encode [as 别名]
def dumps(obj, **kwargs):
return encode(obj)
示例7: dump
# 需要导入模块: import cjson [as 别名]
# 或者: from cjson import encode [as 别名]
def dump(obj, fp, **kwargs):
fp.write( encode(obj) )
示例8: verify_and_create_timelock
# 需要导入模块: import cjson [as 别名]
# 或者: from cjson import encode [as 别名]
def verify_and_create_timelock(self, output):
mark, address, value, txid, n = output
mark_data = self.kv.get_by_section_key('mark_available', '{}#{}'.format(mark, address))
if not mark_data:
return
if mark_data['available']:
return
return_address = mark_data['return_address']
locktime = mark_data['locktime']
oracle_fees = mark_data['oracle_fees']
miners_fee_satoshi = mark_data['miners_fee_satoshi']
req_sigs = mark_data['req_sigs']
self.oracle.task_queue.save({
"operation": 'safe_timelock_create',
"json_data": cjson.encode({
'mark': mark,
'return_address': return_address,
'oracle_fees': oracle_fees,
'req_sigs': req_sigs,
'miners_fee_satoshi': miners_fee_satoshi,
'address': address,
'value': value,
'txid': txid,
'n': n}),
"done": 0,
"next_check": locktime
})
logging.info("found transaction for mark:{} on address:{}".format(mark, address))
info_msg = {
'operation': 'safe_timelock_found_transaction',
'in_reply_to': 'none',
'message_id': "%s-%s" % ("locked_transaction", str(randrange(1000000000,9000000000))),
'contract_id' : "{}#{}".format(address, mark),
}
self.oracle.broadcast_with_fastcast(json.dumps(info_msg))
示例9: encode_json
# 需要导入模块: import cjson [as 别名]
# 或者: from cjson import encode [as 别名]
def encode_json(obj):
"""Wrapper to re-encode JSON string in an implementation-independent way."""
# TODO: Verify correctness of cjson
return cjson.encode(obj)
示例10: filter_row
# 需要导入模块: import cjson [as 别名]
# 或者: from cjson import encode [as 别名]
def filter_row(self, row):
user_id = row[3]
user_info = {'user_id': [user_id, ]}
try:
user_id = int(user_id)
entry = self.user_by_id[user_id]
if 'username' in entry:
user_info['username'] = [entry['username'], ]
if 'name' in entry:
user_info['name'] = [entry['name'], ]
except KeyError:
log.error("Unable to find CWSM user_id: %r in the user_by_id map of size %s", user_id, len(self.user_by_id))
row[3] = self.remap_id(user_id) # student_id
# Courseware_studentmodule is not processed with the other SQL tables, so it
# is not escaped in the same way. In particular, we will not decode and encode it,
# but merely transform double backslashes.
state_str = row[4].replace('\\\\', '\\')
try:
if state_str == 'NULL':
updated_state_dict = {}
else:
state_dict = cjson.decode(state_str, all_unicode=True)
# Traverse the dictionary, looking for entries that need to be scrubbed.
updated_state_dict = self.obfuscator.obfuscate_structure(state_dict, u"state", user_info)
except Exception: # pylint: disable=broad-except
log.exception(u"Unable to parse state as JSON for record %s: type = %s, state = %r",
row[0], type(state_str), state_str)
updated_state_dict = {}
if updated_state_dict is not None:
# Can't reset values, so update original fields.
updated_state = cjson.encode(updated_state_dict).replace('\\', '\\\\')
row[4] = updated_state
if self.obfuscator.is_logging_enabled():
log.info(u"Obfuscated state for user_id '%s' module_id '%s'", user_id, row[2])
return row
示例11: run
# 需要导入模块: import cjson [as 别名]
# 或者: from cjson import encode [as 别名]
def run(self):
with self.output().open('w') as output_file:
with self.input()['data'][0].open('r') as input_file:
for line in input_file:
row = json.loads(line)
filtered_row = self.filter_row(row)
output_file.write(json.dumps(filtered_row, ensure_ascii=False).encode('utf-8'))
output_file.write('\n')
示例12: mapper
# 需要导入模块: import cjson [as 别名]
# 或者: from cjson import encode [as 别名]
def mapper(self, line):
event = eventlog.parse_json_event(line)
date_string = event['time'].split("T")[0]
filtered_event = self._filter_event(event)
if filtered_event is None:
return
yield date_string.encode('utf-8'), line.rstrip('\r\n')