Currently the project only has the reverse implemented: an SCVal to JSON converter located in crates/core/src/decode/scval_to_json.rs. There is no code that takes a JSON payload and turns it back into a Stellar ScVal. We need to implement a parser that recursively converts a serde_json::Value into a stellar_xdr::curr::ScVal to allow frontend tooling to supply arguments and state payloads for contract execution and simulation.
Implementation Guidelines
Create a new public function, for example pub fn json_to_scval(value: &serde_json::Value) -> Result<ScVal, ConversionError>, that handles the conversion. Enforce a maximum recursion depth, such as 100 levels, to prevent stack overflow vulnerabilities from maliciously nested JSON arrays or objects.
Map JSON primitives carefully. JSON booleans map to ScVal::Bool, and null maps to ScVal::Void or to ScVal::Vec(None) / ScVal::Map(None) depending on context. JSON numbers map to ScVal::U32, ScVal::I32, ScVal::U64, or ScVal::I64 based on their signedness and bounds.
Map JSON strings with attention to encoding. Plain strings map to ScVal::String or ScVal::Symbol. Hex-encoded strings starting with 0x should be detected and decoded to ScVal::Bytes. Large numeric strings should be detected and parsed back into their respective ScVal::U128, ScVal::I128, ScVal::U256, or ScVal::I256 parts. Stellar strkeys starting with C or G should be detected and decoded to ScVal::Address.
Map JSON arrays by recursively converting them into ScVal::Vec(Some(ScVec(...))).
Map JSON objects by recursively converting them into ScVal::Map. Also handle the lossless array-of-entries fallback format, [{"key": ..., "value": ...}, ...], that scval_to_json generates when map keys collide as strings.
Map complex variants like ScVal::Error, ScVal::LedgerKeyNonce, and ScVal::ContractInstance by checking for their specific JSON object structures, such as {"type": "Contract", "code": 4}.
Files Location
crates/core/src/decode/json_to_scval.rs, with export added in crates/core/src/decode/mod.rs.
Expected Result
Running cargo test --package grat-core --lib decode validates the mapping, specifically testing that json_to_scval(scval_to_json(&val)) == val round-trips successfully for various primitives, nested vectors and maps, large integers, and strkeys.
Please use the PR template when submitting the pull request.
Join the contributor Telegram group with any questions: https://t.me/+sII7WPhll2liMGNk
Currently the project only has the reverse implemented: an
SCValto JSON converter located incrates/core/src/decode/scval_to_json.rs. There is no code that takes a JSON payload and turns it back into a StellarScVal. We need to implement a parser that recursively converts aserde_json::Valueinto astellar_xdr::curr::ScValto allow frontend tooling to supply arguments and state payloads for contract execution and simulation.Implementation Guidelines
Create a new public function, for example
pub fn json_to_scval(value: &serde_json::Value) -> Result<ScVal, ConversionError>, that handles the conversion. Enforce a maximum recursion depth, such as 100 levels, to prevent stack overflow vulnerabilities from maliciously nested JSON arrays or objects.Map JSON primitives carefully. JSON booleans map to
ScVal::Bool, and null maps toScVal::Voidor toScVal::Vec(None)/ScVal::Map(None)depending on context. JSON numbers map toScVal::U32,ScVal::I32,ScVal::U64, orScVal::I64based on their signedness and bounds.Map JSON strings with attention to encoding. Plain strings map to
ScVal::StringorScVal::Symbol. Hex-encoded strings starting with0xshould be detected and decoded toScVal::Bytes. Large numeric strings should be detected and parsed back into their respectiveScVal::U128,ScVal::I128,ScVal::U256, orScVal::I256parts. Stellar strkeys starting withCorGshould be detected and decoded toScVal::Address.Map JSON arrays by recursively converting them into
ScVal::Vec(Some(ScVec(...))).Map JSON objects by recursively converting them into
ScVal::Map. Also handle the lossless array-of-entries fallback format,[{"key": ..., "value": ...}, ...], thatscval_to_jsongenerates when map keys collide as strings.Map complex variants like
ScVal::Error,ScVal::LedgerKeyNonce, andScVal::ContractInstanceby checking for their specific JSON object structures, such as{"type": "Contract", "code": 4}.Files Location
crates/core/src/decode/json_to_scval.rs, with export added incrates/core/src/decode/mod.rs.Expected Result
Running
cargo test --package grat-core --lib decodevalidates the mapping, specifically testing thatjson_to_scval(scval_to_json(&val)) == valround-trips successfully for various primitives, nested vectors and maps, large integers, and strkeys.Please use the PR template when submitting the pull request.
Join the contributor Telegram group with any questions: https://t.me/+sII7WPhll2liMGNk