Skip to content
This repository was archived by the owner on Nov 21, 2024. It is now read-only.

Commit 24a2195

Browse files
author
Kato Shinya
committed
update
1 parent b18b456 commit 24a2195

File tree

4 files changed

+122
-13
lines changed

4 files changed

+122
-13
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ dependencies {
6767
annotationProcessor 'org.projectlombok:lombok:1.18.12'
6868

6969
implementation 'org.thinkit.common:precondition-validator:v1.1.0'
70-
implementation 'org.thinkit.formatter.common:formatter-commons:v1.0.1'
70+
implementation 'org.thinkit.formatter.common:formatter-commons:v1.0.2'
7171
implementation 'org.thinkit.framework.content:content-framework:v1.1.0'
7272
implementation 'org.thinkit.api.catalog:catalog-api:v1.0.2'
7373
}

src/main/java/org/thinkit/formatter/json/JsonFormatter.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public String format(@NonNull final String json) {
6666
if (Brace.START.getTag().equals(lowercaseToken) || Bracket.START.getTag().equals(lowercaseToken)) {
6767
appender.appendToken().incrementIndent().appendNewline();
6868
} else if (Brace.END.getTag().equals(lowercaseToken) || Bracket.END.getTag().equals(lowercaseToken)) {
69-
appender.decrementIndent().appendToken();
69+
appender.decrementIndent().appendNewline().appendToken();
7070
} else if (Delimiter.COMMA.getTag().equals(lowercaseToken)) {
7171
appender.appendToken().appendNewline();
7272

@@ -75,13 +75,11 @@ public String format(@NonNull final String json) {
7575
if (Bracket.END.getTag().equals(lastToken)) {
7676
appender.appendNewline();
7777
}
78-
} else if (Delimiter.COMMA.getTag().equals(lowercaseToken)) {
79-
appender.appendToken().appendSpace();
8078
} else {
8179
appender.appendToken();
8280
}
8381
}
8482

85-
return appender.toString();
83+
return appender.appendNewline().toString();
8684
}
8785
}

src/main/java/org/thinkit/formatter/json/JsonTokenizer.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,6 @@ public boolean next() {
118118
if (Quotation.DOUBLE_QUOTATION.getTag().equals(this.token)) {
119119
this.afterDoubleQuotation();
120120
} else {
121-
if (!this.isWhitespace(this.lowercaseToken)) {
122-
this.lastToken = this.lowercaseToken;
123-
}
124-
125121
this.lowercaseToken = this.token.toLowerCase(Locale.ROOT);
126122
}
127123

@@ -140,10 +136,6 @@ private void afterDoubleQuotation() {
140136
final String tokenAfterQuote = jsonTokenizer.nextToken();
141137
sb.append(tokenAfterQuote);
142138

143-
if (!this.isWhitespace(this.lowercaseToken)) {
144-
this.lastToken = this.lowercaseToken;
145-
}
146-
147139
this.lowercaseToken = tokenAfterQuote.toLowerCase(Locale.ROOT);
148140

149141
if (Quotation.DOUBLE_QUOTATION.getTag().equals(tokenAfterQuote) && !"\\".equals(this.lastToken)) {
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright 2021 Kato Shinya.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package org.thinkit.formatter.json;
16+
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.junit.jupiter.api.Assertions.assertNotNull;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
/**
23+
* {@link JsonFormatter} クラスのテストケースを管理するクラスです。
24+
*
25+
* @author Kato Shinya
26+
* @since 1.0.2
27+
*/
28+
public final class JsonFormatterTest {
29+
30+
@Test
31+
void testFormat() {
32+
33+
final String formattedJson = JsonFormatter.newInstance().format(
34+
" {\"metadata\":{\"author\":\"Kato Shinya\",\"since\":\"1.0\",\"version\":\"1.0\",\"creationDate\":\"2020/06/24\",\"encoding\":\"UTF-8\",\"description\":\"test\"},\"selectionNodes\":[{\"node\":{\"conditionId\":\"0\",\"test1\":\"test\",\"test2\":\"test\"}},{\"node\":{\"conditionId\":\"1\",\"test1\":\"test\",\"test2\":\"test\"}},{\"node\":{\"conditionId\":\"2\",\"test1\":\"test\",\"test2\":\"test\"}}],\"conditionNodes\":[{\"node\":{\"conditionId\":\"0\",\"exclude\":false,\"conditions\":[{\"keyName\":\"test\",\"operand\":\"=\",\"value\":\"0\"}]}},{\"node\":{\"conditionId\":\"1\",\"exclude\":false,\"conditions\":[{\"keyName\":\"test\",\"operand\":\"=\",\"value\":\"1\"}]}},{\"node\":{\"conditionId\":\"2\",\"exclude\":false,\"conditions\":[{\"keyName\":\"test\",\"operand\":\"=\",\"value\":\"2\"}]}}]}");
35+
36+
assertNotNull(formattedJson);
37+
assertEquals(EXPECTED_FORMATTED_JSON_STRING, formattedJson);
38+
}
39+
40+
/**
41+
* 整形されたJSONの期待値
42+
*/
43+
private final String EXPECTED_FORMATTED_JSON_STRING = """
44+
{
45+
"metadata":{
46+
"author":"Kato Shinya",
47+
"since":"1.0",
48+
"version":"1.0",
49+
"creationDate":"2020/06/24",
50+
"encoding":"UTF-8",
51+
"description":"test"
52+
},
53+
"selectionNodes":[
54+
{
55+
"node":{
56+
"conditionId":"0",
57+
"test1":"test",
58+
"test2":"test"
59+
}
60+
},
61+
{
62+
"node":{
63+
"conditionId":"1",
64+
"test1":"test",
65+
"test2":"test"
66+
}
67+
},
68+
{
69+
"node":{
70+
"conditionId":"2",
71+
"test1":"test",
72+
"test2":"test"
73+
}
74+
}
75+
],
76+
"conditionNodes":[
77+
{
78+
"node":{
79+
"conditionId":"0",
80+
"exclude":false,
81+
"conditions":[
82+
{
83+
"keyName":"test",
84+
"operand":"=",
85+
"value":"0"
86+
}
87+
]
88+
}
89+
},
90+
{
91+
"node":{
92+
"conditionId":"1",
93+
"exclude":false,
94+
"conditions":[
95+
{
96+
"keyName":"test",
97+
"operand":"=",
98+
"value":"1"
99+
}
100+
]
101+
}
102+
},
103+
{
104+
"node":{
105+
"conditionId":"2",
106+
"exclude":false,
107+
"conditions":[
108+
{
109+
"keyName":"test",
110+
"operand":"=",
111+
"value":"2"
112+
}
113+
]
114+
}
115+
}
116+
]
117+
}
118+
""";
119+
}

0 commit comments

Comments
 (0)