Python json.JSONDecoder() Examples

The following are 30 code examples of json.JSONDecoder(). 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 json , or try the search function .
Example #1
Source File: test_pages.py    From wagtail with 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 #2
Source File: auto_merge.py    From Cirq with 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 #3
Source File: auto_merge.py    From Cirq with 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 #4
Source File: main.py    From SAKS-tutorials with 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 #5
Source File: BdApi.py    From moviecatcher with 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 #6
Source File: addon_updater.py    From kaleidoscope with 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 #7
Source File: TJSONProtocol.py    From galaxy-sdk-python with 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 #8
Source File: main.py    From SAKS-tutorials with 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 #9
Source File: main.py    From SAKS-tutorials with 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 #10
Source File: config.py    From cloudplow with 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 #11
Source File: config.py    From cloudplow with 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 #12
Source File: utils.py    From bitmask-dev with 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 #13
Source File: test_pages.py    From wagtail with 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 #14
Source File: test_pages.py    From wagtail with 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 #15
Source File: test_pages.py    From wagtail with 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 #16
Source File: main.py    From SAKS-tutorials with 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 #17
Source File: data.py    From authenticator with 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 #18
Source File: iotrace.py    From standalone-linux-io-tracer with 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 #19
Source File: condition.py    From python-sdk with 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 #20
Source File: condition.py    From python-sdk with 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 #21
Source File: json.py    From dimod with 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 #22
Source File: addon_updator.py    From Screencast-Keys with 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 #23
Source File: bazelci.py    From continuous-integration with 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 #24
Source File: test_service_client.py    From civis-python with 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 #25
Source File: variable_command.py    From airflow with 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 #26
Source File: config.py    From plex_autoscan with 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 #27
Source File: response.py    From pylovepdf with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, response):

        self.status = response.status_code
        try:
            headers = response.headers['content-type']
            if 'application/pdf' in headers:
                setattr(self, 'headers', response.headers)
                setattr(self, 'iter_content', response.iter_content)
            elif 'application/zip' in headers:
                setattr(self, 'headers', response.headers)
                setattr(self, 'iter_content', response.iter_content)
            elif 'image/jpeg'in headers:
                setattr(self, 'headers', response.headers)
                setattr(self, 'iter_content', response.iter_content)
            else:
                raise KeyError
        except KeyError:
            try:
                params = json.JSONDecoder().decode(response.text)

                if params:
                    for attr, value in params.items():
                        setattr(self, attr, value)
                        
            except json.decoder.JSONDecodeError:
                pass 
Example #28
Source File: JsonTree.py    From JsonTree with MIT License 5 votes vote down vote up
def run(self, edit):
        self.keys = []
        self.trimkeys = []
        content = self.view.substr(sublime.Region(0, self.view.size()))
        try:
            #json_data = json.loads(content)
            json_data = json.JSONDecoder(object_pairs_hook=OrderedDict).decode(content)
            self.fill_keys(json_data)
            sublime.active_window().show_quick_panel(self.keys, self.goto)
        except Exception as ex:
            print(ex) 
Example #29
Source File: auto_merge.py    From Cirq with Apache License 2.0 5 votes vote down vote up
def check_auto_merge_labeler(repo: GithubRepository, pull_id: int
                             ) -> Optional[CannotAutomergeError]:
    """
    References:
        https://developer.github.com/v3/issues/events/#list-events-for-an-issue
    """
    url = ("https://api.github.com/repos/{}/{}/issues/{}/events"
           "?access_token={}".format(repo.organization,
                                     repo.name,
                                     pull_id,
                                     repo.access_token))

    response = requests.get(url)

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

    payload = json.JSONDecoder().decode(response.content.decode())
    relevant = [event
                for event in payload
                if event['event'] == 'labeled' and
                event['label']['name'] in AUTO_MERGE_LABELS]
    if not relevant:
        return CannotAutomergeError('"automerge" label was never added.')

    return check_collaborator_has_write(repo, relevant[-1]['actor']['login']) 
Example #30
Source File: utils.py    From nbodykit with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        kwargs['object_hook'] = JSONDecoder.hook
        json.JSONDecoder.__init__(self, *args, **kwargs)