mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-12 06:22:58 +00:00
perf[storage]: 优化json输出格式,减少换行,增加行号
This commit is contained in:
@@ -59,7 +59,6 @@ public abstract class JsonUtils {
|
||||
DefaultPrettyPrinter prettyPrinter = (DefaultPrettyPrinter) MAPPER.getSerializationConfig().getDefaultPrettyPrinter();
|
||||
DefaultPrettyPrinter.Indenter indenter = new DefaultIndenter(StringUtils.TAB_ASCII, FileUtils.LS);
|
||||
prettyPrinter.indentObjectsWith(indenter);
|
||||
prettyPrinter.indentArraysWith(indenter);
|
||||
|
||||
MAPPER_TURBO.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
MAPPER_TURBO.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
|
||||
|
||||
@@ -76,11 +76,11 @@ public class ResourceReader implements IResourceReader {
|
||||
var iterator = resource.getRows().iterator();
|
||||
// 从ROW_SERVER这行开始读取数据
|
||||
while (iterator.hasNext()) {
|
||||
var row = iterator.next();
|
||||
var columns = iterator.next().getColumns();
|
||||
var instance = ReflectionUtils.newInstance(clazz);
|
||||
|
||||
for (var fieldInfo : fieldInfos) {
|
||||
var content = row.get(fieldInfo.index);
|
||||
var content = columns.get(fieldInfo.index);
|
||||
if (StringUtils.isNotEmpty(content) || fieldInfo.field.getType() == String.class) {
|
||||
inject(instance, fieldInfo.field, content);
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ public class ResourceData {
|
||||
// 配置表字段名
|
||||
private List<ResourceHeader> headers = new ArrayList<>();
|
||||
// 配置表数据
|
||||
private List<List<String>> rows = new ArrayList<>();
|
||||
private List<ResourceRow> rows = new ArrayList<>();
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
@@ -45,11 +45,11 @@ public class ResourceData {
|
||||
this.headers = headers;
|
||||
}
|
||||
|
||||
public List<List<String>> getRows() {
|
||||
public List<ResourceRow> getRows() {
|
||||
return rows;
|
||||
}
|
||||
|
||||
public void setRows(List<List<String>> rows) {
|
||||
public void setRows(List<ResourceRow> rows) {
|
||||
this.rows = rows;
|
||||
}
|
||||
|
||||
|
||||
@@ -59,6 +59,11 @@ public enum ResourceEnum {
|
||||
return typeMap.containsKey(type);
|
||||
}
|
||||
|
||||
public static boolean isExcel(String type) {
|
||||
var resourceEnum = getResourceEnumByType(type);
|
||||
return resourceEnum == ResourceEnum.EXCEL_XLS || resourceEnum == ResourceEnum.EXCEL_XLSX;
|
||||
}
|
||||
|
||||
public static String typesToString() {
|
||||
return StringUtils.joinWith(StringUtils.SPACE, typeMap.values().toArray());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
package com.zfoo.storage.model.resource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
* @version 4.0
|
||||
*/
|
||||
public class ResourceRow {
|
||||
|
||||
private int row;
|
||||
|
||||
private List<String> columns = new ArrayList<>();
|
||||
|
||||
public static ResourceRow valueOf(int row, List<String> columns) {
|
||||
var resourceRow = new ResourceRow();
|
||||
resourceRow.row = row;
|
||||
resourceRow.columns = columns;
|
||||
return resourceRow;
|
||||
}
|
||||
|
||||
public int getRow() {
|
||||
return row;
|
||||
}
|
||||
|
||||
public void setRow(int row) {
|
||||
this.row = row;
|
||||
}
|
||||
|
||||
public List<String> getColumns() {
|
||||
return columns;
|
||||
}
|
||||
|
||||
public void setColumns(List<String> columns) {
|
||||
this.columns = columns;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,6 +20,7 @@ import com.zfoo.protocol.util.StringUtils;
|
||||
import com.zfoo.storage.model.resource.ResourceData;
|
||||
import com.zfoo.storage.model.resource.ResourceEnum;
|
||||
import com.zfoo.storage.model.resource.ResourceHeader;
|
||||
import com.zfoo.storage.model.resource.ResourceRow;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.apache.poi.ss.usermodel.WorkbookFactory;
|
||||
@@ -42,14 +43,18 @@ public class ExcelToJsonUtils {
|
||||
public static void excelConvertJson(String inputDir, String outputDir) throws IOException {
|
||||
var listFiles = FileUtils.getAllReadableFiles(new File(inputDir))
|
||||
.stream()
|
||||
.filter(it -> ResourceEnum.containsResourceEnum(FileUtils.fileExtName(it.getName())))
|
||||
.filter(it -> ResourceEnum.isExcel(FileUtils.fileExtName(it.getName())))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
for (var file : listFiles) {
|
||||
var fileName = StringUtils.format("{}.json", FileUtils.fileSimpleName(file.getName()));
|
||||
var fileSimpleName = FileUtils.fileSimpleName(file.getName());
|
||||
var jsonFileName = StringUtils.format("{}.json", fileSimpleName);
|
||||
var inputStream = FileUtils.openInputStream(file);
|
||||
var resourceData = readResourceDataFromExcel(inputStream, fileName);
|
||||
FileUtils.writeStringToFile(new File(FileUtils.joinPath(outputDir, fileName)), JsonUtils.object2StringPrettyPrinter(resourceData));
|
||||
var resourceData = readResourceDataFromExcel(inputStream, file.getName());
|
||||
|
||||
var outputFilePath = FileUtils.joinPath(outputDir, jsonFileName);
|
||||
FileUtils.deleteFile(new File(outputFilePath));
|
||||
FileUtils.writeStringToFile(new File(outputFilePath), JsonUtils.object2StringPrettyPrinter(resourceData));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,18 +75,18 @@ public class ExcelToJsonUtils {
|
||||
iterator.next();
|
||||
iterator.next();
|
||||
// 从ROW_SERVER这行开始读取数据
|
||||
List<List<String>> data = new ArrayList<>();
|
||||
List<ResourceRow> rows = new ArrayList<>();
|
||||
while (iterator.hasNext()) {
|
||||
var row = iterator.next();
|
||||
List<String> rowData = new ArrayList<>();
|
||||
var columns = new ArrayList<String>();
|
||||
for (var header : headers) {
|
||||
var cell = row.getCell(header.getIndex());
|
||||
var content = CellUtils.getCellStringValue(cell);
|
||||
rowData.add(content);
|
||||
columns.add(content);
|
||||
}
|
||||
data.add(rowData);
|
||||
rows.add(ResourceRow.valueOf(row.getRowNum(), columns));
|
||||
}
|
||||
resource.setRows(data);
|
||||
resource.setRows(rows);
|
||||
return resource;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user