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

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

These are basic types like Int, String, or StoragePath.

{
  "kind": <kind>
}

Where kind is one of:

  • 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"
  }
}

(他にもあります。詳しくは翻訳元の原文へ)
翻訳元->https://cadence-lang.org/docs/json-cadence-spec#composites-struct-resource-event-contract-enum

Flow BlockchainのCadence version1.0ドキュメント (JSON-Cadence format)

Previous << Contract Upgrades with Incompatible Changes

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?