def test_to_json_should_handle_attributes_containing_lists_of_dicts(self):
     children = [{'name': 'child1'}, {'name': 'child2'}]
     res = activeresource.ActiveResource()
     res.children = children
     json = res.to_json()
     parsed = util.json_to_dict(json)
     self.assertEqual(children, parsed['active_resource']['children'])
 def test_to_json_should_handle_attributes_containing_lists_of_dicts(self):
     children = [{'name': 'child1'}, {'name': 'child2'}]
     res = activeresource.ActiveResource()
     res.children = children
     json = res.to_json()
     parsed = util.json_to_dict(json.decode('utf-8'))
     self.assertEqual(children, parsed['active_resource']['children'])
Example #3
0
    def from_json(self, json_string):
        """Grab errors from a JSON response.

        Args:
            json_string: An json errors object (e.g. "{ 'errors': {} }")
        Returns:
            None
        """
        try:
            decoded = util.json_to_dict(json_string.decode('utf-8'))
        except ValueError:
            decoded = {}
        if not decoded:
            decoded = {}
        if isinstance(decoded, dict) and ('errors' in decoded
                                          or len(decoded) == 0):
            errors = decoded.get('errors', {})
            if isinstance(errors, list):
                # Deprecated in ActiveResource
                self.from_array(errors)
            else:
                self.from_hash(errors)
        else:
            # Deprecated in ActiveResource
            self.from_hash(decoded)
 def test_to_json_should_handle_attributes_containing_lists_of_strings(
         self):
     store = self.store({'name': 'foo', 'id': 1})
     store.websites = ['http://example.com', 'http://store.example.com']
     json = store.to_json()
     parsed = util.json_to_dict(json.decode('utf-8'))
     self.assertEqual(['http://example.com', 'http://store.example.com'],
                      parsed['store']['websites'])
Example #5
0
 def decode(resource_string):
     """Convert a resource string to a dictionary."""
     log = logging.getLogger('pyactiveresource.format')
     log.debug('decoding resource: %s', resource_string)
     try:
         data = util.json_to_dict(resource_string)
     except ValueError, err:
         raise Error(err)
Example #6
0
 def decode(resource_string):
     """Convert a resource string to a dictionary."""
     log = logging.getLogger('pyactiveresource.format')
     log.debug('decoding resource: %s', resource_string)
     try:
         data = util.json_to_dict(resource_string.decode('utf-8'))
     except ValueError as err:
         raise Error(err)
     return remove_root(data)
Example #7
0
    def test_json_to_dict_multiple_records(self):
        """Test the json to dict function."""
        topics_json = '''{
             "topics": [
               { "title": "The First Topic" },
               { "title": "The Second Topic" }
             ]
           }'''

        expected_topic_dicts = [
              { 'title': 'The First Topic' },
              { 'title': 'The Second Topic' },
            ]

        self.assertEqual(expected_topic_dicts,
                         util.json_to_dict(topics_json)['topics'])
    def test_json_to_dict_single_record(self):
        """Test the json_to_dict function."""
        topic_json = '''{
              "topic": {
                "title": "The First Topic",
                "author_name": "David",
                "id": 1,
                "approved": true,
                "replies_count": 0,
                "replies_close_in": 2592000000,
                "written_on": "2003-07-16",
                "viewed_at": "2003-07-16T09:28:00+0000",
                "content": "--- \\n1: should be an integer\\n:message: Have a nice day\\narray: \\n- should-have-dashes: true\\n  should_have_underscores: true\\n",
                "author_email_address": "*****@*****.**",
                "parent_id": null,
                "ad_revenue": 1.5,
                "optimum_viewing_angle": 135.0,
                "resident": "yes"
              }
            }'''

        expected_topic_dict = {
            'title': 'The First Topic',
            'author_name': 'David',
            'id': 1,
            'approved': True,
            'replies_count': 0,
            'replies_close_in': 2592000000,
            'written_on': "2003-07-16",
            'viewed_at': "2003-07-16T09:28:00+0000",
            'content':
            "--- \n1: should be an integer\n:message: Have a nice day\narray: \n- should-have-dashes: true\n  should_have_underscores: true\n",
            'author_email_address': '*****@*****.**',
            'parent_id': None,
            'ad_revenue': 1.5,
            'optimum_viewing_angle': 135.0,
            'resident': 'yes'
        }

        self.assertEqual(expected_topic_dict,
                         util.json_to_dict(topic_json)['topic'])
    def test_json_to_dict_single_record(self):
        """Test the json_to_dict function."""
        topic_json = '''{
              "topic": {
                "title": "The First Topic",
                "author_name": "David",
                "id": 1,
                "approved": true,
                "replies_count": 0,
                "replies_close_in": 2592000000,
                "written_on": "2003-07-16",
                "viewed_at": "2003-07-16T09:28:00+0000",
                "content": "--- \\n1: should be an integer\\n:message: Have a nice day\\narray: \\n- should-have-dashes: true\\n  should_have_underscores: true\\n",
                "author_email_address": "*****@*****.**",
                "parent_id": null,
                "ad_revenue": 1.5,
                "optimum_viewing_angle": 135.0,
                "resident": "yes"
              }
            }'''

        expected_topic_dict = {
            'title': 'The First Topic',
            'author_name': 'David',
            'id': 1,
            'approved': True,
            'replies_count': 0,
            'replies_close_in': 2592000000,
            'written_on': "2003-07-16",
            'viewed_at': "2003-07-16T09:28:00+0000",
            'content': "--- \n1: should be an integer\n:message: Have a nice day\narray: \n- should-have-dashes: true\n  should_have_underscores: true\n",
            'author_email_address': '*****@*****.**',
            'parent_id': None,
            'ad_revenue': 1.5,
            'optimum_viewing_angle': 135.0,
            'resident': 'yes'
            }

        self.assertEqual(expected_topic_dict,
                         util.json_to_dict(topic_json)['topic'])
    def from_json(self, json_string):
        """Grab errors from a JSON response.

        Args:
            json_string: An json errors object (e.g. "{ 'errors': {} }")
        Returns:
            None
        """
        try:
            decoded = util.json_to_dict(json_string)
        except ValueError:
            decoded = {}
        if not decoded:
            decoded = {}
        if isinstance(decoded, dict) and (decoded.has_key("errors") or len(decoded) == 0):
            errors = decoded.get("errors", {})
            if isinstance(errors, list):
                # Deprecated in ActiveResource
                self.from_array(errors)
            else:
                self.from_hash(errors)
        else:
            # Deprecated in ActiveResource
            self.from_hash(decoded)
 def test_to_json_should_handle_attributes_containing_lists_of_strings(self):
     store = self.store({'name': 'foo', 'id': 1})
     store.websites = ['http://example.com', 'http://store.example.com']
     json = store.to_json()
     parsed = util.json_to_dict(json)
     self.assertEqual(['http://example.com', 'http://store.example.com'], parsed['store']['websites'])