Table of Contents
This is a formatter for JSON strings.
JSON Formatter
is specialized in formatting JSON strings that do not use regular expressions and has no functions other than formatting. Therefore it is extremely lightweight besides can be expanded to meet user needs.
Note:
Replace version you want to use. Check the latest Packages.
Please contact me for a token to download the package.
Maven
<dependency>
<groupId>org.thinkit.formatter</groupId>
<artifactId>json-formatter</artifactId>
<version>v1.0.2</version>
</dependency>
<servers>
<server>
<id>github</id>
<username>myConsciousness</username>
<password>xxxxxxxxxxxxxxxxxx</password>
</server>
</servers>
Gradle
repositories {
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/myConsciousness/json-formatter")
credentials {
username = "myConsciousness"
password = "xxxxxxxxxxxxxxxxxx"
}
}
}
dependencies {
implementation 'org.thinkit.formatter:json-formatter:v1.0.2'
}
2. Add an import for JSON Fomratter
import org.thinkit.formatter.json.JsonFormatter;
Formatter formatter = JsonFormatter.newInstance();
You can also create an instance with a specific number of indents as following.
Note:
If a negative indentation is passed at instantiation time, the initial value defined in the content file takes precedence.
Formatter formatter = JsonFormatter.from(indent);
Formatter formatter = JsonFormatter.from(indent);
String formattedJson = formatter.format(json);
I have prepared the following unformatted json string for input/output reference. This is the json template for content-framework
, but it's hard to see without any spaces or line breaks.
{"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"}]}}]}
Let's run JSON Formatter
with the JSON string of above template as an argument.
JsonFormatter.newInstance().format(json);
The output is as follows.
Note:
Whitespace in the JSON string before formatting is trimmed during the formatting process, so no pre-processing is required.
{
"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"
}
]
}
}
]
}
Copyright 2020 Kato Shinya.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing permissions and limitations under
the License.
JSON Formatter
was designed and implemented by Kato Shinya, who works as a freelance developer.
Regardless of the means or content of communication, I would love to hear from you if you have any questions or concerns. I do not check my email box very often so a response may be delayed, anyway thank you for your interest!