From 5bf7fa51d70f3a62c383920f262b4c3a9638a345 Mon Sep 17 00:00:00 2001 From: mu2019 Date: Thu, 12 Oct 2023 11:16:21 +0800 Subject: [PATCH] Update mod.rs add array_insert method. Works on `JsonValue::Array` - insert an entry at index.will return error not on `JsonValue::Array`. --- src/value/mod.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/value/mod.rs b/src/value/mod.rs index d9fb0c8..2881ab3 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -555,6 +555,19 @@ impl JsonValue { } } + /// Works on `JsonValue::Array` - insert an entry at index. + /// will return error not on `JsonValue::Array`. + pub fn array_insert(&mut self, index: usize, value: T) -> Result<()> + where T: Into { + match *self { + JsonValue::Array(ref mut vec) => { + vec.insert(index, value.into()); + Ok(()) + }, + _ => Err(Error::wrong_type("I'snt an Arry")) + } + } + /// When called on an array or an object, will wipe them clean. When called /// on a string will clear the string. Numbers and booleans become null. pub fn clear(&mut self) {