如何将 Java/Scala 对象转换为 Protocol Buffers 消息?
我正在尝试将 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 消息看起来像以下(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
和 longFee
设置为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论