项目作者: myConsciousness

项目描述 :
Lightweight, Extensible and Flexible formatter for JSON based on Java.
高级语言: Java
项目地址: git://github.com/myConsciousness/json-formatter.git
创建时间: 2020-08-04T15:10:17Z
项目社区:https://github.com/myConsciousness/json-formatter

开源协议:Apache License 2.0

下载


Build
Latest Version
License

Java CI with Gradle

JSON Formatter

Table of Contents

What is it?

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.

How To Use

1. Add the dependencies

Note:

Replace version you want to use. Check the latest Packages.

Please contact me for a token to download the package.

Maven

  1. <dependency>
  2. <groupId>org.thinkit.formatter</groupId>
  3. <artifactId>json-formatter</artifactId>
  4. <version>v1.0.2</version>
  5. </dependency>
  6. <servers>
  7. <server>
  8. <id>github</id>
  9. <username>myConsciousness</username>
  10. <password>xxxxxxxxxxxxxxxxxx</password>
  11. </server>
  12. </servers>

Gradle

  1. repositories {
  2. maven {
  3. name = "GitHubPackages"
  4. url = uri("https://maven.pkg.github.com/myConsciousness/json-formatter")
  5. credentials {
  6. username = "myConsciousness"
  7. password = "xxxxxxxxxxxxxxxxxx"
  8. }
  9. }
  10. }
  11. dependencies {
  12. implementation 'org.thinkit.formatter:json-formatter:v1.0.2'
  13. }

2. Add an import for JSON Fomratter

  1. import org.thinkit.formatter.json.JsonFormatter;

3. Create a instance

  1. 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.

  1. Formatter formatter = JsonFormatter.from(indent);

4. Format

  1. Formatter formatter = JsonFormatter.from(indent);
  2. String formattedJson = formatter.format(json);

Demonstrate I/O

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.

  1. {"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.

  1. 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.

  1. {
  2. "metadata": {
  3. "author": "Kato Shinya",
  4. "since": "1.0",
  5. "version": "1.0",
  6. "creationDate": "2020/06/24",
  7. "encoding": "UTF-8",
  8. "description": "test"
  9. },
  10. "selectionNodes": [
  11. {
  12. "node": {
  13. "conditionId": "0",
  14. "test1": "test",
  15. "test2": "test"
  16. }
  17. },
  18. {
  19. "node": {
  20. "conditionId": "1",
  21. "test1": "test",
  22. "test2": "test"
  23. }
  24. },
  25. {
  26. "node": {
  27. "conditionId": "2",
  28. "test1": "test",
  29. "test2": "test"
  30. }
  31. }
  32. ],
  33. "conditionNodes": [
  34. {
  35. "node": {
  36. "conditionId": "0",
  37. "exclude": false,
  38. "conditions": [
  39. {
  40. "keyName": "test",
  41. "operand": "=",
  42. "value": "0"
  43. }
  44. ]
  45. }
  46. },
  47. {
  48. "node": {
  49. "conditionId": "1",
  50. "exclude": false,
  51. "conditions": [
  52. {
  53. "keyName": "test",
  54. "operand": "=",
  55. "value": "1"
  56. }
  57. ]
  58. }
  59. },
  60. {
  61. "node": {
  62. "conditionId": "2",
  63. "exclude": false,
  64. "conditions": [
  65. {
  66. "keyName": "test",
  67. "operand": "=",
  68. "value": "2"
  69. }
  70. ]
  71. }
  72. }
  73. ]
  74. }

License

  1. Copyright 2020 Kato Shinya.
  2. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  3. in compliance with the License. You may obtain a copy of the License at
  4. http://www.apache.org/licenses/LICENSE-2.0
  5. Unless required by applicable law or agreed to in writing, software distributed under the License
  6. is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  7. or implied. See the License for the specific language governing permissions and limitations under
  8. the License.

More Information

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!