class PythonToJson(object): """ This class writes a Python dictionary out in a JSON format. """ _class_name = 'PythonToJson' # 4 spaces of indent _indent_unit = ' ' def __init__(self, dictionary): # Fix error handling for None self._dictionary = dictionary self._logger = PlatformLogger('wlsdeploy.json') return def write_to_json_file(self, file_name): """ Convert the Python dictionary to JSON and write it to the specified file. :param file_name: the name of the file :return: the java.io.File object of the JSON file """ _method_name = 'writeToJsonFile' self._logger.entering(file_name, class_name=self._class_name, method_name=_method_name) try: json_file = JFileUtils.validateWritableFile(file_name) except JIllegalArgumentException, iae: json_ex = exception_helper.create_json_exception( 'WLSDPLY-18015', file_name, iae.getLocalizedMessage(), error=iae) self._logger.throwing(class_name=self._class_name, method_name=_method_name, error=json_ex) raise json_ex fos = None writer = None try: fos = JFileOutputStream(json_file, False) writer = JPrintWriter(fos, True) self._write_dictionary_to_json_file(self._dictionary, writer) except JFileNotFoundException, fnfe: json_ex = exception_helper.create_json_exception( 'WLSDPLY-18010', file_name, fnfe.getLocalizedMessage(), error=fnfe) self._logger.throwing(class_name=self._class_name, method_name=_method_name, error=json_ex) self._close_streams(fos, writer) raise json_ex
def __init__(self, file_name, use_ordering=False): _method_name = '__init__' self._file_name = file_name self._logger = PlatformLogger('wlsdeploy.json') try: self._translator = JJsonTranslator(file_name, use_ordering) except JIllegalArgumentException, iae: json_ex = \ exception_helper.create_json_exception('WLSDPLY-18014', file_name, iae.getLocalizedMessage(), error=iae) self._logger.throwing(class_name=self._class_name, method_name=_method_name, error=json_ex) raise json_ex
def write_to_json_file(self, file_name): """ Convert the Python dictionary to JSON and write it to the specified file. :param file_name: the name of the file :return: the java.io.File object of the JSON file """ _method_name = 'writeToJsonFile' self._logger.entering(file_name, class_name=self._class_name, method_name=_method_name) try: json_file = JFileUtils.validateWritableFile(file_name) except JIllegalArgumentException, iae: json_ex = exception_helper.create_json_exception('WLSDPLY-18015', file_name, iae.getLocalizedMessage(), error=iae) self._logger.throwing(class_name=self._class_name, method_name=_method_name, error=json_ex) raise json_ex
except JFileNotFoundException, fnfe: json_ex = exception_helper.create_json_exception( 'WLSDPLY-18010', file_name, fnfe.getLocalizedMessage(), error=fnfe) self._logger.throwing(class_name=self._class_name, method_name=_method_name, error=json_ex) self._close_streams(fos, writer) raise json_ex except JIOException, ioe: json_ex = exception_helper.create_json_exception( 'WLSDPLY-18011', file_name, ioe.getLocalizedMessage(), error=ioe) self._logger.throwing(class_name=self._class_name, method_name=_method_name, error=json_ex) self._close_streams(fos, writer) raise json_ex self._close_streams(fos, writer) self._logger.exiting(class_name=self._class_name, method_name=_method_name, result=json_file) return json_file def _write_dictionary_to_json_file(self, dictionary, writer, indent=''):