2

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

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

我在 Scala 中创建了一个推土机自定义转换器,在这里:

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 消息如下所示(shortFee并且longFee应该从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

当我运行上面的代码时,msg将两者都shortFee设置longFee0.0,而不是2.0and 3.0。对我做错了什么有任何想法吗?也许有更好的方法(更好的库?)来回转换这些对象?谢谢!

4

0 回答 0