XdrJsonCodable
public protocol XdrJsonCodable
Conversion between an XDR type and its XDR-JSON form, as defined by SEP-0051.
Only the two tree operations are requirements. The two string-boundary conveniences are
derived from them on the extension below, the same way xdrEncoded is derived from
xdrEncode(to:).
There is deliberately no default implementation of the requirements. Reflection cannot
recover XDR field names, cannot see a void union arm at all, and cannot tell apart types
that a typealias collapses onto one another, so a default would be wrong rather than
merely incomplete. Leaving it out makes a type that has not been given a conversion a
compile error instead of wrong output at run time.
let bounds = TimeBoundsXDR(minTime: 0, maxTime: 1)
let json = try bounds.toXdrJson() // {"min_time":"0","max_time":"1"}
let same = try TimeBoundsXDR.fromXdrJson(json)
-
This value as an XDR-JSON tree, with object keys in XDR declaration order.
Declaration
Swift
func toXdrJsonValue() throws -> XdrJsonValue -
Reads a value from an XDR-JSON tree.
This is the requirement generated code calls recursively, so it performs no depth validation of its own: re-walking the tree at every level would be quadratic. A caller holding a tree it built by hand rather than parsed should use
fromXdrJsonTree(_:), which bounds the nesting once and then delegates here.Declaration
Swift
static func fromXdrJsonValue(_ value: XdrJsonValue) throws -> Self -
toXdrJson()Extension methodThis value as canonical XDR-JSON text: one line, no insignificant whitespace, keys in XDR declaration order.
Throws
XdrJsonErrorwhen a value has no XDR-JSON rendering, or when the tree nests too deeply.Declaration
Swift
func toXdrJson() throws -> String -
fromXdrJson(_:Extension method) Reads a value from XDR-JSON text.
Throws
malformedJson(message:)when the text is not well-formed JSON, and the otherXdrJsonErrorcases when it is well-formed but does not describe this type.Declaration
Swift
static func fromXdrJson(_ json: String) throws -> Self -
fromXdrJsonTree(_:Extension method) Reads a value from an XDR-JSON tree built by hand rather than parsed.
XdrJsonParseralready bounds the nesting of anything it produces, so this is the entry point that needs the check; conversions below it recurse no deeper than the tree does.Declaration
Swift
static func fromXdrJsonTree(_ value: XdrJsonValue) throws -> Self
View on GitHub
Install in Dash