Python json.decoder() Examples

The following are code examples for showing how to use json.decoder(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don't like. You can also save this page to your account.

Example 1
Project: universe   Author: openai   File: utils.py    (license) View Source Project 6 votes vote down vote up
def split_buffer(stream, splitter=None, decoder=lambda a: a):
    """Given a generator which yields strings and a splitter function,
    joins all input, splits on the separator and yields each chunk.

    Unlike string.split(), each chunk includes the trailing
    separator, except for the last one if none was found on the end
    of the input.
    """
    splitter = splitter or line_splitter
    buffered = six.text_type('')

    for data in stream_as_text(stream):
        buffered += data
        while True:
            buffer_split = splitter(buffered)
            if buffer_split is None:
                break

            item, buffered = buffer_split
            yield item

    if buffered:
        yield decoder(buffered) 
Example 2
Project: goldmine   Author: Armored-Dragon   File: owner.py    (license) View Source Project 6 votes vote down vote up
def embed_from_json(self, ctx, *, js_text: str):
        """Send an embed from JSON.
        Usage: embed_from_json [json]"""
        echeck_perms(ctx, ('bot_owner',))
        class SemiEmbed:
            def __init__(self, obj):
                self.obj = obj
            def to_dict(self):
                return self.obj
        try:
            embed_obj = json.loads(js_text)
        except json.decoder.JSONDecodeError:
            await ctx.send(':warning: **Invalid JSON data!**')
        else:
            sembed = SemiEmbed(embed_obj)
            try:
                await ctx.send(embed=sembed)
            except discord.HTTPException as e:
                if '400' in str(e):
                    await ctx.send(':warning: **Couldn\'t send embed, check your data!**')
                else:
                    raise e 
Example 3
Project: cmdchallenge-site   Author: jarv   File: json_stream.py    (license) View Source Project 6 votes vote down vote up
def split_buffer(stream, splitter=None, decoder=lambda a: a):
    """Given a generator which yields strings and a splitter function,
    joins all input, splits on the separator and yields each chunk.
    Unlike string.split(), each chunk includes the trailing
    separator, except for the last one if none was found on the end
    of the input.
    """
    splitter = splitter or line_splitter
    buffered = six.text_type('')

    for data in stream_as_text(stream):
        buffered += data
        while True:
            buffer_split = splitter(buffered)
            if buffer_split is None:
                break

            item, buffered = buffer_split
            yield item

    if buffered:
        try:
            yield decoder(buffered)
        except Exception as e:
            raise StreamParseError(e) 
Example 4
Project: universe   Author: openai   File: utils.py    (license) View Source Project 5 votes vote down vote up
def json_splitter(buffer):
    """Attempt to parse a json object from a buffer. If there is at least one
    object, return it and the rest of the buffer, otherwise return None.
    """
    try:
        obj, index = json_decoder.raw_decode(buffer)
        rest = buffer[json.decoder.WHITESPACE.match(buffer, index).end():]
        return obj, rest
    except ValueError:
        return None 
Example 5
Project: zippy   Author: securesystemslab   File: __init__.py    (license) View Source Project 5 votes vote down vote up
def test_pyjson(self):
        self.assertEqual(self.json.scanner.make_scanner.__module__,
                         'json.scanner')
        self.assertEqual(self.json.decoder.scanstring.__module__,
                         'json.decoder')
        self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
                         'json.encoder') 
Example 6
Project: zippy   Author: securesystemslab   File: __init__.py    (license) View Source Project 5 votes vote down vote up
def test_cjson(self):
        self.assertEqual(self.json.scanner.make_scanner.__module__, '_json')
        self.assertEqual(self.json.decoder.scanstring.__module__, '_json')
        self.assertEqual(self.json.encoder.c_make_encoder.__module__, '_json')
        self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
                         '_json') 
Example 7
Project: zippy   Author: securesystemslab   File: __init__.py    (license) View Source Project 5 votes vote down vote up
def additional_tests():
    suite = unittest.TestSuite()
    for mod in (json, json.encoder, json.decoder):
        suite.addTest(doctest.DocTestSuite(mod))
    suite.addTest(TestPyTest('test_pyjson'))
    suite.addTest(TestCTest('test_cjson'))
    return suite 
