Previous << Contract Upgrades with Incompatible Changes
Next >> Measuring Time In Cadence
JSON-Cadenceは、Cadenceの値を言語に依存しないJSONオブジェクトとして表現するために使用されるデータ交換フォーマットです。
このフォーマットは完全なABIよりも型情報が少なく、代わりに以下の原則を推進しています。
- 人間にとって読みやすい - JSON-Cadenceは読みやすく理解しやすいので、開発やデバッグのスピードアップにつながります。
- 互換性 - JSONは、ほとんどの高レベルプログラミング言語に組み込まれたサポート機能を持つ共通フォーマットであるため、さまざまなプラットフォームで簡単に解析できます。
- 移植性 - JSON-Cadenceは自己記述型であるため、型定義(例えばABI)を伴わずに転送およびデコードが可能です。
Values
Void
{
"type": "Void"
}
例
{
"type": "Void"
}
Optional
{
"type": "Optional",
"value": null | <value>
}
例
/* Non-nil */
{
"type": "Optional",
"value": {
"type": "UInt8",
"value": "123"
}
}
/* Nil */
{
"type": "Optional",
"value": null
}
Bool
{
"type": "Bool",
"value": true | false
}
例
{
"type": "Bool",
"value": true
}
String
{
"type": "String",
"value": "..."
}
例
{
"type": "String",
"value": "Hello, world!"
}
Address
{
"type": "Address",
"value": "0x0" /* as hex-encoded string with 0x prefix */
}
例
{
"type": "Address",
"value": "0x1234"
}
Integers
[U]Int, [U]Int8, [U]Int16, [U]Int32, [U]Int64, [U]Int128, [U]Int256, Word8, Word16, Word32, Word64, Word128 or Word256
JSONは64ビットまでの整数リテラルをサポートしていますが、一貫性を保つため、すべての整数型は文字列としてエンコードされます。
デコードには厳密には静的型は必要ありませんが、クライアントに範囲の可能性を通知するために提供されます。
{
"type": "<type>",
"value": "<decimal string representation of integer>"
}
例
{
"type": "UInt8",
"value": "123"
}
Fixed Point Numbers
[U]Fix64
固定小数点数は整数として実装されていますが、JSON-Cadenceでは可読性を考慮して小数表記を使用しています。
{
"type": "[U]Fix64",
"value": "<integer>.<fractional>"
}
例
{
"type": "Fix64",
"value": "12.3"
}
Array
{
"type": "Array",
"value": [
<value at index 0>,
<value at index 1>
// ...
]
}
例
{
"type": "Array",
"value": [
{
"type": "Int16",
"value": "123"
},
{
"type": "String",
"value": "test"
},
{
"type": "Bool",
"value": true
}
]
}
Dictionary
Dictionaryは、Cadenceによって実装された決定論的順序を維持するために、キーと値のペアのリストとしてエンコードされます。{
"type": "Dictionary",
"value": [
{
"key": "<key>",
"value": <value>
},
...
]
}
例
{
"type": "Dictionary",
"value": [
{
"key": {
"type": "UInt8",
"value": "123"
},
"value": {
"type": "String",
"value": "test"
}
}
]
// ...
}
Composites
(Struct, Resource, Event, Contract, Enum)
Compositeのフィールドは、Compositeの型宣言に現れる順序で、名前と値のペアのリストとしてエンコードされます。(原文: Composite fields are encoded as a list of name-value pairs in the order in which they appear in the composite type declaration.)
{
"type": "Struct" | "Resource" | "Event" | "Contract" | "Enum",
"value": {
"id": "<fully qualified type identifier>",
"fields": [
{
"name": "<field name>",
"value": <field value>
},
// ...
]
}
}
例
{
"type": "Resource",
"value": {
"id": "0x3.GreatContract.GreatNFT",
"fields": [
{
"name": "power",
"value": { "type": "Int", "value": "1" }
}
]
}
}
Path
{
"type": "Path",
"value": {
"domain": "storage" | "private" | "public",
"identifier": "..."
}
}
例
{
"type": "Path",
"value": {
"domain": "storage",
"identifier": "flowTokenVault"
}
}
Type Value
{
"type": "Type",
"value": {
"staticType": <type>
}
}
例
{
"type": "Type",
"value": {
"staticType": {
"kind": "Int"
}
}
}
InclusiveRange
{
"type": "InclusiveRange",
"value": {
"start": <start_value>,
"end": <end_value>,
"step": <step_value>
}
}
例
{
"type": "InclusiveRange",
"value": {
"start": {
"type": "Int256",
"value": "10"
},
"end": {
"type": "Int256",
"value": "20"
},
"step": {
"type": "Int256",
"value": "5"
}
}
}
Capability
{
"type": "Capability",
"value": {
"id": <Number>,
"address": "0x0", /* as hex-encoded string with 0x prefix */
"borrowType": <type>,
}
}
例
{
"type": "Capability",
"value": {
"id": "1",
"address": "0x1",
"borrowType": {
"kind": "Int"
}
}
}
Functions
{
"type": "Function",
"value": {
"functionType": <type>
}
}
Functionの値はエクスポートのみ可能で、インポートはできません。
例
{
"type": "Function",
"value": {
"functionType": {
"kind": "Function",
"typeID": "fun():Void",
"parameters": [],
"return": {
"kind": "Void"
}
}
}
}
Types
Simple Types
これらは、Int、String、StoragePathのような基本的な型です。
{
"kind": <kind>
}
ここでkindは次のいずれかです。
AccountAccountCapabilityControllerAccountKeyAddressAnyResourceAnyResourceAttachmentAnyStructAnyStructAttachmentBlockBoolCapabilityCapabilityPathCharacterDeployedContractDeploymentResultFix64FixedPointFixedSizeUnsignedIntegerHashAlgorithmHashableStructIntInt128Int16Int256Int32Int64Int8IntegerNeverNumberPathPrivatePathPublicKeyPublicPathSignatureAlgorithmSignedFixedPointSignedIntegerSignedNumberStorageCapabilityControllerStoragePathStringTypeUFix64UIntUInt128UInt16UInt256UInt32UInt64UInt8VoidWord128Word16Word256Word32Word64Word8
例
{
"kind": "UInt8"
}
Optional Types
{
"kind": "Optional",
"type": <type>
}
例
{
"kind": "Optional",
"type": {
"kind": "String"
}
}
Variable Sized Array Types
{
"kind": "VariableSizedArray",
"type": <type>
}
例
{
"kind": "VariableSizedArray",
"type": {
"kind": "String"
}
}
Constant Sized Array Types
{
"kind": "ConstantSizedArray",
"type": <type>,
"size": <length of array>,
}
例
{
"kind": "ConstantSizedArray",
"type": {
"kind": "String"
},
"size": 3
}
Dictionary Types
{
"kind": "Dictionary",
"key": <type>,
"value": <type>
}
例
{
"kind": "Dictionary",
"key": {
"kind": "String"
},
"value": {
"kind": "UInt16"
}
}
Composite Types
{
"kind": "Struct" | "Resource" | "Event" | "Contract" | "StructInterface" | "ResourceInterface" | "ContractInterface",
"type": "", /* this field exists only to keep parity with the enum structure below; the value must be the empty string */
"typeID": "<fully qualified type ID>",
"initializers": [
<initializer at index 0>,
<initializer at index 1>
/* ... */
],
"fields": [
<field at index 0>,
<field at index 1>
/* ... */
],
}
例
{
"kind": "Resource",
"type": "",
"typeID": "0x3.GreatContract.GreatNFT",
"initializers": [
[
{
"label": "foo",
"id": "bar",
"type": {
"kind": "String"
}
}
]
],
"fields": [
{
"id": "foo",
"type": {
"kind": "String"
}
}
]
}
Field Types
{
"id": "<name of field>",
"type": <type>
}
例
{
"id": "foo",
"type": {
"kind": "String"
}
}
Parameter Types
{
"label": "<label>",
"id": "<identifier>",
"type": <type>
}
例
{
"label": "foo",
"id": "bar",
"type": {
"kind": "String"
}
}
Initializer Types
初期化子型には、初期化子(init関数)へのパラメータのリストがエンコードされています。
[
<parameter at index 0>,
<parameter at index 1>,
/* ... */
]
例
[
{
"label": "foo",
"id": "bar",
"type": {
"kind": "String"
}
}
]
Function Types
{
"kind": "Function",
"typeID": "<function name>",
"parameters": [
<parameter at index 0>,
<parameter at index 1>,
/* ... */
],
"purity: "view" | undefined,
"return": <type>
}
例
{
"kind": "Function",
"typeID": "foo",
"parameters": [
{
"label": "foo",
"id": "bar",
"type": {
"kind": "String"
}
}
],
"purity": "view",
"return": {
"kind": "String"
}
}
Reference Types
{
"kind": "Reference",
"authorization": {
"kind": "Unauthorized" | "EntitlementMapAuthorization" | "EntitlementConjunctionSet" | "EntitlementDisjunctionSet",
"entitlements": [
<entitlement at index 0>,
<entitlement at index 1>
/* ... */
]
},
"type": <type>
}
例
{
"kind": "Reference",
"authorization": {
{
"kind": "EntitlementMapAuthorization",
"entitlements": [
{
"kind": "EntitlementMap",
"typeID": "foo"
}
]
}
},
"type": {
"kind": "String"
}
}
Intersection Types
{
"kind": "Intersection",
"typeID": "<fully qualified type ID>",
"types": [
<type at index 0>,
<type at index 1>,
/* ... */
]
}
例
{
"kind": "Intersection",
"typeID": "{0x1.FungibleToken.Receiver}",
"types": [
{
"kind": "ResourceInterface",
"typeID": "0x1.FungibleToken.Receiver",
"fields": [
{
"id": "uuid",
"type": {
"kind": "UInt64"
}
}
],
"initializers": [],
"type": ""
}
]
}
Capability Types
{
"kind": "Capability",
"type": <type>
}
例
{
"kind": "Capability",
"type": {
"kind": "Reference",
"authorization": {
"kind": "Unauthorized",
"entitlements": null
},
"type": {
"kind": "String"
}
}
}
Enum Types
{
"kind": "Enum",
"type": <type>,
"typeID": "<fully qualified type ID>",
"initializers":[],
"fields": [
{
"id": "rawValue",
"type": <type>
}
]
}
例
{
"kind": "Enum",
"type": {
"kind": "String"
},
"typeID": "0x3.GreatContract.GreatEnum",
"initializers": [],
"fields": [
{
"id": "rawValue",
"type": {
"kind": "String"
}
}
]
}
Repeated Types
複合型が同じJSON型エンコーディング内で複数回現れる場合、それは再帰的であるか、または(複合フィールド内で)繰り返されるためであり、その複合型は代わりにその型IDで表されます。
例
{
"type": "Type",
"value": {
"staticType": {
"kind": "Resource",
"typeID": "0x3.GreatContract.NFT",
"fields": [
{
"id": "foo",
"type": {
"kind": "Optional",
"type": "0x3.GreatContract.NFT"
/* recursive NFT resource type is instead encoded as an ID */
}
}
],
"initializers": [],
"type": ""
}
}
}
Inclusive Range Type
{
"kind": "InclusiveRange",
"element": <integer_type>
}
例
{
"kind": "InclusiveRange",
"element": {
"kind": "Int"
}
}
翻訳元
Previous << Contract Upgrades with Incompatible Changes
Flow blockchain / Cadence version1.0ドキュメント (JSON-Cadence format)