在 Java 项目中实现 dsljson 时,我发现这是不正确的。它非常缓慢且难以实现。
我创建了从 JsonObject
实现的新对象
public static class abc implements JsonObject {
public final int x;
public final String s;
public abc(int x, String s) {
this.x = x;
this.s = s;
}
public void serialize(JsonWriter writer, boolean minimal) {
//parse the instance of object (abc) to json-string
}
public static final JsonReader.ReadJsonObject<abc> JSON_READER =
new JsonReader.ReadJsonObject<abc>() {
public abc deserialize(JsonReader reader) throws IOException {
// Use jsonreader and common json converter (numberconverter,
// stringconverter) to parse json-string to an
// instance of object (abc)
}
};
}
我创建新的:DslJson<Object> dslJson = new DslJson<Object>();
使用时调用“反序列化”/“序列化”。
我认为,我的实现不正确,因此速度太慢。
因此,如果您对此有任何经验或示例lib
那么你能帮我提供一下你的想法吗?
我们还有其他方式使用
dsljson
?DslJson
不能使用像JackSon
?ObjectMapper mapper = new ObjectMapper(); String jsonInString = "{\"age\":33,\"messages\":[\"msg 1\",\"msg 2\"], \"name\":\"mkyong\"}"; User user1 = mapper.readValue(jsonInString, User.class);
最佳答案
库存储库中有几个示例,例如:https://github.com/ngs-doo/dsl-json/blob/master/examples/Maven/src/main/java/com/dslplatform/maven/Example.java#L82
一些值得尝试的事情:
- 重用 dslJson 实例 - 在测试期间多次重新创建它的成本很高
- 避免使用字符串 - byte[] 或 Streams 是 GC 更友好的数据类型
如果 DslJson 不是最快的,很可能您的设置有问题:)(这意味着您应该显示更多代码 - 您测试/基准库的准确程度如何)
关于java - Java项目中的DSL-Json太慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45342522/