diff --git a/src/main/java/io/jboot/web/JbootJson.java b/src/main/java/io/jboot/web/JbootJson.java index 870625a1c25b9319fd8874c1150c5d40fb2f2eb9..f01b5b0551b70b289fcb89794654df6826ab3583 100644 --- a/src/main/java/io/jboot/web/JbootJson.java +++ b/src/main/java/io/jboot/web/JbootJson.java @@ -17,6 +17,8 @@ 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; @@ -24,13 +26,44 @@ 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(getConfig().isCamelCaseJsonStyleEnable()){ + 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(); + + 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(); + } + /** * 优化 map 的属性 diff --git a/src/main/java/io/jboot/web/JbootWebConfig.java b/src/main/java/io/jboot/web/JbootWebConfig.java index da22e0474f3ed6c63136c086d5960821b5667839..287f5855458107cb49f250cf88bb57aa156d7d5a 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 isCamelCaseJsonStyleEnable() { + return camelCaseJsonStyleEnable; + } + + public void setCamelCaseJsonStyleEnable(boolean camelCaseJsonStyleEnable) { + this.camelCaseJsonStyleEnable = camelCaseJsonStyleEnable; + } }