如何将 Java/Scala 对象转换为 Protocol Buffers 消息?

发布于 2024-12-17 19:18:02 字数 2466 浏览 1 评论 0原文

我正在尝试将 Java/Scala 对象转换为 Protocol Buffers 消息。

我的对象名为 IDSNumber,它有一个 doubleValue 方法,该方法返回 java.lang.Double。我试图让它在 Dozer 对象转换期间自动转换为双精度。

我在 Scala 中创建了一个 Dozer Custom Converter,这里:

class IDSConverter extends DozerConverter[IDSNumber, java.lang.Double](classOf[IDSNumber], classOf[java.lang.Double]) {
    def convertTo(source: IDSNumber, destination: java.lang.Double): java.lang.Double = {
        source.doubleValue()
    }
    def convertFrom(source: java.lang.Double , destination: IDSNumber): IDSNumber = {
        new IDSNumber(source)
    }
}

我的 Protocol Buffers 消息看起来像以下(shortFeelongFee 应从 IDSNumber 类型转换):

message PerformanceSettingMessage {
    optional string                  displayCurrency           = 1;
    optional PeriodMessage           performancePeriod         = 2;
    optional int64                   conversionDate            = 3;
    optional BenchmarkOptionsMessage benchmarkOptions          = 4;
    optional string                  investmentStrategyMeaning = 5;
    optional double                  shortFee                  = 6;
    optional double                  longFee                   = 7;
}

我尝试按以下方式使用转换器:

    val longFee  = new IDSNumber(2.0)
    val shortFee = new IDSNumber(3.0)
    // newing up a real performance settings object with IDSNumbers for shortFee and longFee
    val performanceSettings = new PerformanceSettings(Currency.apply("$"), period, fj.data.Option.none(), fj.data.Option.none(), Investment_Weighted, shortFee, longFee)

    val mapper = new DozerBeanMapper() // new DozerMapper
    val idsConverter = new IDSConverter() // my IDSNumber converter
    mapper.setCustomConverters(java.util.Arrays.asList(idsConverter)) // make it use my IDSNumber converter
    val msg = mapper.map(performanceSettings, classOf[PerformanceSettingMessage]) // map my real performanceSettings object to my PerformanceSettingsMessage generated by Protocol Buffers

当我运行上面的代码, msgshortFeelongFee 设置为 0.0,而不是 2.0 和 <代码>3.0。对我做错了什么有什么想法吗?也许有更好的方法(更好的库?)来来回转换这些对象?谢谢!

I'm trying to convert a Java/Scala object into a Protocol Buffers message.

My object is called IDSNumber and it has a doubleValue method which returns a java.lang.Double. I'm trying to get it to be converted automatically to a double during Dozer object conversion.

I've create a Dozer Custom Converter in Scala, here:

class IDSConverter extends DozerConverter[IDSNumber, java.lang.Double](classOf[IDSNumber], classOf[java.lang.Double]) {
    def convertTo(source: IDSNumber, destination: java.lang.Double): java.lang.Double = {
        source.doubleValue()
    }
    def convertFrom(source: java.lang.Double , destination: IDSNumber): IDSNumber = {
        new IDSNumber(source)
    }
}

My Protocol Buffers message looks like the following (shortFee and longFee should be converted from the IDSNumber type):

message PerformanceSettingMessage {
    optional string                  displayCurrency           = 1;
    optional PeriodMessage           performancePeriod         = 2;
    optional int64                   conversionDate            = 3;
    optional BenchmarkOptionsMessage benchmarkOptions          = 4;
    optional string                  investmentStrategyMeaning = 5;
    optional double                  shortFee                  = 6;
    optional double                  longFee                   = 7;
}

I try to use the converter in the following way:

    val longFee  = new IDSNumber(2.0)
    val shortFee = new IDSNumber(3.0)
    // newing up a real performance settings object with IDSNumbers for shortFee and longFee
    val performanceSettings = new PerformanceSettings(Currency.apply("$"), period, fj.data.Option.none(), fj.data.Option.none(), Investment_Weighted, shortFee, longFee)

    val mapper = new DozerBeanMapper() // new DozerMapper
    val idsConverter = new IDSConverter() // my IDSNumber converter
    mapper.setCustomConverters(java.util.Arrays.asList(idsConverter)) // make it use my IDSNumber converter
    val msg = mapper.map(performanceSettings, classOf[PerformanceSettingMessage]) // map my real performanceSettings object to my PerformanceSettingsMessage generated by Protocol Buffers

When I run the above code, msg has both shortFee and longFee set to 0.0, instead of 2.0 and 3.0. Any thoughts on what I'm doing wrong? Perhaps there's a better way (better library?) to convert these objects back and forth? Thanks!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文