perf[storage]: 优化storage,调整了结构并减少了重复的代码块

This commit is contained in:
godot
2022-07-08 15:34:10 +08:00
parent a6a276c8de
commit ba3e9051bc
41 changed files with 326 additions and 335 deletions
@@ -14,13 +14,11 @@ package com.zfoo.protocol.util;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.PrettyPrinter;
import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
import com.zfoo.protocol.exception.RunException;
@@ -29,7 +27,7 @@ import java.io.IOException;
import java.util.*;
/**
* @author jaysunxiao
* @author godotg
* @version 3.0
*/
public abstract class JsonUtils {
@@ -58,12 +56,10 @@ public abstract class JsonUtils {
//当反序列化有未知属性则抛异常,true打开这个设置
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
//美化输出
SerializationConfig config = MAPPER.getSerializationConfig();
PrettyPrinter prettyPrinter = config.getDefaultPrettyPrinter();
DefaultPrettyPrinter defpp = (DefaultPrettyPrinter) prettyPrinter;
DefaultPrettyPrinter.Indenter indenter = new DefaultIndenter(" ", DefaultIndenter.SYS_LF);
defpp.indentArraysWith(indenter);
defpp.indentObjectsWith(indenter);
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);
@@ -88,7 +84,7 @@ public abstract class JsonUtils {
}
}
//格式化/美化/优雅的输出
// 格式化/美化/优雅的输出
public static String object2StringPrettyPrinter(Object object) {
try {
return MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(object);
@@ -214,9 +210,9 @@ public abstract class JsonUtils {
// 循环遍历子节点下的信息
while (iterator.hasNext()) {
var node = iterator.next();
var filed = node.getKey();
var field = node.getKey();
var value = node.getValue().asText();
jsonMap.put(filed, value);
jsonMap.put(field, value);
}
}
return jsonMap;
@@ -10,7 +10,6 @@
* See the License for the specific language governing permissions and limitations under the License.
*
*/
package com.zfoo.protocol.util;
import com.zfoo.protocol.model.Triple;
@@ -21,11 +20,9 @@ import org.junit.Test;
import java.util.*;
/**
* @author jaysunxiao
* @author godotg
* @version 3.0
*/
public class JsonUtilTest {
public static String id = "\"id\":\"1000\"";
@@ -114,5 +111,11 @@ public class JsonUtilTest {
var tripleStr = JsonUtils.object2String(triple);
var temp = JsonUtils.string2Object(tripleStr, Triple.class);
}
@Test
public void prettyPrinterTest() {
var user = JsonUtils.string2Object(userJson, User.class);
System.out.println(JsonUtils.object2StringPrettyPrinter(user));
}
}