+
+ """
+ pretty_json = _json.dumps(obj, separators=(",", ": "), indent=indent)
+ return pretty_json + "\n" # to avoid diff "no newline" warning line
+
+
+def sort_bits(obj, keys_with_bits=[]):
+ """
+ Rearrange string values of list bits names in alphabetical order.
+
+ This function looks at dict items with known keys.
+ If the value is string, space-separated names are sorted.
+ This function is recursive over dicts and lists.
+ Current implementation performs re-arranging in-place (to save memory),
+ so it is not required to store the return value.
+
+ The intended usage is for composite objects which contain
+ OrderedDict elements. The implementation makes sure that ordering
+ (dictated by keys) is preserved. Support for generic dicts is an added value.
+
+ Sadly, dict (at least in Python 2.7) does not have __updatevalue__(key) method
+ which would guarantee iteritems() is not affected when value is updated.
+ Current "obj[key] = value" implementation is safe for dict and OrderedDict,
+ but it may be not safe for other subclasses of dict.
+
+ TODO: Should this docstring include links to support dict and OrderedDict safety?
+ """
+ if isinstance(obj, dict):
+ for key, value in obj.items():
+ # Unicode is not str and vice versa, isinstance has to check for both.
+ # Luckily, "in" recognizes equivalent strings in different encodings.
+ # Type "bytes" is added for Python 3 compatibility.
+ if key in keys_with_bits and isinstance(value, (str, bytes)):
+ obj[key] = " ".join(sorted(value.split(" ")))
+ else:
+ sort_bits(value, keys_with_bits)
+ # A string is not a list, so there is no risk of recursion over characters.
+ elif isinstance(obj, list):
+ for item in obj:
+ sort_bits(item, keys_with_bits)
+ return obj
+
+
+def hide_volatile(obj, keys_with_volatiles=[]):
+ """
+ Takes list of keys with volatile values, and replaces them with generic "*"
+
+ :param obj: python dict from json
+ :param keys_with_volatiles: list of volatile keys
+ :return: corrected
+ """
+ if isinstance(obj, dict):
+ for key, value in obj.items():
+ # Unicode is not str and vice versa, isinstance has to check for both.
+ # Luckily, "in" recognizes equivalent strings in different encodings.
+ # Type "bytes" is added for Python 3 compatibility.
+ if key in keys_with_volatiles and isinstance(
+ value, (str, bytes, int, bool)
+ ):
+ obj[key] = "*"
+ else:
+ hide_volatile(value, keys_with_volatiles)
+ # A string is not a list, so there is no risk of recursion over characters.
+ elif isinstance(obj, list):
+ for item in obj:
+ hide_volatile(item, keys_with_volatiles)
+ return obj
+
+
+def normalize_json_text(
+ text,
+ strict=False,
+ indent=1,
+ keys_with_bits=[],
+ keys_with_volatiles=[],
+ jmes_path=None,
+):
+ """
+ Attempt to return sorted indented JSON string.
+
+ If jmes_path is set the related subset of JSON data is returned as
+ indented JSON string if the subset exists. Empty string is returned if the
+ subset doesn't exist.
+ Empty string is returned if text is not passed.
+ If parse error happens:
+ If strict is true, raise the exception.
+ If strict is not true, return original text with error message.
+ If keys_with_bits is non-empty, run sort_bits on intermediate Python object.