Update json module to jsonutils

json is deprecated, should use oslo_serialization.jsonutils
instead.

Change-Id: I69feb6639b0d5ec66765c00719154e87304f52dc
This commit is contained in:
zhulingjie 2019-02-17 17:59:34 +08:00
parent a0210ab9c1
commit fd2b8ff89f
10 changed files with 45 additions and 43 deletions

View File

@ -40,9 +40,10 @@ In the case of neutron networks, the initial data will come from
``neutronclient``. Some browsing of the API documentation reveals that the
call I want is ``list_networks``::
import json
import os
from oslo_serialization import jsonutils
from keystoneclient.auth.identity import v2
from keystoneclient import session
from neutronclient.v2_0 import client as nc_20
@ -59,7 +60,7 @@ call I want is ``list_networks``::
nc = nc_20.Client(session=get_session())
networks = nc.list_networks()
print(json.dumps(networks, indent=4, sort_keys=True))
print(jsonutils.dumps(networks, indent=4, sort_keys=True))
This outputs::

View File

@ -13,11 +13,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from elasticsearch import exceptions as es_exc
from oslo_config import cfg
from oslo_log import log as logging
from oslo_serialization import jsonutils
from oslo_utils import encodeutils
import six
import webob.exc
@ -553,17 +552,17 @@ class ResponseSerializer(wsgi.JSONResponseSerializer):
self.schema = schema
def search(self, response, query_result):
body = json.dumps(query_result, ensure_ascii=False)
body = jsonutils.dumps(query_result, ensure_ascii=False)
response.unicode_body = six.text_type(body)
response.content_type = 'application/json'
def plugins_info(self, response, query_result):
body = json.dumps(query_result, ensure_ascii=False)
body = jsonutils.dumps(query_result, ensure_ascii=False)
response.unicode_body = six.text_type(body)
response.content_type = 'application/json'
def facets(self, response, query_result):
body = json.dumps(query_result, ensure_ascii=False)
body = jsonutils.dumps(query_result, ensure_ascii=False)
response.unicode_body = six.text_type(body)
response.content_type = 'application/json'

View File

@ -14,12 +14,13 @@
# limitations under the License.
import copy
import json
import logging
import novaclient.exceptions
import novaclient.v2.flavors
import six
from oslo_serialization import jsonutils
from searchlight.elasticsearch.plugins import openstack_clients
from searchlight.elasticsearch.plugins import utils
@ -196,7 +197,7 @@ def serialize_nova_hypervisor(hypervisor, updated_at=None):
# to JSON object in microversion 2.28, we should be able to
# deal with JSON object here.
if not isinstance(serialized['cpu_info'], dict):
serialized['cpu_info'] = json.loads(serialized['cpu_info'])
serialized['cpu_info'] = jsonutils.loads(serialized['cpu_info'])
if not getattr(hypervisor, 'updated_at', None):
serialized['updated_at'] = updated_at or utils.get_now_str()
# TODO(lyj): Remove this once hypervisor notifications supported.

View File

@ -26,7 +26,6 @@ import datetime
import elasticsearch
import httplib2
import importlib
import json
import logging
import mock
import os
@ -597,8 +596,9 @@ class FunctionalTest(test_utils.BaseTestCase):
def _load_fixture_data(self, name):
base_dir = "searchlight/tests/functional/data"
with open(os.path.join(base_dir, name), 'r') as f:
return json.load(f)
# binary mode is needed due to bug/1515231
with open(os.path.join(base_dir, name), 'r+b') as f:
return jsonutils.load(f)
def set_policy_rules(self, rules):
fap = open(self.policy_file, 'w')

View File

