Permalink
Browse files

JsonFormatFilter subfilter to specify indentation

Change how subfilter for JsonFormatFilter is used. Subfilter now
specifies the indentation level of the formatted json. Default
indentation is still 4 in case subfilter is not specified.
  • Loading branch information...
nikobockerman committed Jun 26, 2018
1 parent c8cbd68 commit 0ceeb50cf1ba7ca36d1c7ad3d3592d03ce145e0a
Showing with 17 additions and 9 deletions.
  1. +4 −3 lib/urlwatch/filters.py
  2. +13 −6 test/test_filters.py
View
@@ -190,10 +190,11 @@ class JsonFormatFilter(FilterBase):
__kind__ = 'format-json'
def filter(self, data, subfilter=None):
parsed_json = json.loads(data)
indentation = 4
if subfilter is not None:
parsed_json = parsed_json[subfilter]
return json.dumps(parsed_json, sort_keys=True, indent=4)
indentation = int(subfilter)
parsed_json = json.loads(data)
return json.dumps(parsed_json, sort_keys=True, indent=indentation)
class GrepFilter(FilterBase):
View
@@ -43,15 +43,22 @@ def test_json_format_filter():
result = json_format_filter.filter(
"""{"field1": {"f1.1": "value"},"field2": "value"}""")
print(result)
eq_(
result,
"""{\n "field1": {\n "f1.1": "value"\n },\n "field2": "value"\n}"""
)
eq_(result, """{
"field1": {
"f1.1": "value"
},
"field2": "value"
}""")
def test_json_format_filter_subfilter():
json_format_filter = JsonFormatFilter(None, None)
result = json_format_filter.filter(
"""{"field1": {"f1.1": "value"},"field2": "value"}""", "field1")
"""{"field1": {"f1.1": "value"},"field2": "value"}""", "2")
print(result)
eq_(result, """{\n "f1.1": "value"\n}""")
eq_(result, """{
"field1": {
"f1.1": "value"
},
"field2": "value"
}""")

0 comments on commit 0ceeb50

Please sign in to comment.