[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/APIJSON/APIJSON/main/src/main/java/apijson/JSONResponse.java [Back]  [Original]

/*Copyright (C) 2026 the APIJSON group. All rights reserved. This source code is licensed under the Apache License Version 2.0.*/ package apijson; import java.util.*; /**parser for response * @author Lemon * @see #getObject * @see #getList * @use JSONResponse response = new JSONResponse(json); *
User user = response.getObject(User.class);//not a must *
List commenntList = response.getList("Comment[]", Comment.class);//not a must */ public interface JSONResponse extends JSONMap { static final String TAG = "JSONResponse"; // bug @key /** - */ public static boolean IS_FORMAT_HYPHEN = false; /** _ */ public static boolean IS_FORMAT_UNDERLINE = false; /** $ */ public static boolean IS_FORMAT_DOLLAR = false; //default JSONResponse() { // super(); //} //default JSONResponse(Object json) { // this(parseObject(json)); //} //default JSONResponse(Object json, JSONParser parser) { // this(parseObject(json, parser)); //} //default JSONResponse(Map object) { // super(format(object)); //} //default JSONResponse(M object, JSONCreator creator) { // super(format(object, creator)); //} //GET) {//JSONListformat formatedObject.put(formatArrayKey(key), format((L) value)); } else if (value instanceof Map) {//JSONRequest formatedObject.put(formatObjectKey(key), format((M) value)); } else {//Object formatedObject.put(formatOtherKey(key), value); } } } //debug Log.i(TAG, "format return formatedObject = " + JSON.toJSONString(formatedObject)); return formatedObject; } /**key * @param array * @return */ public static L format(final L array) { //debug Log.i(TAG, "format array = \n" + JSON.toJSONString(array)); if (array == null || array.isEmpty()) { Log.i(TAG, "format array == null || array.isEmpty() >> return array;"); return array; } L formattedArray = JSON.createJSONArray(); Object value; for (int i = 0; i < array.size(); i++) { value = array.get(i); if (value instanceof List) {//JSONListformat formattedArray.add(format((L) value)); } else if (value instanceof Map) {//JSONRequest formattedArray.add(format((M) value)); } else {//Object formattedArray.add(value); } } //debug Log.i(TAG, "format return formattedArray = " + JSON.toJSONString(formattedArray)); return formattedArray; } /** * @param fullName name name:alias * @return name => name; name:alias => alias */ public static String getTableName(String fullName) { //key:alias -> alias; key:alias[] -> alias[] int index = fullName == null ? -1 : fullName.indexOf(":"); return index < 0 ? fullName : fullName.substring(0, index); } /** * @param fullName * @return {@link #formatKey(String, boolean, boolean, boolean, boolean, boolean, Boolean)} formatColon = true, formatAt = true, formatHyphen = true, firstCase = true */ public static String getVariableName(String fullName) { if (JSONMap.isArrayKey(fullName)) { fullName = StringUtil.addSuffix(fullName.substring(0, fullName.length() - 2), "list"); } return formatKey(fullName, true, true, true, true, false, true); } /** key[] => keyList; key:alias[] => aliasList; Table-column[] => tableColumnList * @param key empty ? "list" : key + "List" * @return {@link #formatKey(String, boolean, boolean, boolean, boolean, boolean, Boolean)} formatColon = false, formatAt = true, formatHyphen = true, firstCase = true */ public static String formatArrayKey(String key) { if (JSONMap.isArrayKey(key)) { key = StringUtil.addSuffix(key.substring(0, key.length() - 2), "list"); } int index = key == null ? -1 : key.indexOf(":"); if (index >= 0) { return key.substring(index + 1); // } return formatKey(key, false, true, true, IS_FORMAT_UNDERLINE, IS_FORMAT_DOLLAR, false); // Table-column:alias[] } /** name => name; name:alias => alias * @param key name name:alias * @return {@link #formatKey(String, boolean, boolean, boolean, boolean, boolean, Boolean)} formatColon = false, formatAt = true, formatHyphen = false, firstCase = true */ public static String formatObjectKey(String key) { int index = key == null ? -1 : key.indexOf(":"); if (index >= 0) { return key.substring(index + 1); // } return formatKey(key, false, true, IS_FORMAT_HYPHEN, IS_FORMAT_UNDERLINE, IS_FORMAT_DOLLAR, false); // Table:alias } /** name => name; name:alias => alias * @param fullName name name:alias * @return {@link #formatKey(String, boolean, boolean, boolean, boolean, boolean, Boolean)} formatColon = false, formatAt = true, formatHyphen = false, firstCase = false */ public static String formatOtherKey(String fullName) { return formatKey(fullName, false, true, IS_FORMAT_HYPHEN, IS_FORMAT_UNDERLINE, IS_FORMAT_DOLLAR , IS_FORMAT_HYPHEN || IS_FORMAT_UNDERLINE || IS_FORMAT_DOLLAR ? false : null); } /** * @param fullName name name:alias * @param formatAt @ @a => a * @param formatColon : A:b => b * @param formatHyphen - A-b-cd-Efg => aBCdEfg * @param formatUnderline _ A_b_cd_Efg => aBCdEfg * @param formatDollar $ A$b$cd$Efg => aBCdEfg * @param firstCase Ab => ab ; A-b-Cd => aBCd * @return name => name; name:alias => alias */ public static String formatKey(String fullName, boolean formatColon, boolean formatAt, boolean formatHyphen , boolean formatUnderline, boolean formatDollar, Boolean firstCase) { if (fullName == null) { Log.w(TAG, "formatKey fullName == null >> return null;"); return null; } if (formatColon) { fullName = formatColon(fullName); } if (formatAt) { // @a-b a-b setter fullName = formatAt(fullName); } if (formatHyphen && fullName.contains("-")) { fullName = formatHyphen(fullName, true); } if (formatUnderline && fullName.contains("_")) { fullName = formatUnderline(fullName, true); } if (formatDollar && fullName.contains("$")) { fullName = formatDollar(fullName, true); } // key:value (value [], {}) key return firstCase == null ? fullName : StringUtil.firstCase(fullName, firstCase); } /**"@key" => "key" * @param key * @return */ public static String formatAt(@NotNull String key) { return key.startsWith("@") ? key.substring(1) : key; } /**key:alias => alias * @param key * @return */ public static String formatColon(@NotNull String key) { int index = key.indexOf(":"); return index < 0 ? key : key.substring(index + 1); } /**A-b-cd-Efg => ABCdEfg * @param key * @return */ public static String formatHyphen(@NotNull String key) { return StringUtil.firstCase(formatHyphen(key, true), false); } /**A-b-cd-Efg => ABCdEfg * @param key * @param firstCase true-false-null- * @return */ public static String formatHyphen(@NotNull String key, Boolean firstCase) { return formatHyphen(key, firstCase, false); } /**A-b-cd-Efg => ABCdEfg * @param key * @param firstCase true-false-null- * @param otherCase true-false-null- * @return */ public static String formatHyphen(@NotNull String key, Boolean firstCase, Boolean otherCase) { return formatDivider(key, "-", firstCase, otherCase); } /**A_b_cd_Efg => ABCdEfg * @param key * @return */ public static String formatUnderline(@NotNull String key) { return StringUtil.firstCase(formatUnderline(key, true), false); } /**A_b_cd_Efg => ABCdEfg * @param key * @param firstCase true-false-null- * @return */ public static String formatUnderline(@NotNull String key, Boolean firstCase) { return formatUnderline(key, firstCase, false); } /**A_b_cd_Efg => ABCdEfg * @param key * @param firstCase true-false-null- * @param otherCase true-false-null- * @return */ public static String formatUnderline(@NotNull String key, Boolean firstCase, Boolean otherCase) { return formatDivider(key, "_", firstCase, otherCase); } /**A$b$cd$Efg => ABCdEfg * @param key * @return */ public static String formatDollar(@NotNull String key) { return StringUtil.firstCase(formatDollar(key, true), false); } /**A$b$cd$Efg => ABCdEfg * @param key * @param firstCase true-false-null- * @return */ public static String formatDollar(@NotNull String key, Boolean firstCase) { return formatDollar(key, firstCase, false); } /**A$b$cd$Efg => ABCdEfg * @param key * @param firstCase true-false-null- * @param otherCase true-false-null- * @return */ public static String formatDollar(@NotNull String key, Boolean firstCase, Boolean otherCase) { return formatDivider(key, "$", firstCase, otherCase); } /**A.b.cd.Efg => ABCdEfg * @param key * @return */ public static String formatDot(@NotNull String key) { return StringUtil.firstCase(formatDot(key, true), false); } /**A.b.cd.Efg => ABCdEfg * @param key * @param firstCase true-false-null- * @return */ public static String formatDot(@NotNull String key, Boolean firstCase) { return formatDot(key, firstCase, false); } /**A.b.cd.Efg => ABCdEfg * @param key * @param firstCase true-false-null- * @param otherCase true-false-null- * @return */ public static String formatDot(@NotNull String key, Boolean firstCase, Boolean otherCase) { return formatDivider(key, ".", firstCase, otherCase); } /**A/b/cd/Efg => ABCdEfg * @param key * @return */ public static String formatDivider(@NotNull String key, Boolean firstCase) { return formatDivider(key, "/", firstCase); } /** * @param key * @param divider * @return */ public static String formatDivider(@NotNull String key, @NotNull String divider) { return StringUtil.firstCase(formatDivider(key, divider, true), false); } /** * @param key * @param divider * @param firstCase true-false-null- * @return */ public static String formatDivider(@NotNull String key, @NotNull String divider, Boolean firstCase) { return formatDivider(key, divider, firstCase, false); } /** * @param key * @param divider * @param firstCase true-false-null- * @param otherCase true-false-null- * @return */ public static String formatDivider(@NotNull String key, @NotNull String divider, Boolean firstCase, Boolean otherCase) { String[] parts = StringUtil.split(key, divider); StringBuilder name = new StringBuilder(); for (String part : parts) { if (otherCase != null) { part = otherCase ? part.toUpperCase() : part.toLowerCase(); } if (firstCase != null) { part = StringUtil.firstCase(part, firstCase); } name.append(part); } return name.toString(); } }
Web Proxy Viewer  |  New URL  |  Original Page