@ -14,7 +14,8 @@
# limitations under the License.
import os
import simplejson as json
from oslo_serialization import jsonutils
from glanceclient.v2 import client as glance
from keystoneauth1 import session
@ -78,7 +79,7 @@ def get_glance_images_and_members_with_pyclient():
glance_client = get_glanceclient()
images = glance_client.images.list()
images_json = json.dumps(list(images), indent=4)
images_json = jsonutils.dumps(list(images), indent=4)
with open(IMAGES_FILE, "w") as f:
f.write(images_json)
@ -92,7 +93,7 @@ def get_glance_images_and_members_with_pyclient():
image_members_list = list(image_members)
if len(image_members_list) > 0:
image_members_dict[image['id']] = image_members_list
image_members_json = json.dumps(image_members_dict, indent=4)
image_members_json = jsonutils.dumps(image_members_dict, indent=4)
with open(IMAGE_MEMBERS_FILE, "w") as f:
f.write(image_members_json)
@ -108,7 +109,7 @@ def get_glance_metadefs_with_pyclient():
namespace['namespace'])
namespace_list.append(_namespace)
metadef_namespace_json = json.dumps(namespace_list, indent=4)
metadef_namespace_json = jsonutils.dumps(namespace_list, indent=4)
with open(METADEFS_FILE, "w") as f:
f.write(metadef_namespace_json)
@ -121,7 +122,7 @@ def get_nova_servers_with_pyclient():
servers_list = []
for each in servers:
servers_list.append(each.to_dict())
servers_json = json.dumps(list(servers_list), indent=4)
servers_json = jsonutils.dumps(list(servers_list), indent=4)
with open(SERVERS_FILE, "w") as f:
f.write(servers_json)
@ -134,7 +135,7 @@ def get_nova_flavors_with_pyclient():
flavor_dict.pop("links")
flavor_dict.update({"tenant_id": _get_flavor_tenant(flavor)})
flavor_dict.update({"extra_spec": flavor.get_keys()})
flavors_json = json.dumps([flavor_dict], indent=4)
flavors_json = jsonutils.dumps([flavor_dict], indent=4)
with open(FLAVORS_FILE, "w") as f:
f.write(flavors_json)
@ -146,7 +147,8 @@ def get_nova_server_groups_with_pyclient():
server_groups_list = []
for each in server_groups:
server_groups_list.append(each.to_dict())
server_groups_json = json.dumps(list(server_groups_list), indent=4)
server_groups_json = jsonutils.dumps(list(server_groups_list),
indent=4)
with open(SERVERS_FILE, "w") as f:
f.write(server_groups_json)

View File

@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from oslo_serialization import jsonutils
from searchlight.tests.functional import generate_load_data
@ -107,8 +107,8 @@ class FakeGlanceClient(object):
def __init__(self):
# Load Images from file
self._images = []
with open(generate_load_data.IMAGES_FILE, "r") as file:
image_data = json.load(file)
with open(generate_load_data.IMAGES_FILE, "r+b") as file:
image_data = jsonutils.load(file)
for image in image_data:
fake_image = FakeImage(**image)
self._images.append(fake_image)
@ -117,8 +117,8 @@ class FakeGlanceClient(object):
# Load Images members from file
self._images_members_dict = dict()
self._image_members_list = []
with open(generate_load_data.IMAGE_MEMBERS_FILE, "r") as file:
image_members_data = json.load(file)
with open(generate_load_data.IMAGE_MEMBERS_FILE, "r+b") as file:
image_members_data = jsonutils.load(file)
for image_id, image_members in image_members_data.items():
for image_member in image_members:
fake_image_member = FakeImageMember(**image_member)
@ -129,8 +129,8 @@ class FakeGlanceClient(object):
# Load Metadef namespaces from file
self._metadefs_namespace = []
self.metadefs_namespace = []
with open(generate_load_data.METADEFS_FILE, "r") as file:
metadefs_namespace_data = json.load(file)
with open(generate_load_data.METADEFS_FILE, "r+b") as file:
metadefs_namespace_data = jsonutils.load(file)
for metadef_namespace in metadefs_namespace_data:
fake_namespace = FakeNamespace(**metadef_namespace)
self._metadefs_namespace.append(fake_namespace)

View File

