public static String toJson(FieldItem fieldItem, List<String> paths) { int level = paths.size(); StringBuilder sb = new StringBuilder(); String fullTypeName = fieldItem.getFormatedTypeName(); if (paths.contains(fullTypeName)) { // 循环... sb.append(StringHelper.loopTab(level + 1)); sb.append("//{...} 循环 @type " + fullTypeName); return sb.toString(); } List<String> myPaths = new ArrayList<String>(); myPaths.addAll(paths); myPaths.add(fullTypeName); List<FieldItem> items = fieldItem.getItems(); String name = fieldItem.getName(); if (items.isEmpty()) { // 如果是List sb.append(DocletConstant.TURN_LINE); sb.append(StringHelper.loopTab(level + 1)); sb.append("\""); sb.append(name); sb.append("\""); sb.append(" : "); if ("java.util.List".equals(fullTypeName)) { sb.append(getListDemoValue(fieldItem, myPaths)); } else { sb.append(getDefaultDemoValue(fieldItem)); if (!fieldItem.isLastOne()) { sb.append(","); } sb.append(getCommentText(fieldItem)); } } else { if (StringHelper.isNotEmpty(name)) { sb.append(DocletConstant.TURN_LINE); sb.append(StringHelper.loopTab(level + 1)); sb.append("\""); sb.append(name); sb.append("\""); sb.append(" : "); } if (level > 0) { sb.append(StringHelper.loopTab(level + 1)); } sb.append("{"); sb.append(getCommentText(fieldItem)); items.get(items.size() - 1).setLastOne(true); for (FieldItem item : items) { sb.append(toJson(item, myPaths)); } sb.append(DocletConstant.TURN_LINE); sb.append(StringHelper.loopTab(level + 1)); sb.append("}"); } return sb.toString(); }
private static String getListDemoValue(FieldItem fieldItem, List<String> myPaths) { int level = myPaths.size(); StringBuilder sb = new StringBuilder(); sb.append("["); sb.append(getCommentText(fieldItem)); sb.append(DocletConstant.TURN_LINE); List<ClassMate> typeArguments = fieldItem.getTypeArguments(); ClassMate mate = typeArguments.get(0); FieldItem item = new FieldItem(); item.cloneClassMate(mate); sb.append(JsonFormater.toJson(item, myPaths)); sb.append(DocletConstant.TURN_LINE); sb.append(StringHelper.loopTab(level)); sb.append("]"); if (!fieldItem.isLastOne()) { sb.append(","); } return sb.toString(); }