Skip to content

Commit 17757ef

Browse files
Do not write comments to JSON files (#722)
* Do not write comments to JSON files
1 parent cb733ee commit 17757ef

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

core/src/main/python/compare_model.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from wlsdeploy.logging.platform_logger import PlatformLogger
3636
from wlsdeploy.util import cla_helper
3737
from wlsdeploy.util import variables
38+
from wlsdeploy.json.json_translator import COMMENT_MATCH
3839
from wlsdeploy.util.model_translator import FileToPython
3940
from wlsdeploy.yaml.yaml_translator import PythonToYaml
4041
from wlsdeploy.json.json_translator import PythonToJson
@@ -327,7 +328,7 @@ def _add_results(self, ar_changes, is_delete=False, is_change=False):
327328
#
328329
if value_tree:
329330
if is_change:
330-
leaf['# - ' + splitted[0]] = value_tree[splitted[0]]
331+
leaf[COMMENT_MATCH + splitted[0]] = value_tree[splitted[0]]
331332
else:
332333
if value_tree[splitted[0]] is not None and not isinstance(value_tree[splitted[0]], PyOrderedDict):
333334
self._add_results(ar_changes, is_delete, is_change=True)

core/src/main/python/wlsdeploy/json/json_translator.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
from wlsdeploy.logging.platform_logger import PlatformLogger
2121
import wlsdeploy.exception.exception_helper as exception_helper
2222

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

2426
class JsonToPython(object):
2527
"""
@@ -155,6 +157,7 @@ def _write_dictionary_to_json_file(self, dictionary, writer, indent=''):
155157
:param writer: where to write the dictionary into json syntax
156158
:param indent: current string indention of the json syntax. If not provided, indent is an empty string
157159
"""
160+
_method_name = '_write_dictionary_to_json_file'
158161
_start_dict = '{'
159162
_end_dict = '}'
160163

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

168171
indent += self._indent_unit
169172
for key, value in dictionary.iteritems():
170-
writer.println(end_line)
171-
end_line = ','
172-
writer.write(indent + '"' + _escape_text(key) + '" : ')
173-
if isinstance(value, dict):
174-
self._write_dictionary_to_json_file(value, writer, indent)
175-
elif isinstance(value, list):
176-
self._write_list_to_json_file(value, writer, indent)
173+
if isinstance(key, basestring) and key.startswith(COMMENT_MATCH):
174+
self._logger.finer('WLSDPLY-01714', key, class_name=self._class_name, method_name=_method_name)
177175
else:
178-
writer.write(_format_json_value(value))
176+
writer.println(end_line)
177+
end_line = ','
178+
writer.write(indent + '"' + _escape_text(key) + '" : ')
179+
if isinstance(value, dict):
180+
self._write_dictionary_to_json_file(value, writer, indent)
181+
elif isinstance(value, list):
182+
self._write_list_to_json_file(value, writer, indent)
183+
else:
184+
writer.write(_format_json_value(value))
179185
writer.println()
180186
writer.write(end_indent + _end_dict)
181187

core/src/main/resources/oracle/weblogic/deploy/messages/wlsdeploy_rb.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ WLSDPLY-01710=Unable to parse model from file {0} : {1}
305305
WLSDPLY-01711=Parse model {0} file from {1}
306306
WLSDPLY-01712=Persist model {0} file to {1}
307307
WLSDPLY-01713=Unable to persist model to file {0} : {1}
308+
WLSDPLY-01714=Skip writing comment line {0} to JSON file
308309

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

0 commit comments

Comments
 (0)