Python 3 Compatible JSON

In order to be really python3 compatible, the json lib was replaced
with oslo.serialization(1.10 or newer) module jsontuils since it's
the recommended migration to python3 guide.

https://wiki.openstack.org/wiki/Python3#Serialization:_base64.2C_JSON.2C_etc.

Change-Id: I2d8b62e642aba4ccd1b70be7e9b3784a95a6743d
Closes-Bug: #1629068
This commit is contained in:
Luong Anh Tuan 2016-11-15 14:36:46 +07:00 committed by Tuan Luong-Anh
parent f330dac837
commit ab41106cf6
5 changed files with 11 additions and 10 deletions

View File

@ -13,7 +13,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import json
import os import os
import time import time
@ -21,6 +20,7 @@ import netaddr
from oslo_concurrency import processutils from oslo_concurrency import processutils
from oslo_config import cfg from oslo_config import cfg
from oslo_log import log as logging from oslo_log import log as logging
from oslo_serialization import jsonutils
from oslo_utils import excutils from oslo_utils import excutils
from oslo_utils import units from oslo_utils import units
import requests import requests
@ -363,7 +363,7 @@ def collect_extra_hardware(data, failures):
return return
try: try:
data['data'] = json.loads(out) data['data'] = jsonutils.loads(out)
except ValueError as exc: except ValueError as exc:
msg = 'JSON returned from hardware-detect cannot be decoded: %s' msg = 'JSON returned from hardware-detect cannot be decoded: %s'
failures.add(msg, exc) failures.add(msg, exc)

View File

@ -12,9 +12,9 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import json
from oslo_log import log from oslo_log import log
from oslo_serialization import jsonutils
from oslo_service import loopingcall from oslo_service import loopingcall
import requests import requests
@ -72,7 +72,7 @@ class APIClient(object):
raise errors.HeartbeatError(str(e)) raise errors.HeartbeatError(str(e))
if response.status_code == requests.codes.CONFLICT: if response.status_code == requests.codes.CONFLICT:
data = json.loads(response.content) data = jsonutils.loads(response.content)
raise errors.HeartbeatConflictError(data.get('faultstring')) raise errors.HeartbeatConflictError(data.get('faultstring'))
elif response.status_code != requests.codes.ACCEPTED: elif response.status_code != requests.codes.ACCEPTED:
msg = 'Invalid status code: {}'.format(response.status_code) msg = 'Invalid status code: {}'.format(response.status_code)
@ -118,7 +118,7 @@ class APIClient(object):
return False return False
try: try:
content = json.loads(response.content) content = jsonutils.loads(response.content)
except Exception as e: except Exception as e:
LOG.warning('Error decoding response: %s', e) LOG.warning('Error decoding response: %s', e)
return False return False

View File

@ -12,13 +12,13 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import json
import socket import socket
import time import time
import mock import mock
from oslo_concurrency import processutils from oslo_concurrency import processutils
from oslo_config import cfg from oslo_config import cfg
from oslo_serialization import jsonutils
from oslotest import base as test_base from oslotest import base as test_base
import pkg_resources import pkg_resources
from stevedore import extension from stevedore import extension
@ -158,7 +158,8 @@ class TestBaseAgent(test_base.BaseTestCase):
# object. # object.
a_encoded = self.encoder.encode(a) a_encoded = self.encoder.encode(a)
b_encoded = self.encoder.encode(b) b_encoded = self.encoder.encode(b)
self.assertEqual(json.loads(a_encoded), json.loads(b_encoded)) self.assertEqual(jsonutils.loads(a_encoded),
jsonutils.loads(b_encoded))
def test_get_status(self): def test_get_status(self):
started_at = time.time() started_at = time.time()

View File

@ -12,9 +12,8 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import json
import mock import mock
from oslo_serialization import jsonutils
from oslo_service import loopingcall from oslo_service import loopingcall
from oslotest import base as test_base from oslotest import base as test_base
@ -28,7 +27,7 @@ API_URL = 'http://agent-api.ironic.example.org/'
class FakeResponse(object): class FakeResponse(object):
def __init__(self, content=None, status_code=200, headers=None): def __init__(self, content=None, status_code=200, headers=None):
content = content or {} content = content or {}
self.content = json.dumps(content) self.content = jsonutils.dumps(content)
self.status_code = status_code self.status_code = status_code
self.headers = headers or {} self.headers = headers or {}

View File

@ -9,6 +9,7 @@ netifaces>=0.10.4 # MIT
oslo.config!=3.18.0,>=3.14.0 # Apache-2.0 oslo.config!=3.18.0,>=3.14.0 # Apache-2.0
oslo.concurrency>=3.8.0 # Apache-2.0 oslo.concurrency>=3.8.0 # Apache-2.0
oslo.log>=3.11.0 # Apache-2.0 oslo.log>=3.11.0 # Apache-2.0
oslo.serialization>=1.10.0 # Apache-2.0
oslo.service>=1.10.0 # Apache-2.0 oslo.service>=1.10.0 # Apache-2.0
oslo.utils>=3.18.0 # Apache-2.0 oslo.utils>=3.18.0 # Apache-2.0
pecan!=1.0.2,!=1.0.3,!=1.0.4,!=1.2,>=1.0.0 # BSD pecan!=1.0.2,!=1.0.3,!=1.0.4,!=1.2,>=1.0.0 # BSD