Python json.JSONDecoder() Examples

The following are 30 code examples for showing how to use json.JSONDecoder(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module json , or try the search function .

Example 1
Project: coa_tools   Author: ndee85   File: addon_updater.py    License: GNU General Public License v3.0 6 votes vote down vote up
def get_api(self, url):
        # return the json version
        get = None
        get = self.get_raw(url)
        if get != None:
            try:
                return json.JSONDecoder().decode(get)
            except Exception as e:
                self._error = "API response has invalid JSON format"
                self._error_msg = str(e.reason)
                self._update_ready = None
                return None
        else:
            return None


    # create a working directory and download the new files 
Example 2
Project: numcodecs   Author: zarr-developers   File: json.py    License: MIT License 6 votes vote down vote up
def __init__(self, encoding='utf-8', skipkeys=False, ensure_ascii=True,
                 check_circular=True, allow_nan=True, sort_keys=True, indent=None,
                 separators=None, strict=True):
        self._text_encoding = encoding
        if separators is None:
            # ensure separators are explicitly specified, and consistent behaviour across
            # Python versions, and most compact representation if indent is None
            if indent is None:
                separators = ',', ':'
            else:
                separators = ', ', ': '
        separators = tuple(separators)
        self._encoder_config = dict(skipkeys=skipkeys, ensure_ascii=ensure_ascii,
                                    check_circular=check_circular, allow_nan=allow_nan,
                                    indent=indent, separators=separators,
                                    sort_keys=sort_keys)
        self._encoder = _json.JSONEncoder(**self._encoder_config)
        self._decoder_config = dict(strict=strict)
        self._decoder = _json.JSONDecoder(**self._decoder_config) 
Example 3
Project: bitmask-dev   Author: leapcode   File: utils.py    License: GNU General Public License v3.0 6 votes vote down vote up
def json_loads(data):
    """
    It works as json.loads but supporting multiple encodings in the same
    string and accepting an `str` parameter that won't be converted to unicode.

    :param data: the string to load the objects from
    :type data: str

    :returns: the corresponding python object result of parsing 'data', this
              behaves similarly as json.loads, with the exception of that
              returns always `str` instead of `unicode`.
    """
    obj = None
    with CustomJsonScanner():
        # We need to use the cls parameter in order to trigger the code
        # that will let us control the string parsing method.
        obj = json.loads(data, cls=json.JSONDecoder)

    return obj 
Example 4
Project: galaxy-sdk-python   Author: XiaoMi   File: TJSONProtocol.py    License: Apache License 2.0 6 votes vote down vote up
def readJSONString(self, skipContext):
    string = []
    if skipContext is False:
      self.context.read()
    self.readJSONSyntaxChar(QUOTE)
    while True:
      character = self.reader.read()
      if character == QUOTE:
        break
      if character == ESCSEQ[0]:
        character = self.reader.read()
        if character == ESCSEQ[1]:
          self.readJSONSyntaxChar(ZERO)
          self.readJSONSyntaxChar(ZERO)
          character = json.JSONDecoder().decode('"\u00%s"' % self.trans.read(2))
        else:
          off = ESCAPE_CHAR.find(character)
          if off == -1:
            raise TProtocolException(TProtocolException.INVALID_DATA,
                                     "Expected control char")
          character = ESCAPE_CHAR_VALS[off]
      string.append(character)
    return ''.join(string) 
Example 5
Project: standalone-linux-io-tracer   Author: Open-CAS   File: iotrace.py    License: BSD 3-Clause Clear License 6 votes vote down vote up
def __decode_json_stream(document, pos=0, decoder=json.JSONDecoder()):
    while True:
        # Create json stream without whitespace
        match = NOT_WHITESPACE.search(document, pos)

        if not match:
            # No more data
            return
        pos = match.start()

        try:
            obj, pos = decoder.raw_decode(document, pos)
        except json.JSONDecodeError:
            raise Exception("Invalid json formatting")

        yield obj 
Example 6
Project: python-sdk   Author: optimizely   File: condition.py    License: Apache License 2.0 6 votes vote down vote up
def object_hook(self, object_dict):
        """ Hook which when passed into a json.JSONDecoder will replace each dict
    in a json string with its index and convert the dict to an object as defined
    by the passed in condition_decoder. The newly created condition object is
    appended to the conditions_list.

    Args:
      object_dict: Dict representing an object.

    Returns:
      An index which will be used as the placeholder in the condition_structure
    """
        instance = self.decoder(object_dict)
        self.condition_list.append(instance)
        self.index += 1
        return self.index 
Example 7
Project: python-sdk   Author: optimizely   File: condition.py    License: Apache License 2.0 6 votes vote down vote up
def loads(conditions_string):
    """ Deserializes the conditions property into its corresponding
  components: the condition_structure and the condition_list.

  Args:
    conditions_string: String defining valid and/or conditions.

  Returns:
    A tuple of (condition_structure, condition_list).
    condition_structure: nested list of operators and placeholders for operands.
    condition_list: list of conditions whose index correspond to the values of the placeholders.
  """
    decoder = ConditionDecoder(_audience_condition_deserializer)

    # Create a custom JSONDecoder using the ConditionDecoder's object_hook method
    # to create the condition_structure as well as populate the condition_list
    json_decoder = json.JSONDecoder(object_hook=decoder.object_hook)

    # Perform the decoding
    condition_structure = json_decoder.decode(conditions_string)
    condition_list = decoder.condition_list

    return (condition_structure, condition_list) 
Example 8
Project: dimod   Author: dwavesystems   File: json.py    License: Apache License 2.0 6 votes vote down vote up
def dimod_object_hook(obj):
    """JSON-decoding for dimod objects.

    See Also:
        :class:`json.JSONDecoder` for using custom decoders.

    """
    if _is_sampleset_v2(obj):
        # in the future we could handle subtypes but right now we just have the
        # one
        return SampleSet.from_serializable(obj)
    elif _is_bqm(obj):
        # in the future we could handle subtypes but right now we just have the
        # one
        return BinaryQuadraticModel.from_serializable(obj)
    return obj 
Example 9
Project: airflow   Author: apache   File: variable_command.py    License: Apache License 2.0 6 votes vote down vote up
def _variable_export_helper(filepath):
    """Helps export all of the variables to the file"""
    var_dict = {}
    with create_session() as session:
        qry = session.query(Variable).all()

        data = json.JSONDecoder()
        for var in qry:
            try:
                val = data.decode(var.val)
            except Exception:  # pylint: disable=broad-except
                val = var.val
            var_dict[var.key] = val

    with open(filepath, 'w') as varfile:
        varfile.write(json.dumps(var_dict, sort_keys=True, indent=4))
    print("{} variables successfully exported to {}".format(len(var_dict), filepath)) 
Example 10
Project: continuous-integration   Author: bazelbuild   File: bazelci.py    License: Apache License 2.0 6 votes vote down vote up
def test_logs_for_status(bep_file, status):
    targets = []
    with open(bep_file, encoding="utf-8") as f:
        raw_data = f.read()
    decoder = json.JSONDecoder()

    pos = 0
    while pos < len(raw_data):
        try:
            bep_obj, size = decoder.raw_decode(raw_data[pos:])
        except ValueError as e:
            eprint("JSON decoding error: " + str(e))
            return targets
        if "testSummary" in bep_obj:
            test_target = bep_obj["id"]["testSummary"]["label"]
            test_status = bep_obj["testSummary"]["overallStatus"]
            if test_status in status:
                outputs = bep_obj["testSummary"]["failed"]
                test_logs = []
                for output in outputs:
                    test_logs.append(url2pathname(urlparse(output["uri"]).path))
                targets.append((test_target, test_logs))
        pos += size + 1
    return targets 
Example 11
Project: moviecatcher   Author: EvilCult   File: BdApi.py    License: MIT License 6 votes vote down vote up
def getPlayUrl (self, taskID) :
		taskID = str(taskID)
		bdApi = 'https://yun.baidu.com/rest/2.0/services/cloud_dl?need_task_info=1&status=255&start=0&limit=10&method=list_task&app_id=250528&channel=chunlei&web=1&app_id=250528&bdstoken={}&clienttype=0'.format(self.bdInfo['token'])
		html = self.Tools.getPage(bdApi, self.requestHeader)
		if html['code'] == '200' : 
			body =  html['body']
		else :
			body =  html['body']

		info = json.JSONDecoder().decode(body)
		taskInfo = info['task_info']

		try:
			for x in taskInfo :
				if taskID == x['task_id'] :
					savePath = urllib.parse.quote(x['save_path'].encode("UTF8"))
					playUrl = 'https://yun.baidu.com/play/video#video/path=' + savePath
					break 
		except Exception as e:
			playUrl = ''

		return (playUrl) 
Example 12
Project: SAKS-tutorials   Author: spoonysonny   File: main.py    License: GNU General Public License v2.0 6 votes vote down vote up
def get_pm25():
    global weather_url
    req = urllib2.Request(weather_url)
    resp = urllib2.urlopen(req)
    content = resp.read()
    if(content):
        weatherJSON = json.JSONDecoder().decode(content)
        #print(content)
        try:
            if weatherJSON['HeWeather data service 3.0'][0]['status'] == "ok":
                if weatherJSON['HeWeather data service 3.0'][0].has_key('aqi'):
                    print(weatherJSON['HeWeather data service 3.0'][0]['aqi']['city']['pm25'])
                    return int(weatherJSON['HeWeather data service 3.0'][0]['aqi']['city']['pm25'])
                else:
                    return -1
            else:
                return -1
        except:
            return -1 
Example 13
Project: SAKS-tutorials   Author: spoonysonny   File: main.py    License: GNU General Public License v2.0 6 votes vote down vote up
def get_pm25():
    global weather_url
    req = urllib2.Request(weather_url)
    resp = urllib2.urlopen(req, timeout = 5)
    content = resp.read()
    if(content):
        weatherJSON = json.JSONDecoder().decode(content)
        #print(content)
        try:
            if weatherJSON['HeWeather data service 3.0'][0]['status'] == "ok":
                if weatherJSON['HeWeather data service 3.0'][0].has_key('aqi'):
                    print(weatherJSON['HeWeather data service 3.0'][0]['aqi']['city']['pm25'])
                    return int(weatherJSON['HeWeather data service 3.0'][0]['aqi']['city']['pm25'])
                else:
                    return -1
            else:
                return -1
        except:
            return -1 
Example 14
Project: SAKS-tutorials   Author: spoonysonny   File: main.py    License: GNU General Public License v2.0 6 votes vote down vote up
def get_city_temp():
    global weather_url
    req = urllib2.Request(weather_url)
    resp = urllib2.urlopen(req, timeout = 5)
    content = resp.read()
    if(content):
        weatherJSON = json.JSONDecoder().decode(content)
        #print(content)
        try:
            if weatherJSON['HeWeather data service 3.0'][0]['status'] == "ok":
                if weatherJSON['HeWeather data service 3.0'][0].has_key('now'):
                    print(weatherJSON['HeWeather data service 3.0'][0]['now']['tmp'])
                    return int(weatherJSON['HeWeather data service 3.0'][0]['now']['tmp'])
                else:
                    return -128
            else:
                return -128
        except:
            return -128 
Example 15
Project: SAKS-tutorials   Author: spoonysonny   File: main.py    License: GNU General Public License v2.0 6 votes vote down vote up
def get_pm25():
    global weather_url
    req = urllib2.Request(weather_url)
    resp = urllib2.urlopen(req)
    content = resp.read()
    if(content):
        weatherJSON = json.JSONDecoder().decode(content)
        #print(content)
        try:
            if weatherJSON['HeWeather data service 3.0'][0]['status'] == "ok":
                if weatherJSON['HeWeather data service 3.0'][0].has_key('aqi'):
                    print(weatherJSON['HeWeather data service 3.0'][0]['aqi']['city']['pm25'])
                    return int(weatherJSON['HeWeather data service 3.0'][0]['aqi']['city']['pm25'])
                else:
                    return -1
            else:
                return -1
        except:
            return -1 
Example 16
Project: authenticator   Author: JeNeSuisPasDave   File: data.py    License: MIT License 6 votes vote down vote up
def __init__(self, **kw_args):
        """Compose the standard JSONDecoder with a custom object_hook.

        The custom object_hook will recognize a dictionary that represents
        a ClientData object, and decode it as a ClientData object. All other
        objects will get passed to the standard JSONDecoder.

        Args:
            Same arguments as JSONDecoder.__init__() with the exception that
            'strict' is always set to False. If an 'object_hook' is supplied
            then it will be called by _object_decode() if the object is
            not interpreted as ClientData.

        """
        self._other_object_hook = None
        kw_args_new = kw_args.copy()
        if 'object_hook' in kw_args:
            self._other_object_hook = kw_args['object_hook']
        kw_args_new['object_hook'] = self._object_decode
        # Note: strict=False because the notes attribute might contain
        #       line feeds.
        #
        kw_args_new['strict'] = False

        self._decoder = json.JSONDecoder(**kw_args_new) 
Example 17
Project: Screencast-Keys   Author: nutti   File: addon_updator.py    License: GNU General Public License v3.0 6 votes vote down vote up
def _request(url, json_decode=True):
    ssl._create_default_https_context = ssl._create_unverified_context
    req = urllib.request.Request(url)

    try:
        result = urllib.request.urlopen(req)
    except urllib.error.HTTPError as e:
        raise RuntimeError("HTTP error ({})".format(str(e.code)))
    except urllib.error.URLError as e:
        raise RuntimeError("URL error ({})".format(str(e.reason)))

    data = result.read()
    result.close()

    if json_decode:
        try:
            return json.JSONDecoder().decode(data.decode())
        except Exception as e:
            raise RuntimeError("API response has invalid JSON format ({})"
                               .format(str(e.reason)))

    return data.decode() 
Example 18
Project: plex_autoscan   Author: l3uddz   File: config.py    License: GNU General Public License v3.0 6 votes vote down vote up
def upgrade_settings(self, currents):
        fields_env = {}

        # ENV gets priority: ENV > config.json
        for name, data in self.base_config.items():
            if name in os.environ:
                # Use JSON decoder to get same behaviour as config file
                fields_env[name] = json.JSONDecoder().decode(os.environ[name])
                logger.info("Using ENV setting %s=%s", name, fields_env[name])

        # Update in-memory config with environment settings
        currents.update(fields_env)

        # Do inner upgrade
        upgraded_settings, upgraded = self.__inner_upgrade(self.base_config, currents)
        return upgraded_settings, upgraded 
Example 19
Project: civis-python   Author: civisanalytics   File: test_service_client.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_generate_classes_maybe_cached(url_mock, api_spec_mock,
                                       parse_mock, mock_swagger):
    api_spec_mock.return_value = {}
    mock_class_function = (lambda client, return_type: "return")
    parse_mock.return_value = {'class': mock_class_function}
    url_mock.return_value = MOCK_URL

    sc = ServiceClient(MOCK_SERVICE_ID, root_path='/foo')

    mock_spec_str = str(json.dumps(mock_swagger))
    mock_spec = json.JSONDecoder(object_pairs_hook=OrderedDict).decode(mock_spec_str)  # noqa: E501
    classes = sc.generate_classes_maybe_cached(mock_spec)

    parse_mock.assert_has_calls([
        # the call from generate_classes_maybe_cached in ServiceClient.__init__
        mock.call({}, root_path='/foo'),
        # the call from generate_classes_maybe_cached in this test
        mock.call(mock_spec, root_path='/foo')
    ])

    assert 'class' in classes 
Example 20
Project: wagtail   Author: wagtail   File: test_pages.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_fields_ordering(self):
        response = self.get_response(type='demosite.BlogEntryPage', fields='date,title,feed_image,related_links')

        # Will crash if the JSON is invalid
        content = json.loads(response.content.decode('UTF-8'))

        # Test field order
        content = json.JSONDecoder(object_pairs_hook=collections.OrderedDict).decode(response.content.decode('UTF-8'))
        field_order = [
            'id',
            'meta',
            'title',
            'admin_display_title',
            'date',
            'feed_image',
            'related_links',
        ]
        self.assertEqual(list(content['items'][0].keys()), field_order) 
Example 21
Project: wagtail   Author: wagtail   File: test_pages.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_field_ordering(self):
        # Need to override this as the admin API has a __types field

        response = self.get_response(16)

        # Will crash if the JSON is invalid
        content = json.loads(response.content.decode('UTF-8'))

        # Test field order
        content = json.JSONDecoder(object_pairs_hook=collections.OrderedDict).decode(response.content.decode('UTF-8'))
        field_order = [
            'id',
            'meta',
            'title',
            'admin_display_title',
            'body',
            'tags',
            'date',
            'feed_image',
            'feed_image_thumbnail',
            'carousel_items',
            'related_links',
            '__types',
        ]
        self.assertEqual(list(content.keys()), field_order) 
Example 22
Project: wagtail   Author: wagtail   File: test_pages.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_fields_ordering(self):
        response = self.get_response(type='demosite.BlogEntryPage', fields='date,title,feed_image,related_links')

        # Will crash if the JSON is invalid
        content = json.loads(response.content.decode('UTF-8'))

        # Test field order
        content = json.JSONDecoder(object_pairs_hook=collections.OrderedDict).decode(response.content.decode('UTF-8'))
        field_order = [
            'id',
            'meta',
            'title',
            'date',
            'feed_image',
            'related_links',
        ]
        self.assertEqual(list(content['items'][0].keys()), field_order) 
Example 23
Project: wagtail   Author: wagtail   File: test_pages.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_field_ordering(self):
        response = self.get_response(16)

        # Will crash if the JSON is invalid
        content = json.loads(response.content.decode('UTF-8'))

        # Test field order
        content = json.JSONDecoder(object_pairs_hook=collections.OrderedDict).decode(response.content.decode('UTF-8'))
        field_order = [
            'id',
            'meta',
            'title',
            'body',
            'tags',
            'date',
            'feed_image',
            'feed_image_thumbnail',
            'carousel_items',
            'related_links',
        ]
        self.assertEqual(list(content.keys()), field_order) 
Example 24
Project: cloudplow   Author: l3uddz   File: config.py    License: GNU General Public License v3.0 6 votes vote down vote up
def upgrade_settings(self, currents):
        fields_env = {}

        # ENV gets priority: ENV > config.json
        for name, data in self.base_config.items():
            if name in os.environ:
                # Use JSON decoder to get same behaviour as config file
                fields_env[name] = json.JSONDecoder().decode(os.environ[name])
                log.info("Using ENV setting %s=%s", name, fields_env[name])

        # Update in-memory config with environment settings
        currents.update(fields_env)

        # Do inner upgrade
        upgraded_settings, upgraded = self.__inner_upgrade(self.base_config, currents)
        return upgraded_settings, upgraded 
Example 25
Project: cloudplow   Author: l3uddz   File: config.py    License: GNU General Public License v3.0 6 votes vote down vote up
def upgrade(self, cfg):
        fields = []
        fields_env = {}

        # ENV gets priority: ENV < config.json
        for name, data in self.base_config.items():
            if name not in cfg:
                cfg[name] = data
                fields.append(name)

            if name in os.environ:
                # Use JSON decoder to get same behaviour as config file
                fields_env[name] = json.JSONDecoder().decode(os.environ[name])
                log.info("Using ENV setting %s=%s", name, fields_env[name])

        # Only rewrite config file if new fields added
        if len(fields):
            log.info("Upgraded config. Added %d new field(s): %r", len(fields), fields)
            self.save(cfg)

        # Update in-memory config with environment settings
        cfg.update(fields_env)

        return cfg 
Example 26
Project: kaleidoscope   Author: blenderskool   File: addon_updater.py    License: GNU General Public License v3.0 6 votes vote down vote up
def get_api(self, url):
		# return the json version
		get = None
		get = self.get_raw(url)
		if get != None:
			try:
				return json.JSONDecoder().decode(get)
			except Exception as e:
				self._error = "API response has invalid JSON format"
				self._error_msg = str(e.reason)
				self._update_ready = None
				print(self._error, self._error_msg)
				return None
		else:
			return None


	# create a working directory and download the new files 
Example 27
Project: Cirq   Author: quantumlib   File: auto_merge.py    License: Apache License 2.0 6 votes vote down vote up
def from_github(repo: GithubRepository,
                    pull_id: int) -> 'PullRequestDetails':
        """
        References:
            https://developer.github.com/v3/pulls/#get-a-single-pull-request
        """
        url = ("https://api.github.com/repos/{}/{}/pulls/{}"
               "?access_token={}".format(repo.organization,
                                         repo.name,
                                         pull_id,
                                         repo.access_token))

        response = requests.get(url)

        if response.status_code != 200:
            raise RuntimeError(
                'Pull check failed. Code: {}. Content: {}.'.format(
                    response.status_code, response.content))

        payload = json.JSONDecoder().decode(response.content.decode())
        return PullRequestDetails(payload, repo) 
Example 28
Project: Cirq   Author: quantumlib   File: auto_merge.py    License: Apache License 2.0 6 votes vote down vote up
def check_collaborator_has_write(repo: GithubRepository, username: str
                                 ) -> Optional[CannotAutomergeError]:
    """
    References:
        https://developer.github.com/v3/issues/events/#list-events-for-an-issue
    """
    url = ("https://api.github.com/repos/{}/{}/collaborators/{}/permission"
           "?access_token={}".format(repo.organization,
                                     repo.name,
                                     username,
                                     repo.access_token))

    response = requests.get(url)

    if response.status_code != 200:
        raise RuntimeError(
            'Collaborator check failed. Code: {}. Content: {}.'.format(
                response.status_code, response.content))

    payload = json.JSONDecoder().decode(response.content.decode())
    if payload['permission'] not in ['admin', 'write']:
        return CannotAutomergeError(
            'Only collaborators with write permission can use automerge.')

    return None 
Example 29
Project: dustmaps   Author: gregreen   File: json_serializers.py    License: GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        json.JSONDecoder.__init__(
            self,
            object_hook=self.object_hook,
            *args,
            **kwargs) 
Example 30
Project: pysphere   Author: rnbguy   File: misphere.py    License: MIT License 5 votes vote down vote up
def recv_handler(self):
        while self.recv_handle_live:
            r, _, _ = select.select([self.socket], [], [], 0.5)
            if r:
                resps = self.socket.recv(512).decode()
                while True:
                    resp_dict, idx = json.JSONDecoder().raw_decode(resps)
                    self.resp_handler(resp_dict)
                    if idx >= len(resps):
                        break
                    else:
                        resps = resps[idx:]