Example 8
Project: django-modern-rpc   Author: alorence   File: jsonhandler.py    (license) View Source Project 5 votes vote down vote up
def loads(self, str_data):
        try:
            decoder = import_string(settings.MODERNRPC_JSON_DECODER)
            return json.loads(str_data, cls=decoder)

        except JSONDecodeError as e:
            raise RPCParseError(str(e)) 
Example 9
Project: oil   Author: oilshell   File: __init__.py    (license) View Source Project 5 votes vote down vote up
def test_pyjson(self):
        self.assertEqual(self.json.scanner.make_scanner.__module__,
                         'json.scanner')
        self.assertEqual(self.json.decoder.scanstring.__module__,
                         'json.decoder')
        self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
                         'json.encoder') 
Example 10
Project: oil   Author: oilshell   File: __init__.py    (license) View Source Project 5 votes vote down vote up
def test_cjson(self):
        self.assertEqual(self.json.scanner.make_scanner.__module__, '_json')
        self.assertEqual(self.json.decoder.scanstring.__module__, '_json')
        self.assertEqual(self.json.encoder.c_make_encoder.__module__, '_json')
        self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
                         '_json') 
Example 11
Project: oil   Author: oilshell   File: __init__.py    (license) View Source Project 5 votes vote down vote up
def additional_tests():
    suite = unittest.TestSuite()
    for mod in (json, json.encoder, json.decoder):
        suite.addTest(doctest.DocTestSuite(mod))
    suite.addTest(TestPyTest('test_pyjson'))
    suite.addTest(TestCTest('test_cjson'))
    return suite 
Example 12
Project: python2-tracer   Author: extremecoders-re   File: __init__.py    (license) View Source Project 5 votes vote down vote up
def test_pyjson(self):
        self.assertEqual(self.json.scanner.make_scanner.__module__,
                         'json.scanner')
        self.assertEqual(self.json.decoder.scanstring.__module__,
                         'json.decoder')
        self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
                         'json.encoder') 
Example 13
Project: python2-tracer   Author: extremecoders-re   File: __init__.py    (license) View Source Project 5 votes vote down vote up
def test_cjson(self):
        self.assertEqual(self.json.scanner.make_scanner.__module__, '_json')
        self.assertEqual(self.json.decoder.scanstring.__module__, '_json')
        self.assertEqual(self.json.encoder.c_make_encoder.__module__, '_json')
        self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
                         '_json') 
Example 14
Project: python2-tracer   Author: extremecoders-re   File: __init__.py    (license) View Source Project 5 votes vote down vote up
def additional_tests():
    suite = unittest.TestSuite()
    for mod in (json, json.encoder, json.decoder):
        suite.addTest(doctest.DocTestSuite(mod))
    suite.addTest(TestPyTest('test_pyjson'))
    suite.addTest(TestCTest('test_cjson'))
    return suite 
Example 15
Project: web_ctp   Author: molebot   File: __init__.py    (license) View Source Project 5 votes vote down vote up
def test_pyjson(self):
        self.assertEqual(self.json.scanner.make_scanner.__module__,
                         'json.scanner')
        self.assertEqual(self.json.decoder.scanstring.__module__,
                         'json.decoder')
        self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
                         'json.encoder') 
Example 16
Project: web_ctp   Author: molebot   File: __init__.py    (license) View Source Project 5 votes vote down vote up
def test_cjson(self):
        self.assertEqual(self.json.scanner.make_scanner.__module__, '_json')
        self.assertEqual(self.json.decoder.scanstring.__module__, '_json')
        self.assertEqual(self.json.encoder.c_make_encoder.__module__, '_json')
        self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
                         '_json') 
