示例#1
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}')
示例#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}')
示例#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}')
示例#4
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}')
示例#5
0
def result_resolver(input):
    reader = JsonReader(input)
    formatter = JsonFormatter(
        reader, {"force_property_quotes": True, "indent_character": "\t", "normalize_strings": True}
    )

    return formatter.format()
示例#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}'
        )
示例#7
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}'
        )
示例#8
0
    def format(self, text, options):
        reader = JsonReader(text)
        formatter = JsonFormatter(reader, {'force_property_quotes': True, 'quote_char': '"', 'normalize_strings': True, 'indent_character': options['indent_character']})
        formattedText = formatter.format()

        self.view.set_syntax_file('Packages/JavaScript/JSON.tmLanguage')

        return formattedText
示例#9
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}'
        )
示例#10
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}'
        )
示例#11
0
def result_resolver(input):
    reader = JsonReader(input)
    formatter = JsonFormatter(
        reader, {
            'force_property_quotes': True,
            'indent_character': '\t',
            'normalize_strings': True
        })

    return formatter.format()
示例#12
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}')
示例#13
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}'
        )
示例#14
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}')
示例#15
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}')
示例#16
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}')
示例#17
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}')
示例#18
0
    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()
示例#19
0
    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()
示例#20
0
    def test_should_format_empty_object(self):
        reader = JsonReader('{ }')
        formatter = JsonFormatter(reader)

        output = formatter.format()
        expect(output).to_equal('{}')
示例#21
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}')
示例#22
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]')
示例#23
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}')
示例#24
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}')
示例#25
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}')
示例#26
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}')
示例#27
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}')
示例#28
0
    def test_should_format_empty_object(self):
        reader = JsonReader("{ }")
        formatter = JsonFormatter(reader)

        output = formatter.format()
        expect(output).to_equal("{}")
示例#29
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}')
示例#30
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}')
示例#31
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}')
示例#32
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}')
示例#33
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}")
示例#34
0
    def test_should_format_multiple_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}')
示例#35
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]")
示例#36
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}')
示例#37
0
    def test_should_format_properties_with_forced_single_quotes(self):
        reader = JsonReader('{\'hello\':"world" ,"value":123}')
        formatter = JsonFormatter(reader, {"force_property_quotes": True, "quote_char": "'"})

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