Skip to content

Do not write comments to JSON files #722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion core/src/main/python/compare_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from wlsdeploy.logging.platform_logger import PlatformLogger
from wlsdeploy.util import cla_helper
from wlsdeploy.util import variables
from wlsdeploy.json.json_translator import COMMENT_MATCH
from wlsdeploy.util.model_translator import FileToPython
from wlsdeploy.yaml.yaml_translator import PythonToYaml
from wlsdeploy.json.json_translator import PythonToJson
Expand Down Expand Up @@ -327,7 +328,7 @@ def _add_results(self, ar_changes, is_delete=False, is_change=False):
#
if value_tree:
if is_change:
leaf['# - ' + splitted[0]] = value_tree[splitted[0]]
leaf[COMMENT_MATCH + splitted[0]] = value_tree[splitted[0]]
else:
if value_tree[splitted[0]] is not None and not isinstance(value_tree[splitted[0]], PyOrderedDict):
self._add_results(ar_changes, is_delete, is_change=True)
Expand Down
22 changes: 14 additions & 8 deletions core/src/main/python/wlsdeploy/json/json_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from wlsdeploy.logging.platform_logger import PlatformLogger
import wlsdeploy.exception.exception_helper as exception_helper

# Unlike with yaml files, JSON files do not allow comments. remove from file
COMMENT_MATCH = '# - '

class JsonToPython(object):
"""
Expand Down Expand Up @@ -155,6 +157,7 @@ def _write_dictionary_to_json_file(self, dictionary, writer, indent=''):
:param writer: where to write the dictionary into json syntax
:param indent: current string indention of the json syntax. If not provided, indent is an empty string
"""
_method_name = '_write_dictionary_to_json_file'
_start_dict = '{'
_end_dict = '}'

Expand All @@ -167,15 +170,18 @@ def _write_dictionary_to_json_file(self, dictionary, writer, indent=''):

indent += self._indent_unit
for key, value in dictionary.iteritems():
writer.println(end_line)
end_line = ','
writer.write(indent + '"' + _escape_text(key) + '" : ')
if isinstance(value, dict):
self._write_dictionary_to_json_file(value, writer, indent)
elif isinstance(value, list):
self._write_list_to_json_file(value, writer, indent)
if isinstance(key, basestring) and key.startswith(COMMENT_MATCH):
self._logger.finer('WLSDPLY-01714', key, class_name=self._class_name, method_name=_method_name)
else:
writer.write(_format_json_value(value))
writer.println(end_line)
end_line = ','
writer.write(indent + '"' + _escape_text(key) + '" : ')
if isinstance(value, dict):
self._write_dictionary_to_json_file(value, writer, indent)
elif isinstance(value, list):
self._write_list_to_json_file(value, writer, indent)
else:
writer.write(_format_json_value(value))
writer.println()
writer.write(end_indent + _end_dict)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ WLSDPLY-01710=Unable to parse model from file {0} : {1}
WLSDPLY-01711=Parse model {0} file from {1}
WLSDPLY-01712=Persist model {0} file to {1}
WLSDPLY-01713=Unable to persist model to file {0} : {1}
WLSDPLY-01714=Skip writing comment line {0} to JSON file

# wlsdeploy/util/string_utils.py
WLSDPLY-01720=to_boolean() method called with non-boolean value {0} so returning False
Expand Down