corda - 如何将 UniqueIdentifier 序列化为字符串并返回
问题描述
我需要UniqueIdentifier
在两个服务之间传递一个字符串。不幸的是UniqueIdentifier#toString
,UniqueIdentifier.fromString
如果externalId
设置了 an,则效果不佳。
序列化和反序列化 a 的规范方法是UniqueIdentifier
什么?
解决方案
如果您使用 Corda 的库创建UniqueIdentifier
具有自定义外部 id 的集合,您可以看到toString()
将生成${externalId}_$id
ie模式dummyExternalId_10ed0cc3-7bdf-4000-b610-595e36667d7d
。
因此,要将其从该字符串转换回 UniqueIdentifier,只需按分隔符分隔_
if (p.text.contains("_")) {
val ids = p.text.split("_")
//Create UUID object from string.
val uuid: UUID = UUID.fromString(ids[1])
//Create UniqueIdentifier object using externalId and UUID.
return UniqueIdentifier(ids[0], uuid)
}
链接在这里
如果您在外部 id 中有下划线,您可能需要自己的函数。
val potentialIds = input.split("_")
// Drop last one and stitch back the external id
val externalIdString = potentialIds.dropLast(1).joinToString("_")
// Last one is the UUID
val uuid = UUID.fromString(potentialIds.last())
val finalUniqueIdentifier = UniqueIdentifier(externalIdString, uuid)
推荐阅读
- python - Python Selenium 在没有 driver.refresh() 的情况下查询页面元素
- c - 将字符串拆分为不同的部分,没有空格和分隔符
- python - 日期/月份计算
- python - TensorFlow - TFRecords 加载和转换带有边界框的图像
- amazon-web-services - AWS CLI 无法向 API Gateway 阶段变量添加权限
- javascript - 图像旁边的定位按钮
- blazor-webassembly - Blazor Webassembly 应用程序如何知道哪个用户已登录服务器端?
- kubernetes - 数据未跨容器共享
- python - 似乎不明白错误的原因
- regex - 为什么 sed 的正则表达式分组与普通正则表达式不同?