Exemple #1
0
    def test_should_format_nested_object(self):
        reader = JsonReader('{"hello":"world" ,"obj":{"abc":123}}')
        formatter = JsonFormatter(reader)

        output = formatter.format()
        expect(output).to_equal(
            '{\n  "hello": "world",\n  "obj": {\n    "abc": 123\n  }\n}')
Exemple #2
0
    def test_should_format_object_with_array(self):
        reader = JsonReader('{"hello":"world" ,"arr":["hello","world"]}')
        formatter = JsonFormatter(reader)

        output = formatter.format()
        expect(output).to_equal(
            '{\n  "hello": "world",\n  "arr": ["hello", "world"]\n}')
Exemple #3
0
    def test_should_preserve_end_line_comment(self):
        reader = JsonReader('{\'"hello"\':"world", // comment\n\'value\':123}')
        formatter = JsonFormatter(reader, {'force_property_quotes': True})

        output = formatter.format()
        expect(output).to_equal(
            '{\n  "\\"hello\\"": "world", // comment\n  "value": 123\n}')
Exemple #4
0
    def test_should_format_single_quote_properties_with_forced_double_quotes(
            self):
        reader = JsonReader('{\'hello\':"world" ,\'value\':123}')
        formatter = JsonFormatter(reader, {'force_property_quotes': True})

        output = formatter.format()
        expect(output).to_equal('{\n  "hello": "world",\n  "value": 123\n}')
Exemple #5
0
    def test_should_format_array_of_objects_with_tabs(self):
        reader = JsonReader(
            '{"arr":[{"int":123,"str":"hello"},{"int":123,"str":"hello"}]}')
        formatter = JsonFormatter(reader, {'indent_character': '\t'})

        output = formatter.format()
        expect(output).to_equal(
            '{\n\t"arr": [{\n\t\t"int": 123,\n\t\t"str": "hello"\n\t}, {\n\t\t"int": 123,\n\t\t"str": "hello"\n\t}]\n}'
        )
Exemple #6
0
    def test_should_format_array_of_objects(self):
        reader = JsonReader(
            '{"arr":[{"int":123,"str":"hello"},{"int":123,"str":"hello"}]}')
        formatter = JsonFormatter(reader)

        output = formatter.format()
        expect(output).to_equal(
            '{\n  "arr": [{\n    "int": 123,\n    "str": "hello"\n  }, {\n    "int": 123,\n    "str": "hello"\n  }]\n}'
        )
Exemple #7
0
def result_resolver(input):
    reader = JsonReader(input)
    formatter = JsonFormatter(
        reader, {
            'force_property_quotes': True,
            'indent_character': '\t',
            'normalize_strings': True
        })

    return formatter.format()
Exemple #8
0
    def test_should_format_property_with_empty_double_quote_string_and_normalized_strings(
            self):
        reader = JsonReader('{\'hello\':""}')
        formatter = JsonFormatter(reader, {
            'force_property_quotes': True,
            'normalize_strings': True
        })

        output = formatter.format()
        expect(output).to_equal('{\n  "hello": ""\n}')
Exemple #9
0
    def test_should_strip_in_line_comment_block(self):
        reader = JsonReader(
            '{\'"hello"\':"world", /* comment block */ \'value\':123}')
        formatter = JsonFormatter(reader, {
            'force_property_quotes': True,
            'remove_comments': True
        })

        output = formatter.format()
        expect(output).to_equal(
            '{\n  "\\"hello\\"": "world",\n  "value": 123\n}')
Exemple #10
0
    def test_should_preserve_end_line_comment_forcing_new_line_for_subsequent_tokens(
            self):
        reader = JsonReader(
            '{\'"hello"\':"world",\n// full line comment\n\'value\'://comment\n123}'
        )
        formatter = JsonFormatter(reader, {'force_property_quotes': True})

        output = formatter.format()
        expect(output).to_equal(
            '{\n  "\\"hello\\"": "world",\n  // full line comment\n  "value": //comment\n  123\n}'
        )
Exemple #11
0
    def test_should_format_property_values_with_normalized_strings(self):
        reader = JsonReader('{\'hello\':"world" ,"value":123}')
        formatter = JsonFormatter(
            reader, {
                'force_property_quotes': True,
                'quote_char': '\'',
                'normalize_strings': True
            })

        output = formatter.format()
        expect(output).to_equal(
            '{\n  \'hello\': \'world\',\n  \'value\': 123\n}')
Exemple #12
0
    def test_should_format_json_on_one_line(self):
        reader = JsonReader(
            '{"hello":\n  "world", // comment\n \'value\':123}')
        formatter = JsonFormatter(
            reader, {
                'force_property_quotes': True,
                'remove_comments': True,
                'spacer': '',
                'newline': '',
                'indent_character': ''
            })

        output = formatter.format()
        expect(output).to_equal('{"hello":"world","value":123}')
    def format(self, text, indent_char='\t'):
        if text:
            match = first_char_exp.search(text)

            if match:
                first_char = match.group(1)

                if first_char == '<':
                    formatter = XmlIndentFormatter(indent_char)
                    return formatter.indent(text)
                else:
                    formatter = JsonFormatter(
                        JsonReader(text), {'indent_character': indent_char})
                    return formatter.format()
    def unindent(self, text):
        if text:
            match = first_char_exp.search(text)

            if match:
                first_char = match.group(1)

                if first_char == '<':
                    formatter = XmlIndentFormatter('', True)
                    return formatter.indent(text)
                else:
                    formatter = JsonFormatter(
                        JsonReader(text), {
                            'remove_comments': True,
                            'spacer': '',
                            'newline': '',
                            'indent_character': ''
                        })
                    return formatter.format()
Exemple #15
0
    def test_should_format_single_property(self):
        reader = JsonReader('{"hello":"world"}')
        formatter = JsonFormatter(reader)

        output = formatter.format()
        expect(output).to_equal('{\n  "hello": "world"\n}')
Exemple #16
0
    def test_should_format_scalar_array(self):
        reader = JsonReader('[1,2,3]')
        formatter = JsonFormatter(reader)

        output = formatter.format()
        expect(output).to_equal('[1, 2, 3]')
Exemple #17
0
    def test_should_format_empty_object(self):
        reader = JsonReader('{ }')
        formatter = JsonFormatter(reader)

        output = formatter.format()
        expect(output).to_equal('{}')
Exemple #18
0
    def test_should_format_multiple_properties(self):
        reader = JsonReader('{"hello":"world" ,"value":123}')
        formatter = JsonFormatter(reader)

        output = formatter.format()
        expect(output).to_equal('{\n  "hello": "world",\n  "value": 123\n}')