Example 17
Project: web_ctp   Author: molebot   File: __init__.py    (license) View Source Project 5 votes vote down vote up
def additional_tests():
    suite = unittest.TestSuite()
    for mod in (json, json.encoder, json.decoder):
        suite.addTest(doctest.DocTestSuite(mod))
    suite.addTest(TestPyTest('test_pyjson'))
    suite.addTest(TestCTest('test_cjson'))
    return suite 
Example 18
Project: cmdchallenge-site   Author: jarv   File: json_stream.py    (license) View Source Project 5 votes vote down vote up
def json_splitter(buffer):
    """Attempt to parse a json object from a buffer. If there is at least one
    object, return it and the rest of the buffer, otherwise return None.
    """
    buffer = buffer.strip()
    try:
        obj, index = json_decoder.raw_decode(buffer)
        rest = buffer[json.decoder.WHITESPACE.match(buffer, index).end():]
        return obj, rest
    except ValueError:
        return None 
Example 19
Project: taktyk   Author: kosior   File: auth.py    (license) View Source Project 5 votes vote down vote up
def log_in_for_userkey():
    headers = {}
    url = settings.API_LOGIN_URL + settings.APPKEY
    provided_api_app = settings.SECRETKEY and settings.ACCOUNTKEY

    while True:
        username = get_username()

        if provided_api_app:
            data = {'login': username, 'accountkey': settings.ACCOUNTKEY}
            headers = apisign(url, settings.SECRETKEY, **data)
            msg = 'Nieprawid?owy login, accountkey, lub secretkey.'
        else:
            password = get_password()
            data = {'login': username, 'password': password}
            msg = 'Nieprawid?owy login lub has?o.'

        response = requests.post(url, data=data, headers=headers)

        password = None
        data = None

        try:
            userkey = response.json()['userkey']
        except (json.decoder.JSONDecodeError, KeyError):
            logging.error(msg)
            logging.debug(traceback.format_exc())
            if provided_api_app:
                raise SystemExit
            reset_credentials()
            continue
        else:
            settings.USERKEY = userkey
            save_userkey(userkey)
            break 
Example 20
Project: pefile.pypy   Author: cloudtracer   File: __init__.py    (license) View Source Project 5 votes vote down vote up
def test_pyjson(self):
        self.assertEqual(self.json.scanner.make_scanner.__module__,
                         'json.scanner')
        self.assertEqual(self.json.decoder.scanstring.__module__,
                         'json.decoder')
        self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
                         'json.encoder') 
Example 21
Project: pefile.pypy   Author: cloudtracer   File: __init__.py    (license) View Source Project 5 votes vote down vote up
def test_cjson(self):
        self.assertEqual(self.json.scanner.make_scanner.__module__, '_json')
        self.assertEqual(self.json.decoder.scanstring.__module__, '_json')
        self.assertEqual(self.json.encoder.c_make_encoder.__module__, '_json')
        self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
                         '_json') 
Example 22
Project: pefile.pypy   Author: cloudtracer   File: __init__.py    (license) View Source Project 5 votes vote down vote up
def additional_tests():
    suite = unittest.TestSuite()
    for mod in (json, json.encoder, json.decoder):
        suite.addTest(doctest.DocTestSuite(mod))
    suite.addTest(TestPyTest('test_pyjson'))
    suite.addTest(TestCTest('test_cjson'))
    return suite 
Example 23
Project: ouroboros   Author: pybee   File: __init__.py    (license) View Source Project 5 votes vote down vote up
def test_pyjson(self):
        self.assertEqual(self.json.scanner.make_scanner.__module__,
                         'json.scanner')
        self.assertEqual(self.json.decoder.scanstring.__module__,
                         'json.decoder')
        self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
                         'json.encoder') 
Example 24
Project: ouroboros   Author: pybee   File: __init__.py    (license) View Source Project 5 votes vote down vote up
def test_cjson(self):
        self.assertEqual(self.json.scanner.make_scanner.__module__, '_json')
        self.assertEqual(self.json.decoder.scanstring.__module__, '_json')
        self.assertEqual(self.json.encoder.c_make_encoder.__module__, '_json')
        self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
                         '_json') 
