Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: encode JsonString as object in scale #5084

Closed
wants to merge 1 commit into from

Conversation

Erigara
Copy link
Contributor

@Erigara Erigara commented Sep 18, 2024

Context

Aim to close differences between scale and json by encoding JsonString as enum in scale and not just String.

Closes #5024

Solution

Approach is to manually implement encode and decode for JsonString and describe it in schema.

Review notes

I am not sure that this will help since there is anyway not 1 to 1 mapping between scale and json.

And in the json we still expect arbitrary json object and not smt encoded using schema...

@github-actions github-actions bot added the api-changes Changes in the API for client libraries label Sep 18, 2024
Copy link
Contributor

@mversic mversic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not much to say, I'm not really into this change, but I'll approve if people think this is the way

crates/iroha_primitives/src/json.rs Outdated Show resolved Hide resolved
Comment on lines +2395 to +2427
"JsonString": {
"Enum": [
{
"tag": "Null",
"discriminant": 0
},
{
"tag": "String",
"discriminant": 1,
"type": "String"
},
{
"tag": "Bool",
"discriminant": 2,
"type": "bool"
},
{
"tag": "Array",
"discriminant": 3,
"type": "Vec<JsonString>"
},
{
"tag": "Number",
"discriminant": 4,
"type": "String"
},
{
"tag": "Object",
"discriminant": 5,
"type": "SortedMap<String, JsonString>"
}
]
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"JsonString": {
"Enum": [
{
"tag": "Null",
"discriminant": 0
},
{
"tag": "String",
"discriminant": 1,
"type": "String"
},
{
"tag": "Bool",
"discriminant": 2,
"type": "bool"
},
{
"tag": "Array",
"discriminant": 3,
"type": "Vec<JsonString>"
},
{
"tag": "Number",
"discriminant": 4,
"type": "String"
},
{
"tag": "Object",
"discriminant": 5,
"type": "SortedMap<String, JsonString>"
}
]
},
"JsonValue: "JsonValue"

There was an idea that we can introduce a new json type into schema. It would be up to the sdk to know how to handle this type like it is for Option<T> or Vec<T>. What happened with this suggestion?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not aware of this idea.
I think the main difference is that Vec and Option has native representation in scale and json doesn't.

#[display(fmt = "{_0}")]
pub struct JsonString(String);
#[serde(transparent)]
pub struct JsonString(Value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub struct JsonString(Value);
pub struct JsonValue(Value);

if we're to proceed with this change we'll have to rename it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, made a draft with minimal changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-changes Changes in the API for client libraries
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SCALE-native JSON value
2 participants