0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

JSON-Cadence format

Last updated at Posted at 2024-10-19

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

これらは、IntStringStoragePathのような基本的な型です。

{
  "kind": <kind>
}

ここでkindは次のいずれかです。

  • Account
  • AccountCapabilityController
  • AccountKey
  • Address
  • AnyResource
  • AnyResourceAttachment
  • AnyStruct
  • AnyStructAttachment
  • Block
  • Bool
  • Capability
  • CapabilityPath
  • Character
  • DeployedContract
  • DeploymentResult
  • Fix64
  • FixedPoint
  • FixedSizeUnsignedInteger
  • HashAlgorithm
  • HashableStruct
  • Int
  • Int128
  • Int16
  • Int256
  • Int32
  • Int64
  • Int8
  • Integer
  • Never
  • Number
  • Path
  • PrivatePath
  • PublicKey
  • PublicPath
  • SignatureAlgorithm
  • SignedFixedPoint
  • SignedInteger
  • SignedNumber
  • StorageCapabilityController
  • StoragePath
  • String
  • Type
  • UFix64
  • UInt
  • UInt128
  • UInt16
  • UInt256
  • UInt32
  • UInt64
  • UInt8
  • Void
  • Word128
  • Word16
  • Word256
  • Word32
  • Word64
  • Word8

{
  "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)

Next >> Measuring Time In Cadence

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?