diff --git a/precompile/binaries/minlib/json.mv b/precompile/binaries/minlib/json.mv index 06293878..085c0349 100644 Binary files a/precompile/binaries/minlib/json.mv and b/precompile/binaries/minlib/json.mv differ diff --git a/precompile/binaries/minlib/simple_json.mv b/precompile/binaries/minlib/simple_json.mv index c3001fe4..58f699ee 100644 Binary files a/precompile/binaries/minlib/simple_json.mv and b/precompile/binaries/minlib/simple_json.mv differ diff --git a/precompile/binaries/stdlib/json.mv b/precompile/binaries/stdlib/json.mv index 06293878..085c0349 100644 Binary files a/precompile/binaries/stdlib/json.mv and b/precompile/binaries/stdlib/json.mv differ diff --git a/precompile/binaries/stdlib/simple_json.mv b/precompile/binaries/stdlib/simple_json.mv index c3001fe4..58f699ee 100644 Binary files a/precompile/binaries/stdlib/simple_json.mv and b/precompile/binaries/stdlib/simple_json.mv differ diff --git a/precompile/modules/initia_stdlib/sources/json.move b/precompile/modules/initia_stdlib/sources/json.move index 42a9d3e8..1b6b3515 100644 --- a/precompile/modules/initia_stdlib/sources/json.move +++ b/precompile/modules/initia_stdlib/sources/json.move @@ -232,8 +232,22 @@ module initia_std::json { }; i = i + 1; }; - assert!(i < elem.value.child_length, EKEY_NOT_FOUND); - get_next_index(index, i) + + if( i >= elem.value.child_length) { + JsonIndex { + data: vector::empty(), + } + } else { + get_next_index(index, i) + } + } + + public fun is_null_index(index: &JsonIndex): bool { + if( vector::length(&index.data) == 0) { + true + } else { + false + } } fun set_elem(object: &mut JsonObject, index: JsonIndex, elem: JsonElem) { @@ -747,4 +761,16 @@ module initia_std::json { let json_string = stringify(&obj); assert!(json_string == string::utf8(b"{}"), 0); } + + #[test] + fun test_find_key() { + let test_str = string::utf8(b"{ \"def\": {\"d\": [-1, 312, \"45.12324\"]}, \"abc\": 18446744073709551615}"); + let obj = parse(test_str); + let index = start_index(); + let idx = find(&obj, &index, &string::utf8(b"abc")); + assert!( !is_null_index(&idx), 0 ); + + let idx = find(&obj, &index, &string::utf8(b"a")); + assert!( is_null_index(&idx), 1 ); + } } \ No newline at end of file diff --git a/precompile/modules/initia_stdlib/sources/simple_json.move b/precompile/modules/initia_stdlib/sources/simple_json.move index dd7ca757..41557525 100644 --- a/precompile/modules/initia_stdlib/sources/simple_json.move +++ b/precompile/modules/initia_stdlib/sources/simple_json.move @@ -5,6 +5,8 @@ module initia_std::simple_json { use initia_std::decimal256::{Decimal256}; use initia_std::string::{String}; + const EKEY_NOT_FOUND: u64 = 0; + struct SimpleJsonObject has copy, drop { obj: JsonObject, index: JsonIndex, @@ -70,9 +72,32 @@ module initia_std::simple_json { object.index = json::get_next_index(&prev_index, position); } + // to travel object + public fun set_to_last_index(object: &mut SimpleJsonObject){ + let (prev_index, _) = json::get_prev_index(&object.index); + let child_length = json::get_child_length(json::borrow(&object.obj, &prev_index)); + if(child_length == 0) return; + object.index = json::get_next_index(&prev_index, child_length - 1); + } + public fun find_and_set_index(object: &mut SimpleJsonObject, key: &String) { let (prev_index, _) = json::get_prev_index(&object.index); - object.index = json::find(&object.obj, &prev_index, key); + let find_index = json::find(&object.obj, &prev_index, key); + + assert!(!json::is_null_index(&find_index), EKEY_NOT_FOUND); + object.index = find_index; + } + + public fun try_find_and_set_index(object: &mut SimpleJsonObject, key: &String):bool { + let (prev_index, _) = json::get_prev_index(&object.index); + let find_index = json::find(&object.obj, &prev_index, key); + + if ( json::is_null_index(&find_index)) { + false + } else { + object.index = find_index; + true + } } public fun set_bool(object: &mut SimpleJsonObject, key: Option, value: bool) { @@ -223,4 +248,50 @@ module initia_std::simple_json { let json_string = json::stringify(to_json_object(&obj)); assert!(json_string == string::utf8(b"{}"), 0); } + + #[test] + fun test_find_and_set_key0() { + let obj = from_json_object(json::parse(string::utf8(b"{}"))); + increase_depth(&mut obj); + let ok = try_find_and_set_index(&mut obj, &string::utf8(b"move")); + assert!( !ok, 0); + + set_to_last_index(&mut obj); + set_object(&mut obj, option::some(string::utf8(b"move"))); + increase_depth(&mut obj); + set_object(&mut obj, option::some(string::utf8(b"async_callback"))); + + let json_str = json::stringify(to_json_object(&obj)); + assert!( json_str == string::utf8(b"{\"move\":{\"async_callback\":{}}}"), 1) + } + + #[test] + fun test_find_and_set_key1() { + let obj = from_json_object(json::parse(string::utf8(b"{\"move\":{}}"))); + increase_depth(&mut obj); + let ok = try_find_and_set_index(&mut obj, &string::utf8(b"move")); + assert!( ok, 0); + + increase_depth(&mut obj); + set_object(&mut obj, option::some(string::utf8(b"async_callback"))); + + let json_str = json::stringify(to_json_object(&obj)); + assert!( json_str == string::utf8(b"{\"move\":{\"async_callback\":{}}}"), 1) + } + + #[test] + fun test_find_and_set_key3() { + let obj = from_json_object(json::parse(string::utf8(b"{\"forward\": {\"receiver\": \"chain-c-bech32-address\"}, \"wasm\":{}}"))); + increase_depth(&mut obj); + let ok = try_find_and_set_index(&mut obj, &string::utf8(b"move")); + assert!( !ok, 0); + + set_to_last_index(&mut obj); + set_object(&mut obj, option::some(string::utf8(b"move"))); + increase_depth(&mut obj); + set_object(&mut obj, option::some(string::utf8(b"async_callback"))); + + let json_str = json::stringify(to_json_object(&obj)); + assert!( json_str == string::utf8(b"{\"forward\":{\"receiver\":\"chain-c-bech32-address\"},\"move\":{\"async_callback\":{}},\"wasm\":{}}"), 1) + } } \ No newline at end of file diff --git a/precompile/modules/minitia_stdlib/sources/json.move b/precompile/modules/minitia_stdlib/sources/json.move index b8d80b88..502c0d26 100644 --- a/precompile/modules/minitia_stdlib/sources/json.move +++ b/precompile/modules/minitia_stdlib/sources/json.move @@ -232,8 +232,22 @@ module minitia_std::json { }; i = i + 1; }; - assert!(i < elem.value.child_length, EKEY_NOT_FOUND); - get_next_index(index, i) + + if( i >= elem.value.child_length) { + JsonIndex { + data: vector::empty(), + } + } else { + get_next_index(index, i) + } + } + + public fun is_null_index(index: &JsonIndex): bool { + if( vector::length(&index.data) == 0) { + true + } else { + false + } } fun set_elem(object: &mut JsonObject, index: JsonIndex, elem: JsonElem) { @@ -747,4 +761,16 @@ module minitia_std::json { let json_string = stringify(&obj); assert!(json_string == string::utf8(b"{}"), 0); } + + #[test] + fun test_find_key() { + let test_str = string::utf8(b"{ \"def\": {\"d\": [-1, 312, \"45.12324\"]}, \"abc\": 18446744073709551615}"); + let obj = parse(test_str); + let index = start_index(); + let idx = find(&obj, &index, &string::utf8(b"abc")); + assert!( !is_null_index(&idx), 0 ); + + let idx = find(&obj, &index, &string::utf8(b"a")); + assert!( is_null_index(&idx), 1 ); + } } \ No newline at end of file diff --git a/precompile/modules/minitia_stdlib/sources/simple_json.move b/precompile/modules/minitia_stdlib/sources/simple_json.move index 9d3692ae..0d5bc9c3 100644 --- a/precompile/modules/minitia_stdlib/sources/simple_json.move +++ b/precompile/modules/minitia_stdlib/sources/simple_json.move @@ -5,6 +5,8 @@ module minitia_std::simple_json { use minitia_std::decimal256::{Decimal256}; use minitia_std::string::{String}; + const EKEY_NOT_FOUND: u64 = 0; + struct SimpleJsonObject has copy, drop { obj: JsonObject, index: JsonIndex, @@ -70,9 +72,32 @@ module minitia_std::simple_json { object.index = json::get_next_index(&prev_index, position); } + // to travel object + public fun set_to_last_index(object: &mut SimpleJsonObject){ + let (prev_index, _) = json::get_prev_index(&object.index); + let child_length = json::get_child_length(json::borrow(&object.obj, &prev_index)); + if(child_length == 0) return; + object.index = json::get_next_index(&prev_index, child_length - 1); + } + public fun find_and_set_index(object: &mut SimpleJsonObject, key: &String) { let (prev_index, _) = json::get_prev_index(&object.index); - object.index = json::find(&object.obj, &prev_index, key); + let find_index = json::find(&object.obj, &prev_index, key); + + assert!(!json::is_null_index(&find_index), EKEY_NOT_FOUND); + object.index = find_index; + } + + public fun try_find_and_set_index(object: &mut SimpleJsonObject, key: &String):bool { + let (prev_index, _) = json::get_prev_index(&object.index); + let find_index = json::find(&object.obj, &prev_index, key); + + if ( json::is_null_index(&find_index)) { + false + } else { + object.index = find_index; + true + } } public fun set_bool(object: &mut SimpleJsonObject, key: Option, value: bool) { @@ -223,4 +248,50 @@ module minitia_std::simple_json { let json_string = json::stringify(to_json_object(&obj)); assert!(json_string == string::utf8(b"{}"), 0); } + + #[test] + fun test_find_and_set_key0() { + let obj = from_json_object(json::parse(string::utf8(b"{}"))); + increase_depth(&mut obj); + let ok = try_find_and_set_index(&mut obj, &string::utf8(b"move")); + assert!( !ok, 0); + + set_to_last_index(&mut obj); + set_object(&mut obj, option::some(string::utf8(b"move"))); + increase_depth(&mut obj); + set_object(&mut obj, option::some(string::utf8(b"async_callback"))); + + let json_str = json::stringify(to_json_object(&obj)); + assert!( json_str == string::utf8(b"{\"move\":{\"async_callback\":{}}}"), 1) + } + + #[test] + fun test_find_and_set_key1() { + let obj = from_json_object(json::parse(string::utf8(b"{\"move\":{}}"))); + increase_depth(&mut obj); + let ok = try_find_and_set_index(&mut obj, &string::utf8(b"move")); + assert!( ok, 0); + + increase_depth(&mut obj); + set_object(&mut obj, option::some(string::utf8(b"async_callback"))); + + let json_str = json::stringify(to_json_object(&obj)); + assert!( json_str == string::utf8(b"{\"move\":{\"async_callback\":{}}}"), 1) + } + + #[test] + fun test_find_and_set_key3() { + let obj = from_json_object(json::parse(string::utf8(b"{\"forward\": {\"receiver\": \"chain-c-bech32-address\"}, \"wasm\":{}}"))); + increase_depth(&mut obj); + let ok = try_find_and_set_index(&mut obj, &string::utf8(b"move")); + assert!( !ok, 0); + + set_to_last_index(&mut obj); + set_object(&mut obj, option::some(string::utf8(b"move"))); + increase_depth(&mut obj); + set_object(&mut obj, option::some(string::utf8(b"async_callback"))); + + let json_str = json::stringify(to_json_object(&obj)); + assert!( json_str == string::utf8(b"{\"forward\":{\"receiver\":\"chain-c-bech32-address\"},\"move\":{\"async_callback\":{}},\"wasm\":{}}"), 1) + } } \ No newline at end of file