Python cjson.encode() Examples

The following are 14 code examples of cjson.encode(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module cjson , or try the search function .
Example #1
Source File: compat.py    From jsondb with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 
Example #2
Source File: jose.py    From jose with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def json_encode(data):
    return _json_encode(
        OrderedDict(sorted(data.items(), key=lambda item: item[0]))
    ) 
Example #3
Source File: serializers.py    From blitzdb with MIT License 5 votes vote down vote up
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') 
Example #4
Source File: serializers.py    From blitzdb with MIT License 5 votes vote down vote up
def serialize(cls, data):
            return cjson.encode(data) 
Example #5
Source File: external_modules.py    From script-languages with MIT License 5 votes vote down vote up
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))
                ''')) 
Example #6
Source File: external_modules.py    From script-languages with MIT License 5 votes vote down vote up
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))
                ''')) 
Example #7
Source File: json.py    From luscan-devel with GNU General Public License v2.0 5 votes vote down vote up
def dumps(obj, **kwargs):
        return encode(obj) 
Example #8
Source File: json.py    From luscan-devel with GNU General Public License v2.0 5 votes vote down vote up
def dump(obj, fp, **kwargs):
        fp.write( encode(obj) ) 
Example #9
Source File: timelock_mark_release_handler.py    From orisi with MIT License 5 votes vote down vote up
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)) 
Example #10
Source File: eventlog.py    From edx-analytics-pipeline with GNU Affero General Public License v3.0 5 votes vote down vote up
def encode_json(obj):
    """Wrapper to re-encode JSON string in an implementation-independent way."""
    # TODO: Verify correctness of cjson
    return cjson.encode(obj) 
Example #11
Source File: data_obfuscation.py    From edx-analytics-pipeline with GNU Affero General Public License v3.0 5 votes vote down vote up
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 
Example #12
Source File: data_obfuscation.py    From edx-analytics-pipeline with GNU Affero General Public License v3.0 5 votes vote down vote up
def filter_row(self, row):
        user_id = row[5]
        user_info = {}
        if user_id != 'NULL':
            user_id = int(user_id)
            user_info['user_id'] = [user_id, ]
            try:
                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 wiki user_id: %s in the user_by_id map", user_id)

        row[2] = ''  # user_message
        row[3] = ''  # automatic_log
        row[4] = ''  # ip_address
        # For user_id, preserve 'NULL' value if present.
        if user_id != 'NULL':
            row[5] = self.remap_id(user_id)

        wiki_content = backslash_decode_value(row[12].decode('utf8'))
        cleaned_content = self.obfuscator.obfuscate_text(wiki_content, user_info)
        row[12] = backslash_encode_value(cleaned_content).encode('utf8')

        return row 
Example #13
Source File: data_obfuscation.py    From edx-analytics-pipeline with GNU Affero General Public License v3.0 5 votes vote down vote up
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') 
Example #14
Source File: events_obfuscation.py    From edx-analytics-pipeline with GNU Affero General Public License v3.0 5 votes vote down vote up
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')