From f5ff4e2559621289812c44de8e572c6c37252572 Mon Sep 17 00:00:00 2001 From: CuberL Date: Thu, 5 Dec 2024 11:21:39 +0800 Subject: [PATCH 1/2] Add example for setting values in array --- README.md | 18 ++++++++++++++++++ sjson_test.go | 1 + 2 files changed, 19 insertions(+) diff --git a/README.md b/README.md index 4598424..44b44c2 100644 --- a/README.md +++ b/README.md @@ -168,6 +168,24 @@ println(value) // {"friends":["Andy","Carol","Sara"] ``` +Update existing value in array: +```go +value, _ := sjson.Set(`{"friends":["Andy","Carol"]}`, "friends.1", "Sara") +println(value) + +// Output: +// {"friends":["Andy","Sara"] +``` + +Update all existing values in array: +```go +value, _ := sjson.Set(`{"friends":["Andy","Carol"]}`, "friends.#()#", "Sara") +println(value) + +// Output: +// {"friends":["Sara","Sara"] +``` + Append an array value by using the `-1` key in a path: ```go value, _ := sjson.Set(`{"friends":["Andy","Carol"]}`, "friends.-1", "Sara") diff --git a/sjson_test.go b/sjson_test.go index aa3d968..ed54fd0 100644 --- a/sjson_test.go +++ b/sjson_test.go @@ -68,6 +68,7 @@ func TestBasic(t *testing.T) { testRaw(t, setRaw, `[null,true]`, ``, "1", `true`) testRaw(t, setRaw, `[1,null,true]`, `[1]`, "2", `true`) testRaw(t, setRaw, `[1,true,false]`, `[1,null,false]`, "1", `true`) + testRaw(t, setRaw, `[true,true,true]`, `[1,null,false]`, "#()#", `true`) testRaw(t, setRaw, `[1,{"hello":"when","this":[0,null,2]},false]`, `[1,{"hello":"when","this":[0,1,2]},false]`, From b1fd0cf1e18001a9d887115ad48901521ff22696 Mon Sep 17 00:00:00 2001 From: CuberL Date: Thu, 5 Dec 2024 13:50:47 +0800 Subject: [PATCH 2/2] update readme --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 44b44c2..d34ca72 100644 --- a/README.md +++ b/README.md @@ -165,7 +165,7 @@ value, _ := sjson.Set(`{"friends":["Andy","Carol"]}`, "friends.2", "Sara") println(value) // Output: -// {"friends":["Andy","Carol","Sara"] +// {"friends":["Andy","Carol","Sara"]} ``` Update existing value in array: @@ -174,7 +174,7 @@ value, _ := sjson.Set(`{"friends":["Andy","Carol"]}`, "friends.1", "Sara") println(value) // Output: -// {"friends":["Andy","Sara"] +// {"friends":["Andy","Sara"]} ``` Update all existing values in array: @@ -183,7 +183,7 @@ value, _ := sjson.Set(`{"friends":["Andy","Carol"]}`, "friends.#()#", "Sara") println(value) // Output: -// {"friends":["Sara","Sara"] +// {"friends":["Sara","Sara"]} ``` Append an array value by using the `-1` key in a path: @@ -192,7 +192,7 @@ value, _ := sjson.Set(`{"friends":["Andy","Carol"]}`, "friends.-1", "Sara") println(value) // Output: -// {"friends":["Andy","Carol","Sara"] +// {"friends":["Andy","Carol","Sara"]} ``` Append an array value that is past the end: @@ -201,7 +201,7 @@ value, _ := sjson.Set(`{"friends":["Andy","Carol"]}`, "friends.4", "Sara") println(value) // Output: -// {"friends":["Andy","Carol",null,null,"Sara"] +// {"friends":["Andy","Carol",null,null,"Sara"]} ``` Delete a value: