JSON4S不序列化Java课程

发布于 2025-01-27 18:35:45 字数 794 浏览 3 评论 0原文

我有一些Scala代码,需要能够使用JSON4S序列化/估算一些Java类。

我正在使用“ org.json4s” %%“ json4s-ext”%“ 4.0.5”“ org.json4s” %%“ JSON4S-JACKSON”%4.0.5“我也尝试了3.6.7版本。

模型代码(Java):

import com.fasterxml.jackson.annotation.JsonProperty;

public class Blah {
    @JsonProperty("what")
    public final String what;

    public Blah() {
        this(null);
    }
    public Blah(String what) {
        this.what = what;
    }
}

序列化(Scala):

import org.json4s.DefaultFormats
import org.json4s.jackson.Serialization

println(Serialization.write(new Blah("helloooo!!!!"))(DefaultFormats))

它仅打印出:{}

我了解我可以为每个Java类编写CustomSerializer,但是我有很多Java类,并且真的很想避免这样做。关于如何使这项工作的想法?

I have some scala code that needs to be able to serialize/deserialize some Java classes using Json4s.

I am using "org.json4s" %% "json4s-ext" % "4.0.5" and "org.json4s" %% "json4s-jackson" % "4.0.5" though I have also tried with the 3.6.7 version.

Model Code (Java):

import com.fasterxml.jackson.annotation.JsonProperty;

public class Blah {
    @JsonProperty("what")
    public final String what;

    public Blah() {
        this(null);
    }
    public Blah(String what) {
        this.what = what;
    }
}

Serialization (Scala):

import org.json4s.DefaultFormats
import org.json4s.jackson.Serialization

println(Serialization.write(new Blah("helloooo!!!!"))(DefaultFormats))

It only ever prints out: {}.

I understand I can write a CustomSerializer for each Java class but I have a lot of Java classes and would really like to avoid doing that. Any ideas on how to make this work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遇见了你 2025-02-03 18:35:45

JSON4S是Scala的JSON库。要使用Java对象,建议直接使用Jackson。请参阅此问题在这里

JSON4S仅使用Jackson作为解析器,在解析后,这一切都可以通过JSON4S AST与Scala对象进行匹配。

如果您想与Java收藏合作,则应使用Jackson
直接

因此,我们可以获得mapper,它是杰克逊的objectMapper的对象,并正常使用它 -

import org.json4s.jackson.JsonMethods

println(JsonMethods.mapper.writeValueAsString(new Blah("helloooo!!!!")))

Json4s is the Json library for Scala. To work with Java objects, it is recommended to directly use Jackson. See this question here

Json4s uses jackson only as a parser, and after parsing, it all works by matching from Json4s AST to Scala objects.

If you want to work with Java collections, you should use Jackson
directly

So, we can get the mapper which is an object of Jackson's ObjectMapper and use it normally -

import org.json4s.jackson.JsonMethods

println(JsonMethods.mapper.writeValueAsString(new Blah("helloooo!!!!")))
泛滥成性 2025-02-03 18:35:45

尝试为您的添加Getter方法

Try to add getter method for your what property

话少心凉 2025-02-03 18:35:45

JSON4S忽略任何JSONPROPERTY注释,需要为Java模型类的每个属性提供构造函数参数和Scala Case-class样式Getter方法。在上面的示例中,看起来像:

public class Blah {
    private final String what;

    public String what() {
        return what;
    }

    public Blah() {
        this(null);
    }
    public Blah(String what) {
        this.what = what;
    }
}

Json4s ignores any JsonProperty annotation and requires both a constructor parameter and a Scala case-class style getter method for each property of your Java model class. In the example above that would look like:

public class Blah {
    private final String what;

    public String what() {
        return what;
    }

    public Blah() {
        this(null);
    }
    public Blah(String what) {
        this.what = what;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文