hyperledger / iroha

Iroha - A simple, enterprise-grade decentralized ledger
https://wiki.hyperledger.org/display/iroha
Apache License 2.0
432 stars 276 forks source link

SCALE-native JSON value #5024

Open 0x009922 opened 2 weeks ago

0x009922 commented 2 weeks ago

Context

Continuation of:

In short, Iroha supports arbitrary JSON payloads for certain actions. In SCALE they are encoded as JSON strings (JsonString type), while in JSON format they are inlined as values.

This is a proposal of an alternative solution: introduce as SCALE-native JsonValue type, encoded not as a string, but directly as an enum:

enum JsonValue {
    Null,
    Bool(bool),
    Number(Number),
    String(String),
    Array(Vec<JsonValue>),
    Object(Map<String, JsonValue>),
}

_this is a literal copy of serde_json::Value_

Benefits

Downsides

Why it is String now

AFAIK the initial choice of string representation for JSON values is an efficiency concern: we don't want to pay for extra (de)serialization while transmitting data to/from WASM smartcontracts.

Basically, string was used as a way to have lazy encoding of data, as requested.

I think it should be possible to achieve lazy encoding in a more direct way with some sort of Lazy<T> container, making it both more explicit and flexible.

DCNick3 commented 1 week ago

Like the suggestion of the Lazy container, this could nicely decouple the laziness from the data type.