├── .gitignore ├── README.md ├── java2json.jar ├── resources └── META-INF │ └── plugin.xml ├── screenshot └── java2json.gif └── src └── com └── linsage ├── Java2JsonAction.java ├── KV.java └── menu ├── SponsorAction.java └── ToggleCommentAction.java /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea/ 2 | *.iml 3 | rebel*.xml 4 | .gradle 5 | classes 6 | logs 7 | target 8 | out -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # java2json 2 | 3 | A simple plugin for converting Java bean to JSON in IntelliJ IDEA 4 | 5 | When you post json request using Postman :) 6 | 7 | ## Screenshot 8 | 9 |  10 | 11 | https://plugins.jetbrains.com/plugin/10336-java-bean-to-json 12 | 13 | ## Change Log 14 | - v1.0 15 | - First revision 16 | - v1.0.1 17 | - update plugin info 18 | - v1.0.2 19 | - add doc comment 20 | - v1.0.3 21 | - support time class type 22 | - v1.0.4 23 | - build IntelliJ IDEA 192.* due to Java functionality 24 | - v1.0.5 25 | - support enum 26 | - v1.0.6 27 | - add tools menu 28 | - v1.0.7 29 | - support @JsonProperty annotation 30 | 31 | ## Support 32 | 33 | [Donate with PayPal](https://www.paypal.me/linsage) 34 | 35 |  36 | -------------------------------------------------------------------------------- /java2json.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linsage/java2json/7815957541f168cd25890f8866e5f299bf7ce6f2/java2json.jar -------------------------------------------------------------------------------- /resources/META-INF/plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | com.linsage 3 | Java Bean to Json 4 | 1.0.7 5 | linsage 6 | com.intellij.modules.lang 7 | GitHub | 9 | Donate with PayPal | 10 | 11 | 12 | A simple plugin for converting Java bean to JSON 13 | When you post json request using Postman :) 14 | ]]> 15 | 16 | 1.0 - First revision. 18 | 1.0.1 - update plugin info. 19 | 1.0.2 - add doc comment. 20 | 1.0.3 - support time class type. 21 | 1.0.4 - build IntelliJ IDEA 192.* due to Java functionality 22 | 1.0.5 - support enum 23 | 1.0.6 - add tools menu 24 | 1.0.7 - support @JsonProperty annntation 25 | ]]> 26 | 27 | 28 | com.intellij.modules.lang 29 | com.intellij.modules.java 30 | 31 | 32 | 33 | 35 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /screenshot/java2json.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linsage/java2json/7815957541f168cd25890f8866e5f299bf7ce6f2/screenshot/java2json.gif -------------------------------------------------------------------------------- /src/com/linsage/Java2JsonAction.java: -------------------------------------------------------------------------------- 1 | package com.linsage; 2 | 3 | import com.google.common.collect.Sets; 4 | import com.intellij.notification.*; 5 | import com.intellij.openapi.actionSystem.AnAction; 6 | import com.intellij.openapi.actionSystem.AnActionEvent; 7 | import com.intellij.openapi.actionSystem.CommonDataKeys; 8 | import com.intellij.openapi.editor.Editor; 9 | import com.intellij.openapi.project.Project; 10 | import com.intellij.psi.*; 11 | import com.intellij.psi.util.PsiTreeUtil; 12 | import com.intellij.psi.util.PsiTypesUtil; 13 | import com.intellij.psi.util.PsiUtil; 14 | import org.jetbrains.annotations.NonNls; 15 | 16 | import java.awt.*; 17 | import java.awt.datatransfer.Clipboard; 18 | import java.awt.datatransfer.StringSelection; 19 | import java.text.DateFormat; 20 | import java.text.SimpleDateFormat; 21 | import java.time.LocalDate; 22 | import java.time.LocalDateTime; 23 | import java.time.LocalTime; 24 | import java.util.ArrayList; 25 | import java.util.Date; 26 | import java.util.HashMap; 27 | import java.util.Map; 28 | import java.util.regex.Matcher; 29 | import java.util.regex.Pattern; 30 | import java.util.Set; 31 | 32 | public class Java2JsonAction extends AnAction { 33 | private static NotificationGroup notificationGroup; 34 | private static String pattern = "yyyy-MM-dd HH:mm:ss"; 35 | private static DateFormat df = new SimpleDateFormat(pattern); 36 | public static boolean isShowComment = true; 37 | 38 | @NonNls 39 | private static final Map normalTypes = new HashMap<>(); 40 | 41 | static { 42 | notificationGroup = new NotificationGroup("Java2Json.NotificationGroup", NotificationDisplayType.BALLOON, true); 43 | 44 | normalTypes.put("Boolean", false); 45 | normalTypes.put("Byte", 0); 46 | normalTypes.put("Short", Short.valueOf((short) 0)); 47 | normalTypes.put("Integer", 0); 48 | normalTypes.put("Long", 0L); 49 | normalTypes.put("Float", 0.0F); 50 | normalTypes.put("Double", 0.0D); 51 | normalTypes.put("String", ""); 52 | normalTypes.put("BigDecimal", 0.0); 53 | normalTypes.put("Date", df.format(new Date())); 54 | normalTypes.put("Timestamp", System.currentTimeMillis()); 55 | normalTypes.put("LocalDate", LocalDate.now().toString()); 56 | normalTypes.put("LocalTime", LocalTime.now().toString()); 57 | normalTypes.put("LocalDateTime", LocalDateTime.now().toString()); 58 | 59 | } 60 | 61 | private static boolean isNormalType(String typeName) { 62 | return normalTypes.containsKey(typeName); 63 | } 64 | 65 | 66 | @Override 67 | public void actionPerformed(AnActionEvent e) { 68 | Editor editor = (Editor) e.getDataContext().getData(CommonDataKeys.EDITOR); 69 | PsiFile psiFile = (PsiFile) e.getDataContext().getData(CommonDataKeys.PSI_FILE); 70 | Project project = editor.getProject(); 71 | PsiElement referenceAt = psiFile.findElementAt(editor.getCaretModel().getOffset()); 72 | PsiClass selectedClass = (PsiClass) PsiTreeUtil.getContextOfType(referenceAt, new Class[]{PsiClass.class}); 73 | try { 74 | KV kv = getFields(selectedClass, Sets.newHashSet(selectedClass.getName())); 75 | String json = kv.toPrettyJson(); 76 | StringSelection selection = new StringSelection(json); 77 | Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); 78 | clipboard.setContents(selection, selection); 79 | String message = "Convert " + selectedClass.getName() + " to JSON success, copied to clipboard."; 80 | Notification success = notificationGroup.createNotification(message, NotificationType.INFORMATION); 81 | Notifications.Bus.notify(success, project); 82 | } catch (Exception ex) { 83 | Notification error = notificationGroup.createNotification("Convert to JSON failed.", NotificationType.ERROR); 84 | Notifications.Bus.notify(error, project); 85 | } 86 | } 87 | 88 | 89 | public static KV getFields(PsiClass psiClass, Set classNames) { 90 | KV kv = KV.create(); 91 | KV commentKV = KV.create(); 92 | 93 | if (psiClass != null) { 94 | for (PsiField field : psiClass.getAllFields()) { 95 | PsiType type = field.getType(); 96 | String name = field.getName(); 97 | String jsonKey = getJsonKeyName(name,field.getText()); 98 | 99 | //doc comment 100 | if (field.getDocComment() != null && field.getDocComment().getText() != null) { 101 | commentKV.set(jsonKey, field.getDocComment().getText()); 102 | } 103 | 104 | if (type instanceof PsiPrimitiveType) { //primitive Type 105 | kv.set(jsonKey, PsiTypesUtil.getDefaultValue(type)); 106 | } else { //reference Type 107 | String fieldTypeName = type.getPresentableText(); 108 | if (isNormalType(fieldTypeName)) { //normal Type 109 | kv.set(jsonKey, normalTypes.get(fieldTypeName)); 110 | } else if (type instanceof PsiArrayType) { //array type 111 | PsiType deepType = type.getDeepComponentType(); 112 | ArrayList list = new ArrayList<>(); 113 | String deepTypeName = deepType.getPresentableText(); 114 | if (deepType instanceof PsiPrimitiveType) { 115 | list.add(PsiTypesUtil.getDefaultValue(deepType)); 116 | } else if (isNormalType(deepTypeName)) { 117 | list.add(normalTypes.get(deepTypeName)); 118 | } else { 119 | PsiClass deepTypePsiClass = PsiUtil.resolveClassInType(deepType); 120 | if (!classNames.contains(deepTypePsiClass.getName())) { 121 | classNames.add(deepTypePsiClass.getName()); 122 | list.add(getFields(deepTypePsiClass, Sets.newHashSet(classNames))); 123 | } 124 | } 125 | kv.set(jsonKey, list); 126 | } else if (fieldTypeName.contains("List")) { //list type 127 | PsiType iterableType = PsiUtil.extractIterableTypeParameter(type, false); 128 | PsiClass iterableClass = PsiUtil.resolveClassInClassTypeOnly(iterableType); 129 | ArrayList list = new ArrayList<>(); 130 | String classTypeName = iterableClass.getName(); 131 | if (isNormalType(classTypeName)) { 132 | list.add(normalTypes.get(classTypeName)); 133 | } else { 134 | if (!classNames.contains(iterableClass.getName())) { 135 | classNames.add(iterableClass.getName()); 136 | list.add(getFields(iterableClass, Sets.newHashSet(classNames))); 137 | } 138 | } 139 | kv.set(jsonKey, list); 140 | } else if (PsiUtil.resolveClassInClassTypeOnly(type).isEnum()) { //enum 141 | ArrayList namelist = new ArrayList(); 142 | PsiField[] fieldList = 143 | PsiUtil.resolveClassInClassTypeOnly(type).getFields(); 144 | if (fieldList != null) { 145 | for (PsiField f : fieldList) { 146 | if (f instanceof PsiEnumConstant) { 147 | namelist.add(f.getName()); 148 | } 149 | } 150 | } 151 | kv.set(jsonKey, namelist); 152 | } else { //class type 153 | //System.out.println(name + ":" + type); 154 | PsiClass referencePsiClass = PsiUtil.resolveClassInType(type); 155 | if (!classNames.contains(referencePsiClass.getName())) { 156 | classNames.add(referencePsiClass.getName()); 157 | kv.set(jsonKey, getFields(referencePsiClass, Sets.newHashSet(classNames))); 158 | } 159 | } 160 | } 161 | } 162 | 163 | if (isShowComment && commentKV.size() > 0) { 164 | kv.set("@comment", commentKV); 165 | } 166 | } 167 | 168 | return kv; 169 | } 170 | 171 | private static String getJsonKeyName(String name, String text) { 172 | 173 | String jsonKey = name; 174 | if (text ==null||text ==""){ 175 | return jsonKey; 176 | } 177 | String regPattern = "@JsonProperty\\(\"([\\w\\d_]+)\"\\)"; 178 | Pattern pattern = Pattern.compile(regPattern); 179 | Matcher matcher = pattern.matcher(text); 180 | if (matcher.find()){ 181 | jsonKey = matcher.group(1).split(",")[0]; 182 | } 183 | return jsonKey; 184 | } 185 | } 186 | -------------------------------------------------------------------------------- /src/com/linsage/KV.java: -------------------------------------------------------------------------------- 1 | package com.linsage; 2 | 3 | import com.google.gson.Gson; 4 | import com.google.gson.GsonBuilder; 5 | 6 | import java.util.HashMap; 7 | import java.util.LinkedHashMap; 8 | import java.util.Map; 9 | 10 | /** 11 | * Map简化操作类 12 | * 13 | * @author linsage 14 | * @create 2017-06-14 上午11:06 15 | */ 16 | public class KV extends LinkedHashMap { 17 | public KV() { 18 | } 19 | 20 | public static KV by(K key, V value) { 21 | return new KV().set(key, value); 22 | } 23 | 24 | public static KV create() { 25 | return new KV(); 26 | } 27 | 28 | public KV set(K key, V value) { 29 | super.put(key, value); 30 | return this; 31 | } 32 | 33 | public KV set(Map map) { 34 | super.putAll(map); 35 | return this; 36 | } 37 | 38 | public KV set(KV KV) { 39 | super.putAll(KV); 40 | return this; 41 | } 42 | 43 | public KV delete(Object key) { 44 | super.remove(key); 45 | return this; 46 | } 47 | 48 | public T getAs(Object key) { 49 | return (T) get(key); 50 | } 51 | 52 | public String getStr(Object key) { 53 | return (String) get(key); 54 | } 55 | 56 | public Integer getInt(Object key) { 57 | return (Integer) get(key); 58 | } 59 | 60 | public Long getLong(Object key) { 61 | return (Long) get(key); 62 | } 63 | 64 | public Boolean getBoolean(Object key) { 65 | return (Boolean) get(key); 66 | } 67 | 68 | public Float getFloat(Object key) { 69 | return (Float) get(key); 70 | } 71 | 72 | 73 | /** 74 | * key 存在,并且 value 不为 null 75 | */ 76 | public boolean notNull(Object key) { 77 | return get(key) != null; 78 | } 79 | 80 | /** 81 | * key 不存在,或者 key 存在但 value 为null 82 | */ 83 | public boolean isNull(Object key) { 84 | return get(key) == null; 85 | } 86 | 87 | /** 88 | * key 存在,并且 value 为 true,则返回 true 89 | */ 90 | public boolean isTrue(Object key) { 91 | Object value = get(key); 92 | return (value instanceof Boolean && ((Boolean) value == true)); 93 | } 94 | 95 | /** 96 | * key 存在,并且 value 为 false,则返回 true 97 | */ 98 | public boolean isFalse(Object key) { 99 | Object value = get(key); 100 | return (value instanceof Boolean && ((Boolean) value == false)); 101 | } 102 | 103 | public String toJson() { 104 | return new Gson().toJson(this); 105 | } 106 | 107 | public String toPrettyJson() { 108 | return new GsonBuilder().setPrettyPrinting().create().toJson(this); 109 | } 110 | 111 | public boolean equals(Object KV) { 112 | return KV instanceof KV && super.equals(KV); 113 | } 114 | 115 | 116 | } 117 | -------------------------------------------------------------------------------- /src/com/linsage/menu/SponsorAction.java: -------------------------------------------------------------------------------- 1 | package com.linsage.menu; 2 | 3 | import com.intellij.ide.BrowserUtil; 4 | import com.intellij.openapi.actionSystem.AnAction; 5 | import com.intellij.openapi.actionSystem.AnActionEvent; 6 | 7 | public class SponsorAction extends AnAction { 8 | @Override 9 | public void actionPerformed(AnActionEvent anActionEvent) { 10 | BrowserUtil.browse("https://www.paypal.me/linsage"); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/com/linsage/menu/ToggleCommentAction.java: -------------------------------------------------------------------------------- 1 | package com.linsage.menu; 2 | 3 | import com.intellij.icons.AllIcons; 4 | import com.intellij.openapi.actionSystem.AnAction; 5 | import com.intellij.openapi.actionSystem.AnActionEvent; 6 | import com.linsage.Java2JsonAction; 7 | 8 | public class ToggleCommentAction extends AnAction { 9 | @Override 10 | public void actionPerformed(AnActionEvent e) { 11 | Java2JsonAction.isShowComment = !Java2JsonAction.isShowComment; 12 | e.getPresentation().setIcon(Java2JsonAction.isShowComment ? AllIcons.Actions.Checked : null); 13 | } 14 | } 15 | --------------------------------------------------------------------------------
A simple plugin for converting Java bean to JSON
When you post json request using Postman :)