Code Monkey home page Code Monkey logo

json-formatter's Introduction

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

<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;

3. Create a instance

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);

4. Format

Formatter formatter = JsonFormatter.from(indent);
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.

    {"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"
          }
        ]
      }
    }
  ]
}

License

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.

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!

json-formatter's People

Contributors

myconsciousness avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

json-formatter's Issues

コンテンツファイル名を和名から英名へ変更

Feature Request

1. Feature details

content-framework のバグを調査していたところ、jar の中に格納されたコンテンツファイルが文字化けしていることが判明した。この状態だと動的に jar 内のコンテンツファイルを取得することができないためコンテンツファイルは和名から英名に修正する。

2. Why it is necessary

jar に格納されたコンテンツファイル名が文字化けしていることによって処理中で jar に格納されたコンテンツファイルを取得することができない。

3. How to implement

  1. コンテンツファイル名の修正
  2. コンテンツファイルに紐づく各クラスの修正

4. References

Json Formatterの作成

Jsonを整形する機能を作成する。

基本的な構成パッケージは org.thinkit.formatter とする。

JSONの整形方式

JSONの整形方式に関しては JSONObject.toString(int indemtFactor) を使用する。

また、上記のソースコードを解析し内製化を進めたい。

}, 後の改行インデント改善

現状は }, 後の改行インデント数はその時点まででインクリメントされた値を使用しているため無駄なスペースが挿入されてしまう。
そのため、}, 後かつ次の { までに改行を1つ以上挿入する場合はインデントが 0 の改行を挿入するように修正する。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.