本文整理汇总了Python中cjson.decode方法的典型用法代码示例。如果您正苦于以下问题:Python cjson.decode方法的具体用法?Python cjson.decode怎么用?Python cjson.decode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cjson
的用法示例。
在下文中一共展示了cjson.decode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_user_info
# 需要导入模块: import cjson [as 别名]
# 或者: from cjson import decode [as 别名]
def load_user_info(userinfo_path):
"""Reads a custom user-info file from the local fs that contains username, email, user-id, fullname."""
result = {}
with open(userinfo_path, 'r') as infile:
next(infile, None)
for line in infile:
fields = line.rstrip('\r\n').decode('utf8').split('\t')
# Once split, we can clean up the individual entries, and interpret the embedded newlines and tabs.
# We'll also strip here, to remove the additional whitespace on usernames and fullnames.
fields = [backslash_decode_value(field).strip() for field in fields]
record = UserInfoRecord(*fields)
entry = {key: [value, ] for key, value in record.__dict__.iteritems()}
# Store records twice, once with an int key, and once with a string key.
# (They should therefore not collide.)
result[int(record.user_id)] = entry
result[record.username] = entry
return result
示例2: obfuscate_wiki_entry
# 需要导入模块: import cjson [as 别名]
# 或者: from cjson import decode [as 别名]
def obfuscate_wiki_entry(self, line, user_profile):
fields = line.rstrip('\r\n').decode('utf8').split('\t')
record = ArticleRevisionRecord(*fields)
user_info = {}
if user_profile is not None:
user_id = record.user_id
if user_id != 'NULL':
profile_entry = user_profile.get(user_id)
if profile_entry is None:
log.error("Missing profile entry for user_id %s", user_id)
else:
user_info['name'] = [profile_entry.name, ]
if record.ip_address != 'NULL' and record.ip_address != 'ip_address':
log.warning("Found non-NULL IP address")
if record.automatic_log != '' and record.automatic_log != 'automatic_log':
log.warning(u"Found non-zero-length automatic_log: %s", record.automatic_log)
# Can't reset values, so update original fields.
fields[12] = backslash_encode_value(self.obfuscator.obfuscate_text(backslash_decode_value(record.content), user_info))
fields[2] = backslash_encode_value(self.obfuscator.obfuscate_text(backslash_decode_value(record.user_message), user_info))
return u"\t".join(fields).encode('utf-8')
示例3: row_iterator
# 需要导入模块: import cjson [as 别名]
# 或者: from cjson import decode [as 别名]
def row_iterator(key=key, get_data=get_data):
with t['blocked']:
data = get_data()
with t['process']:
for line in data.split('\n'):
if not line or line == '-':
continue
try:
row = FactRow(json_loads(line))
except ValueError, ve:
print >>sys.stderr, ve, repr(line)
else:
row['dt'] = datetime.datetime.utcfromtimestamp(row.get('ts', 0))
if start <= row['dt'] <= stop:
yield row
示例4: decode
# 需要导入模块: import cjson [as 别名]
# 或者: from cjson import decode [as 别名]
def decode(value):
from bson import json_util
return json_decode(value, encoding='utf-8', object_hook=json_util.object_hook)
示例5: verify
# 需要导入模块: import cjson [as 别名]
# 或者: from cjson import decode [as 别名]
def verify(jws, jwk, alg, validate_claims=True, expiry_seconds=None):
""" Verifies the given :class:`~jose.JWS`
:param jws: The :class:`~jose.JWS` to be verified.
:param jwk: A `dict` representing the JWK to use for verification. This
parameter is algorithm-specific.
:param alg: The algorithm to verify the signature with.
:param validate_claims: A `bool` indicating whether or not the `exp`, `iat`
and `nbf` claims should be validated. Defaults to
`True`.
:param expiry_seconds: An `int` containing the JWT expiry in seconds, used
when evaluating the `iat` claim. Defaults to `None`,
which disables `iat` claim validation.
:rtype: :class:`~jose.JWT`
:raises: :class:`~jose.Expired` if the JWT has expired
:raises: :class:`~jose.NotYetValid` if the JWT is not yet valid
:raises: :class:`~jose.Error` if there is an error decrypting the JWE
"""
header, payload, sig = map(b64decode_url, jws)
header = json_decode(header)
if alg != header[HEADER_ALG]:
raise Error('Invalid algorithm')
(_, verify_fn), mod = JWA[header[HEADER_ALG]]
if not verify_fn(_jws_hash_str(jws.header, jws.payload),
jwk['k'], sig, mod=mod):
raise Error('Mismatched signatures')
claims = json_decode(b64decode_url(jws.payload))
_validate(claims, validate_claims, expiry_seconds)
return JWT(header, claims)
示例6: get_stream
# 需要导入模块: import cjson [as 别名]
# 或者: from cjson import decode [as 别名]
def get_stream(self, url, params=None):
message = None
code = None
try:
self._response = self._session.get(url, stream=True, cert=self._cert, verify=self._verify)
self._response.raise_for_status()
except requests.exceptions.SSLError as ssl_error:
message = "SSL Certificate verification failed."
exception = ssl_error
except requests.exceptions.HTTPError as http_error:
message = "HTTP error."
exception = '{} (`{}`)'.format(
http_error, self._response.content.replace('\n', ' ').strip())
code = self._response.status_code
except requests.exceptions.Timeout as timeout_error:
message = "Connection timeout."
exception = timeout_error
except requests.exceptions.RequestException as req_error:
message = "Connection failed due to unknown problems."
exception = req_error
if message:
exc = APIClientException("{} {}".format(message, exception))
exc.code = code
raise exc
if self._response and self._response.status_code == requests.codes.ok:
for line in self._response.iter_lines(4096):
if line:
# NOTE: here we use cjson.decode() instead of
# stdlib's json.loads() -- because of the bug
# https://bugs.python.org/issue11489 which
# affects all pre-2.7.7 releases of CPython
yield cjson.decode(line)
示例7: serialize
# 需要导入模块: import cjson [as 别名]
# 或者: from cjson import decode [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')
示例8: deserialize
# 需要导入模块: import cjson [as 别名]
# 或者: from cjson import decode [as 别名]
def deserialize(cls, data):
if six.PY3:
return json.loads(data.decode('utf-8'))
else:
return json.loads(data.decode('utf-8'))
示例9: setUp
# 需要导入模块: import cjson [as 别名]
# 或者: from cjson import decode [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))
'''))
示例10: setUp
# 需要导入模块: import cjson [as 别名]
# 或者: from cjson import decode [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))
'''))
示例11: loads
# 需要导入模块: import cjson [as 别名]
# 或者: from cjson import decode [as 别名]
def loads(self):
return self.mapValues(lambda v: json.loads(v.decode('utf-8')))
示例12: loadsObj
# 需要导入模块: import cjson [as 别名]
# 或者: from cjson import decode [as 别名]
def loadsObj(self):
return self.mapValues(lambda v: AttrDict.loads(v.decode('utf-8')))
示例13: load_user_profile
# 需要导入模块: import cjson [as 别名]
# 或者: from cjson import decode [as 别名]
def load_user_profile(userprofile_path):
"""Loads a per-course dump of user-profile data (not a Sqoop dump)."""
result = {}
with open(userprofile_path, 'r') as infile:
for line in infile:
fields = line.rstrip('\r\n').decode('utf8').split('\t')
record = UserProfileRecord(*fields)
result[record.user_id] = record
return result
# Fields in custom "user-info" file (global, not per-course).
示例14: obfuscate_courseware_entry
# 需要导入模块: import cjson [as 别名]
# 或者: from cjson import decode [as 别名]
def obfuscate_courseware_entry(self, line, user_profile):
fields = line.rstrip('\r\n').decode('utf8').split('\t')
record = CoursewareRecord(*fields)
# Skip the header.
if record.state == 'state':
return line.rstrip('\r\n')
user_info = {}
if user_profile is not None:
user_id = record.student_id
if user_id != 'NULL':
profile_entry = user_profile.get(user_id)
if profile_entry is None:
self.missing_profile[user_id] += 1
else:
user_info['name'] = [profile_entry.name, ]
# TODO: also read in auth_user, and store username for each user_id.
pass
# 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.
state_str = record.state.replace('\\\\', '\\')
try:
state_dict = cjson.decode(state_str, all_unicode=True)
except Exception as exc:
log.exception(u"Unable to parse state as JSON for record %s: type = %s, state = %r", record.id, type(state_str), state_str)
return line
# Traverse the dictionary, looking for entries that need to be scrubbed.
updated_state_dict = self.obfuscator.obfuscate_structure(state_dict, u"state", user_info)
if updated_state_dict is not None:
# Can't reset values, so update original fields.
updated_state = json.dumps(updated_state_dict).replace('\\', '\\\\')
fields[4] = updated_state
log.info(u"Obfuscated state for user_id '%s' module_id '%s'", record.student_id, record.module_id)
return u"\t".join(fields).encode('utf-8')
示例15: obfuscate_forum_entry
# 需要导入模块: import cjson [as 别名]
# 或者: from cjson import decode [as 别名]
def obfuscate_forum_entry(self, line, user_profile):
# Round trip does not preserve content. Original had no embedded spaces,
# and entries were in alphabetic order. This is addressed by modifying the
# separators and setting sort_keys, but there are character encodings that
# are also different, as to when \u notation is used for a character as
# opposed to a utf8 encoding of the character.
try:
entry = cjson.decode(line, all_unicode=True)
except ValueError as exc:
log.error("Failed to parse json for line: %r", line)
return ""
# Get user information:
username = entry.get('author_username')
user_info = {'username': [username, ]}
profile_entry = None
if user_profile is not None:
user_id = entry.get('author_id')
profile_entry = user_profile.get(user_id)
if profile_entry is None:
log.error(u"Missing profile entry for user_id %s username %s", user_id, username)
else:
user_info['name'] = [profile_entry.name, ]
# Clean the body of the forum post.
body = entry['body']
clean_body = self.obfuscator.obfuscate_text(body, user_info)
entry['body'] = clean_body
# Also clean the title, since it also contains username and fullname matches.
if 'title' in entry:
title = entry['title']
clean_title = self.obfuscator.obfuscate_text(title, user_info)
entry['title'] = clean_title
return json.dumps(entry, ensure_ascii=False, sort_keys=True, separators=(',', ':')).encode('utf-8')
#####################