Example 25
Project: ouroboros   Author: pybee   File: __init__.py    (license) View Source Project 5 votes vote down vote up
def load_tests(loader, _, pattern):
    suite = unittest.TestSuite()
    for mod in (json, json.encoder, json.decoder):
        suite.addTest(doctest.DocTestSuite(mod))
    suite.addTest(TestPyTest('test_pyjson'))
    suite.addTest(TestCTest('test_cjson'))

    pkg_dir = os.path.dirname(__file__)
    return support.load_package_tests(pkg_dir, loader, suite, pattern) 
Example 26
Project: ndk-python   Author: gittor   File: __init__.py    (license) View Source Project 5 votes vote down vote up
def test_pyjson(self):
        self.assertEqual(self.json.scanner.make_scanner.__module__,
                         'json.scanner')
        self.assertEqual(self.json.decoder.scanstring.__module__,
                         'json.decoder')
        self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
                         'json.encoder') 
Example 27
Project: ndk-python   Author: gittor   File: __init__.py    (license) View Source Project 5 votes vote down vote up
def test_cjson(self):
        self.assertEqual(self.json.scanner.make_scanner.__module__, '_json')
        self.assertEqual(self.json.decoder.scanstring.__module__, '_json')
        self.assertEqual(self.json.encoder.c_make_encoder.__module__, '_json')
        self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
                         '_json') 
Example 28
Project: ndk-python   Author: gittor   File: __init__.py    (license) View Source Project 5 votes vote down vote up
def additional_tests():
    suite = unittest.TestSuite()
    for mod in (json, json.encoder, json.decoder):
        suite.addTest(doctest.DocTestSuite(mod))
    suite.addTest(TestPyTest('test_pyjson'))
    suite.addTest(TestCTest('test_cjson'))
    return suite 
Example 29
Project: qgis-geogiglight-plugin   Author: boundlessgeo   File: layertracking.py    (license) View Source Project 5 votes vote down vote up
def decoder(jsonobj):
    if 'source' in jsonobj:
        return TrackedLayer(jsonobj['source'],
                            jsonobj['repoUrl'])
    else:
        return jsonobj 
Example 30
Project: qgis-geogiglight-plugin   Author: boundlessgeo   File: layertracking.py    (license) View Source Project 5 votes vote down vote up
def readTrackedLayers():
    try:
        global tracked
        filename = os.path.join(userFolder(), "trackedlayers")
        if os.path.exists(filename):
            with open(filename) as f:
                lines = f.readlines()
            jsonstring = "\n".join(lines)
            if jsonstring:
                tracked = JSONDecoder(object_hook = decoder).decode(jsonstring)
    except KeyError:
        pass 
Example 31
Project: kbe_server   Author: xiaohaoppy   File: __init__.py    (license) View Source Project 5 votes vote down vote up
def test_pyjson(self):
        self.assertEqual(self.json.scanner.make_scanner.__module__,
                         'json.scanner')
        self.assertEqual(self.json.decoder.scanstring.__module__,
                         'json.decoder')
        self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
                         'json.encoder') 
Example 32
Project: kbe_server   Author: xiaohaoppy   File: __init__.py    (license) View Source Project 5 votes vote down vote up
def test_cjson(self):
        self.assertEqual(self.json.scanner.make_scanner.__module__, '_json')
        self.assertEqual(self.json.decoder.scanstring.__module__, '_json')
        self.assertEqual(self.json.encoder.c_make_encoder.__module__, '_json')
        self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
                         '_json') 
Example 33
Project: kbe_server   Author: xiaohaoppy   File: __init__.py    (license) View Source Project 5 votes vote down vote up
def load_tests(loader, _, pattern):
    suite = unittest.TestSuite()
    for mod in (json, json.encoder, json.decoder):
        suite.addTest(doctest.DocTestSuite(mod))
    suite.addTest(TestPyTest('test_pyjson'))
    suite.addTest(TestCTest('test_cjson'))

    pkg_dir = os.path.dirname(__file__)
    return support.load_package_tests(pkg_dir, loader, suite, pattern)