@ -13,12 +13,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import mock
import os
import six
import time
from oslo_serialization import jsonutils
from oslo_utils import uuidutils
from searchlight.elasticsearch import ROLE_USER_FIELD
@ -48,13 +48,13 @@ class TestSearchApi(functional.FunctionalTest):
it can use plugins for the sake of making requests
"""
def _modify_policy_file(self, rules):
with open(self.policy_file, 'r') as policy_file:
existing_policy = json.load(policy_file)
with open(self.policy_file, 'r+b') as policy_file:
existing_policy = jsonutils.load(policy_file)
existing_policy.update(rules)
with open(self.policy_file, 'w') as policy_file:
json.dump(existing_policy, policy_file)
jsonutils.dump(existing_policy, policy_file)
time.sleep(2)
@ -1025,7 +1025,7 @@ class TestSearchApi(functional.FunctionalTest):
class TestServerServicePolicies(functional.FunctionalTest):
def _write_policy_file(self, filename, rules):
with open(os.path.join(self.conf_dir, filename), 'w') as pol_file:
json.dump(rules, pol_file)
jsonutils.dump(rules, pol_file)
def _additional_server_config(self):
"""Create some service policy files"""

View File

@ -14,9 +14,9 @@
# limitations under the License.
import copy
import json
import mock
from oslo_serialization import jsonutils
from oslo_utils import uuidutils
from searchlight.listener import NotificationEndpoint
@ -784,14 +784,14 @@ class TestGlanceLoad(functional.FunctionalTest):
self.metadefs_plugin = self.initialized_plugins['OS::Glance::Metadef']
def _get_glance_image_owner_and_count(self):
with open(generate_load_data.IMAGES_FILE, "r") as file:
images_data = json.load(file)
with open(generate_load_data.IMAGES_FILE, "r+b") as file:
images_data = jsonutils.load(file)
if len(images_data) > 0:
return len(images_data), images_data[0]['owner']
def _get_glance_metadefs_owner_and_count(self):
with open(generate_load_data.METADEFS_FILE, "r") as file:
metadefs_data = json.load(file)
with open(generate_load_data.METADEFS_FILE, "r+b") as file:
metadefs_data = jsonutils.load(file)
if len(metadefs_data) > 0:
return len(metadefs_data), metadefs_data[0]['owner']

View File

@ -16,7 +16,6 @@
# limitations under the License.
import json
from keystoneclient.auth.identity import v2
from keystoneclient import session
import os
@ -26,6 +25,8 @@ import string
import swiftclient
import sys
from oslo_serialization import jsonutils
container_base_name = "scale_"
object_base_name = "object_"
object_meta_choices = [None, 'These', 'Are', 'Some', 'Random', 'Words']
@ -43,7 +44,7 @@ object_contents = (
transform="translate(-70,150)"/>
</g>
</svg>"""),
("application/json", json.dumps({"key": ["some", "json", "vals"]})),
("application/json", jsonutils.dumps({"key": ["some", "json", "vals"]})),
("text/html", """<html><body>This is some html</body></html>"""),
("application/octet-stream", "This is some octet stream"),
("application/text", "This is some text")

View File

@ -15,16 +15,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import os
from oslo_config import cfg
import oslo_messaging
from oslo_serialization import jsonutils
import six.moves.urllib.parse as urlparse
import sys
import time
topic = 'notifications'
password = os.environ.get('RABBIT_PASSWORD', os.environ.get('OS_PASSWORD'))
host = urlparse.urlparse(os.environ.get('OS_AUTH_URL')).hostname
@ -35,12 +33,12 @@ class EP(object):
def info(self, ctxt, publisher_id, event_type, payload, metadata):
all_locals = locals()
all_locals.pop('self')
print(json.dumps(all_locals))
print(jsonutils.dumps(all_locals))
def error(self, ctxt, publisher_id, event_type, payload, metadata):
all_locals = locals()
all_locals.pop('self')
print(json.dumps(all_locals))
print(jsonutils.dumps(all_locals))
def main():