Skip to content

Latest commit

 

History

History
292 lines (235 loc) · 6.27 KB

README_JP.md

File metadata and controls

292 lines (235 loc) · 6.27 KB

POJO to JSON

POJOをJSONに変換するシンプルなプラグイン

  • BigDecimalと他のNumericぷったいをサポートします
  • Java8 時間タイプをサポートします
  • Enumをサポートします
  • JacksonとFastjson 注釈は部分的なサポートします
  • Java14 Recordsをサポートします JEP-359
  • Support conversion
    • Inner Class
    • Global Variable
    • Local Variable
    • Constructor Parameter
    • Method Parameter

Support JVM platform languages

  • Java - 完全にサポートします
  • Kotlin - どうやら完全にサポートします

Usage

  • カーソルの位置が結果に影響を与える可能性があることに注意してください!

  • classファイルを開く > カーソルを Class/Variable/Parameter に移動します > 右クリック > Copy/Paste Special > Copy JSON > JSONの結果がクリップボードにコピーされます Image text

  • Project viewでclassファイルを選択 > 右クリック > Copy JSON > JSONの結果がクリップボードにコピーされます Image text

  • Project viewで複数ファイルを選択 > 右クリック > Copy JSON > JSON result will generate to files in the Scratches folder Image text

Installation

  • Install in IDEA:

    • Preferences(Settings) > Plugins > Marketplace > Search"POJO to JSON" > Install
  • Manual Install:

    • plugin -> Preferences(Settings) > Plugins > ⚙️ > Install plugin from disk... -> Select the plug-in package and install(No need to unzip)

Q&A

  • 使用時に常にエラーを報告するのはなぜですか?
This class reference level exceeds maximum limit or has nested references!

When the program throws this warning there are only two possibilities.

  1. This class or parent class has nested references

eg:

public class A {
    private B b;

    public class B {
        private A a;
    }
}
{
 "b":{
  "a":{
   "b":{
     "a":{
        ......
      }
    }
   }
 }
}

or

public class A {
    private A a;
}
{
 "a":{
  "a":{
   "a":{
     "a":{
        ......
      }
    }
   }
 }
}
  1. This class reference level > 200

eg:

public class A {
    private B _0;
    public class B {
        private C _1;
        public class C {
            private D _2;
            public class D {
                // _3 ..... _201..
            }
        }
    }
}
{
  "_0": {
    "_1": {
      "_2": {
        "......_201":{}
      }
    }
  }
}

Perhaps both will happen for entity but this entity are not suitable for JSON.
So you can try to serialize your POJO using Jackson to see what happens.
If no exception, you can submit a bug to this repository issues with your target class :)

  • But how to solve this problem?

You can try the following methods.

Support Annotations and Javadoc

@JsonProperty and @JSONField

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonProperty;

public class User {

    @JsonProperty("name")
    private String username;
    
    @JSONField(name = "pass")
    private String password;
}

paste result:

{
  "name": "",
  "pass": ""
}

@JsonIgnore or Javadoc tags JsonIgnore

import com.fasterxml.jackson.annotation.JsonIgnore;

public class User {
    
    @JsonIgnore
    private String username;
    private String password;
}

or when there is no jackson library

public class JsonIgnoreDocTestPOJO {

  /**
   * @JsonIgnore
   */
  private String username;
  private String password;
}

paste result:

{
  "password": ""
}

@JsonIgnoreProperties or Javadoc tags JsonIgnoreProperties

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import java.util.List;

public class User {

    private String username;
    @JsonIgnoreProperties({"users", "aaa", "bbb"})
    private List<Role> roles;

    public class Role {

        private String roleName;
        private List<User> users;
    }
}

or when there is no jackson library

import java.util.List;

public class User {

    private String username;
    /**
     * @JsonIgnoreProperties users, aaa, bbb
     */
    private List<Role> roles;

    public class Role {

        private String roleName;
        private List<User> users;
    }
}

paste result:

{
  "username": "",
  "roles": [
    {
      "roleName": ""
    }
  ]
}

You may encounter this problem during use.

This class reference level exceeds maximum limit or has nested references!

The above method can solve the nested reference problem well.

@JsonIgnoreType

import com.fasterxml.jackson.annotation.JsonIgnoreType;

import java.util.List;

public class User {

    private String username;
    private List<Role> roles;

    @JsonIgnoreType
    public class Role {
        private String roleName;
        private List<User> users;
    }
}

paste result:

{
  "username": "",
  "roles": []
}

Contributors

Ideas and partial realization from linsage