Skip to content

A simple plugin for converting POJO to JSON in IntelliJ IDEA

License

Notifications You must be signed in to change notification settings

frp1719500292/pojo2json

 
 

Repository files navigation

POJO to JSON

A simple plugin for converting POJO to JSON in IntelliJ IDEA

  • Support BigDecimal and other Numeric objects.
  • Support Java8 time type.
  • Support Enum.
  • Partial support Jackson and Fastjson annotations.
  • Support Java14 Records JEP-359
  • Support conversion
    • Inner Class
    • Global Variable
    • Local Variable
    • Constructor Parameter
    • Method Parameter

Support JVM platform languages

  • Java - full support
  • Kotlin - beta, but full support

Usage

  • Note that the position of the cursor can affect the result!

  • Open class file > Move cursor to Class/Variable/Parameter > Right click > Copy/Paste Special > Copy JSON > JSON result will copy to clipboard Image text

  • Project view select a class file > Right click > Copy JSON > JSON result will copy to clipboard Image text

  • Project view select multiple files > Right click > 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

  • Why always report errors when use it?
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 > 500

eg:

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

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

About

A simple plugin for converting POJO to JSON in IntelliJ IDEA

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 94.4%
  • Kotlin 5.6%