首页 > 解决方案 > 如何将 UniqueIdentifier 序列化为字符串并返回

问题描述

我需要UniqueIdentifier在两个服务之间传递一个字符串。不幸的是UniqueIdentifier#toStringUniqueIdentifier.fromString如果externalId设置了 an,则效果不佳。

序列化和反序列化 a 的规范方法是UniqueIdentifier什么?

标签: corda

解决方案


如果您使用 Corda 的库创建UniqueIdentifier具有自定义外部 id 的集合,您可以看到toString()将生成${externalId}_$idie模式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)

推荐阅读