Skip to content

Commit b4dd1d3

Browse files
authored
Add core include json option (#808)
1 parent cdd7987 commit b4dd1d3

24 files changed

Lines changed: 351 additions & 73 deletions

File tree

packages/codegen/src/generate-utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,11 @@ export class MoveStruct<
169169
T extends Record<string, BcsType<any>>,
170170
const Name extends string = string,
171171
> extends BcsStruct<T, Name> {
172-
async get<Include extends Omit<SuiClientTypes.ObjectInclude, 'content'> = {}>({
172+
async get<Include extends Omit<SuiClientTypes.ObjectInclude, 'content' | 'json'> = {}>({
173173
objectId,
174174
...options
175175
}: GetOptions<Include>): Promise<
176-
SuiClientTypes.Object<Include & { content: true }> & { json: BcsStruct<T>['$inferType'] }
176+
SuiClientTypes.Object<Include & { content: true, json: true }> & { json: BcsStruct<T>['$inferType'] }
177177
> {
178178
const [res] = await this.getMany<Include>({
179179
...options,
@@ -183,11 +183,11 @@ export class MoveStruct<
183183
return res;
184184
}
185185
186-
async getMany<Include extends Omit<SuiClientTypes.ObjectInclude, 'content'> = {}>({
186+
async getMany<Include extends Omit<SuiClientTypes.ObjectInclude, 'content' | 'json'> = {}>({
187187
client,
188188
...options
189189
}: GetManyOptions<Include>): Promise<
190-
Array<SuiClientTypes.Object<Include & { content: true }> & { json: BcsStruct<T>['$inferType'] }>
190+
Array<SuiClientTypes.Object<Include & { content: true, json: true }> & { json: BcsStruct<T>['$inferType'] }>
191191
> {
192192
const response = (await client.core.getObjects({
193193
...options,

packages/codegen/tests/generated/utils/index.ts

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/create-dapp/templates/react-e2e-counter/src/contracts/counter/counter.ts

Lines changed: 3 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/create-dapp/templates/react-e2e-counter/src/contracts/counter/deps/sui/object.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

packages/create-dapp/templates/react-e2e-counter/src/contracts/utils/index.ts

Lines changed: 78 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/deepbook-v3/src/contracts/utils/index.ts

Lines changed: 10 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/docs/content/sui/clients/core.mdx

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,25 @@ console.log(object.digest);
4646
console.log(object.type); // e.g., "0x2::coin::Coin<0x2::sui::SUI>"
4747
```
4848

49+
You can also fetch the JSON representation of the object's Move struct content:
50+
51+
```typescript
52+
const { object } = await client.core.getObject({
53+
objectId: '0x123...',
54+
include: { json: true },
55+
});
56+
57+
// Access the JSON representation
58+
console.log(object.json);
59+
```
60+
61+
<Callout type="warn">
62+
The `json` field structure may vary between API implementations. For example, JSON-RPC returns UID
63+
fields as nested objects (`{ "id": { "id": "0x..." } }`), while gRPC and GraphQL flatten them
64+
(`{ "id": "0x..." }`). For consistent data across all clients, use `include: { content: true }` and
65+
parse the BCS data directly.
66+
</Callout>
67+
4968
### getObjects
5069

5170
Fetch multiple objects in a single request.
@@ -395,11 +414,19 @@ Most methods accept an `include` parameter to control what data is returned:
395414

396415
```typescript
397416
{
398-
content: boolean, // BCS-encoded object content
417+
content: boolean, // BCS-encoded object content
399418
previousTransaction: boolean, // Digest of creating transaction
419+
json: boolean, // JSON representation of Move struct content
400420
}
401421
```
402422

423+
<Callout type="warn">
424+
The `json` field returns the JSON representation of an object's Move struct content. **Warning:**
425+
The exact shape and field names of this data may vary between different API implementations
426+
(JSON-RPC vs gRPC or GraphQL). For consistent data across APIs, use the `content` field and parse
427+
the BCS data directly.
428+
</Callout>
429+
403430
### Transaction Include Options
404431

405432
```typescript

packages/kiosk/src/contracts/0x2/balance.ts

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)