From 5b8a090002d1eb221aba2b5abc8097456fc60d31 Mon Sep 17 00:00:00 2001 From: wangjun Date: Wed, 1 Apr 2020 11:02:07 +0800 Subject: [PATCH 1/5] =?UTF-8?q?JbootJson=20=E6=94=AF=E6=8C=81=E9=A9=BC?= =?UTF-8?q?=E5=B3=B0=E5=BC=8FJsonKey?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/jboot/web/JbootJson.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/jboot/web/JbootJson.java b/src/main/java/io/jboot/web/JbootJson.java index 870625a1..d500754a 100644 --- a/src/main/java/io/jboot/web/JbootJson.java +++ b/src/main/java/io/jboot/web/JbootJson.java @@ -17,6 +17,7 @@ package io.jboot.web; import com.alibaba.fastjson.JSON; import com.jfinal.json.JFinalJson; +import com.jfinal.kit.StrKit; import java.util.Iterator; import java.util.Map; @@ -28,7 +29,23 @@ public class JbootJson extends JFinalJson { @Override protected String mapToJson(Map map, int depth) { optimizeMapAttrs(map); - return map == null || map.isEmpty() ? "null" : super.mapToJson(map, depth); + + StringBuilder sb = new StringBuilder(); + boolean first = true; + Iterator iter = map.entrySet().iterator(); + + sb.append('{'); + while(iter.hasNext()){ + if(first) + first = false; + else + sb.append(','); + + Map.Entry entry = (Map.Entry)iter.next(); + toKeyValue(StrKit.toCamelCase(String.valueOf(entry.getKey())), entry.getValue(), sb, depth); + } + sb.append('}'); + return sb.toString(); } -- Gitee From 9398abb2be454696adf8b22bc6a9e102f8f56434 Mon Sep 17 00:00:00 2001 From: wangjun Date: Wed, 1 Apr 2020 11:32:22 +0800 Subject: [PATCH 2/5] =?UTF-8?q?JbootJson=20=E6=94=AF=E6=8C=81=E9=A9=BC?= =?UTF-8?q?=E5=B3=B0=E5=BC=8FJsonKey?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/jboot/web/JbootJson.java | 10 +++++++++- src/main/java/io/jboot/web/JbootWebConfig.java | 9 +++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/jboot/web/JbootJson.java b/src/main/java/io/jboot/web/JbootJson.java index d500754a..af365935 100644 --- a/src/main/java/io/jboot/web/JbootJson.java +++ b/src/main/java/io/jboot/web/JbootJson.java @@ -18,6 +18,7 @@ package io.jboot.web; import com.alibaba.fastjson.JSON; import com.jfinal.json.JFinalJson; import com.jfinal.kit.StrKit; +import io.jboot.Jboot; import java.util.Iterator; import java.util.Map; @@ -30,6 +31,13 @@ public class JbootJson extends JFinalJson { protected String mapToJson(Map map, int depth) { optimizeMapAttrs(map); + if(Jboot.config(JbootWebConfig.class).getCamelCaseJsonStyleEnable()){ + return toCamelCase(map, depth); + } + return map == null || map.isEmpty() ? "null" : super.mapToJson(map, depth); + } + + private String toCamelCase(Map map, int depth){ StringBuilder sb = new StringBuilder(); boolean first = true; Iterator iter = map.entrySet().iterator(); @@ -42,7 +50,7 @@ public class JbootJson extends JFinalJson { sb.append(','); Map.Entry entry = (Map.Entry)iter.next(); - toKeyValue(StrKit.toCamelCase(String.valueOf(entry.getKey())), entry.getValue(), sb, depth); + toKeyValue(StrKit.toCamelCase(String.valueOf(entry.getKey())),entry.getValue(), sb, depth); } sb.append('}'); return sb.toString(); diff --git a/src/main/java/io/jboot/web/JbootWebConfig.java b/src/main/java/io/jboot/web/JbootWebConfig.java index da22e047..74048a4b 100644 --- a/src/main/java/io/jboot/web/JbootWebConfig.java +++ b/src/main/java/io/jboot/web/JbootWebConfig.java @@ -30,6 +30,7 @@ public class JbootWebConfig { private String cookieEncryptKey = DEFAULT_COOKIE_ENCRYPT_KEY; private String webSocketEndpoint; + private Boolean camelCaseJsonStyleEnable = false; public String getCookieEncryptKey() { return cookieEncryptKey; @@ -46,4 +47,12 @@ public class JbootWebConfig { public void setWebSocketEndpoint(String webSocketEndpoint) { this.webSocketEndpoint = webSocketEndpoint; } + + public Boolean getCamelCaseJsonStyleEnable() { + return camelCaseJsonStyleEnable; + } + + public void setCamelCaseJsonStyleEnable(Boolean camelCaseJsonStyleEnable) { + this.camelCaseJsonStyleEnable = camelCaseJsonStyleEnable; + } } -- Gitee From b207fb6c463609f34d99335c3ced081a3e8e294d Mon Sep 17 00:00:00 2001 From: wangjun Date: Wed, 1 Apr 2020 11:41:56 +0800 Subject: [PATCH 3/5] =?UTF-8?q?JbootJson=20=E6=94=AF=E6=8C=81=E9=A9=BC?= =?UTF-8?q?=E5=B3=B0=E5=BC=8FJsonKey?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/jboot/web/JbootJson.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/jboot/web/JbootJson.java b/src/main/java/io/jboot/web/JbootJson.java index af365935..45e2a11c 100644 --- a/src/main/java/io/jboot/web/JbootJson.java +++ b/src/main/java/io/jboot/web/JbootJson.java @@ -26,12 +26,20 @@ import java.util.Map; public class JbootJson extends JFinalJson { + private static JbootWebConfig config; + + public static JbootWebConfig getConfig() { + if (config == null) { + config = Jboot.config(JbootWebConfig.class); + } + return config; + } @Override protected String mapToJson(Map map, int depth) { optimizeMapAttrs(map); - if(Jboot.config(JbootWebConfig.class).getCamelCaseJsonStyleEnable()){ + if(getConfig().getCamelCaseJsonStyleEnable()){ return toCamelCase(map, depth); } return map == null || map.isEmpty() ? "null" : super.mapToJson(map, depth); -- Gitee From 1a3d05b7e4a175920b9ada4e812e0a8a7deaf085 Mon Sep 17 00:00:00 2001 From: wangjun Date: Wed, 1 Apr 2020 11:45:36 +0800 Subject: [PATCH 4/5] =?UTF-8?q?JbootJson=20=E6=94=AF=E6=8C=81=E9=A9=BC?= =?UTF-8?q?=E5=B3=B0=E5=BC=8FJsonKey?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/jboot/web/JbootWebConfig.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/jboot/web/JbootWebConfig.java b/src/main/java/io/jboot/web/JbootWebConfig.java index 74048a4b..36bf5488 100644 --- a/src/main/java/io/jboot/web/JbootWebConfig.java +++ b/src/main/java/io/jboot/web/JbootWebConfig.java @@ -30,7 +30,7 @@ public class JbootWebConfig { private String cookieEncryptKey = DEFAULT_COOKIE_ENCRYPT_KEY; private String webSocketEndpoint; - private Boolean camelCaseJsonStyleEnable = false; + private boolean camelCaseJsonStyleEnable = false; public String getCookieEncryptKey() { return cookieEncryptKey; @@ -48,11 +48,11 @@ public class JbootWebConfig { this.webSocketEndpoint = webSocketEndpoint; } - public Boolean getCamelCaseJsonStyleEnable() { + public boolean getCamelCaseJsonStyleEnable() { return camelCaseJsonStyleEnable; } - public void setCamelCaseJsonStyleEnable(Boolean camelCaseJsonStyleEnable) { + public void setCamelCaseJsonStyleEnable(boolean camelCaseJsonStyleEnable) { this.camelCaseJsonStyleEnable = camelCaseJsonStyleEnable; } } -- Gitee From 4b48754ef730a56813e5cf707932265be8efc0c8 Mon Sep 17 00:00:00 2001 From: wangjun Date: Wed, 1 Apr 2020 11:46:45 +0800 Subject: [PATCH 5/5] =?UTF-8?q?JbootJson=20=E6=94=AF=E6=8C=81=E9=A9=BC?= =?UTF-8?q?=E5=B3=B0=E5=BC=8FJsonKey?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/jboot/web/JbootJson.java | 2 +- src/main/java/io/jboot/web/JbootWebConfig.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/jboot/web/JbootJson.java b/src/main/java/io/jboot/web/JbootJson.java index 45e2a11c..f01b5b05 100644 --- a/src/main/java/io/jboot/web/JbootJson.java +++ b/src/main/java/io/jboot/web/JbootJson.java @@ -39,7 +39,7 @@ public class JbootJson extends JFinalJson { protected String mapToJson(Map map, int depth) { optimizeMapAttrs(map); - if(getConfig().getCamelCaseJsonStyleEnable()){ + if(getConfig().isCamelCaseJsonStyleEnable()){ return toCamelCase(map, depth); } return map == null || map.isEmpty() ? "null" : super.mapToJson(map, depth); diff --git a/src/main/java/io/jboot/web/JbootWebConfig.java b/src/main/java/io/jboot/web/JbootWebConfig.java index 36bf5488..287f5855 100644 --- a/src/main/java/io/jboot/web/JbootWebConfig.java +++ b/src/main/java/io/jboot/web/JbootWebConfig.java @@ -48,7 +48,7 @@ public class JbootWebConfig { this.webSocketEndpoint = webSocketEndpoint; } - public boolean getCamelCaseJsonStyleEnable() { + public boolean isCamelCaseJsonStyleEnable() { return camelCaseJsonStyleEnable; } -- Gitee