Java项目中的DSL-Json太慢了

时间:2017-07-27 06:04:07

标签: java json jackson dsl

在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,那么您可以帮助我提出您的想法吗?

  1. 我们还有其他方法可以使用dsljson吗?

  2. DslJson无法使用JackSon

    ObjectMapper mapper = new ObjectMapper();
    String jsonInString = "{\"age\":33,\"messages\":[\"msg 1\",\"msg 2\"],
                            \"name\":\"mkyong\"}";
    User user1 = mapper.readValue(jsonInString, User.class);
    

1 个答案:

答案 0 :(得分:1)

库存储库中的示例很少,例如: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不是最快的,很可能你的设置有问题:)(这意味着你应该显示更多代码 - 你究竟是如何测试/修改库)