use crate::convert::{escape_json_key, write_json_string};
use crate::error::Error;
use crate::path::PathSegment;
use crate::scan::{
byte_at, find_object_member_offsets, find_value_offsets, scan_backwards_whitespace, skip_value,
skip_whitespace,
};
pub fn mutate_value(json: &mut Vec<u8>, path: &[PathSegment], new_value: &[u8]) -> Result<(), Error> {
if new_value.is_empty() {
return Err(Error::InvalidJsonSyntax {
pos: 0,
msg: "Replacement value must not be empty",
});
}
let (start, end) = find_value_offsets(json, path)?;
validate_span(json, start, end)?;
splice_range(json, start, end, new_value)
}
pub fn mutate_value_checked(
json: &mut Vec<u8>,
path: &[PathSegment],
new_value: &[u8],
) -> Result<(), Error> {
assert_single_json_value(new_value)?;
mutate_value(json, path, new_value)
}
pub(crate) fn assert_single_json_value(bytes: &[u8]) -> Result<(), Error> {
if bytes.is_empty() {
return Err(Error::InvalidJsonSyntax {
pos: 0,
msg: "JSON value must not be empty",
});
}
let end = skip_value(bytes, 0)?;
let after = skip_whitespace(bytes, end);
if after != bytes.len() {
return Err(Error::InvalidJsonSyntax {
pos: after,
msg: "Trailing junk after JSON value",
});
}
Ok(())
}
pub fn append_to_array(
json: &mut Vec<u8>,
path: &[PathSegment],
new_element: &[u8],
) -> Result<(), Error> {
if new_element.is_empty() {
return Err(Error::InvalidJsonSyntax {
pos: 0,
msg: "Appended value must not be empty",
});
}
let (start, end) = find_value_offsets(json, path)?;
require_container(json, start, end, b'[', b']', "array", "primitive/object")?;
let insertion_point = end - 1;
let is_empty = is_array_empty(json, start, end)?;
let delta = if is_empty {
new_element.len()
} else {
new_element
.len()
.checked_add(1)
.ok_or(Error::InvalidJsonSyntax {
pos: insertion_point,
msg: "Buffer size overflow",
})?
};
grow_and_shift_right(json, insertion_point, delta)?;
if is_empty {
json[insertion_point..insertion_point + new_element.len()].copy_from_slice(new_element);
} else {
json[insertion_point] = b',';
json[insertion_point + 1..insertion_point + 1 + new_element.len()]
.copy_from_slice(new_element);
}
Ok(())
}
pub fn prepend_to_array(
json: &mut Vec<u8>,
path: &[PathSegment],
new_element: &[u8],
) -> Result<(), Error> {
insert_array_element(json, path, 0, new_element)
}
pub fn insert_array_element(
json: &mut Vec<u8>,
path: &[PathSegment],
index: usize,
new_element: &[u8],
) -> Result<(), Error> {
if new_element.is_empty() {
return Err(Error::InvalidJsonSyntax {
pos: 0,
msg: "Inserted value must not be empty",
});
}
let (start, end) = find_value_offsets(json, path)?;
require_container(json, start, end, b'[', b']', "array", "primitive/object")?;
let is_empty = is_array_empty(json, start, end)?;
if is_empty {
if index != 0 {
return Err(Error::PathNotFound);
}
let insertion_point = end - 1;
grow_and_shift_right(json, insertion_point, new_element.len())?;
json[insertion_point..insertion_point + new_element.len()].copy_from_slice(new_element);
return Ok(());
}
let mut pos = skip_whitespace(json, start + 1);
let mut i = 0usize;
loop {
if pos >= end || json[pos] == b']' {
if i == index {
let insertion_point = end - 1;
let delta = new_element.len().checked_add(1).ok_or(Error::InvalidJsonSyntax {
pos: insertion_point,
msg: "Buffer size overflow",
})?;
grow_and_shift_right(json, insertion_point, delta)?;
json[insertion_point] = b',';
json[insertion_point + 1..insertion_point + 1 + new_element.len()]
.copy_from_slice(new_element);
return Ok(());
}
return Err(Error::PathNotFound);
}
if i == index {
let insertion_point = pos;
let delta = new_element.len().checked_add(1).ok_or(Error::InvalidJsonSyntax {
pos: insertion_point,
msg: "Buffer size overflow",
})?;
grow_and_shift_right(json, insertion_point, delta)?;
json[insertion_point..insertion_point + new_element.len()].copy_from_slice(new_element);
json[insertion_point + new_element.len()] = b',';
return Ok(());
}
pos = skip_value(json, pos)?;
pos = skip_whitespace(json, pos);
if pos < json.len() && json[pos] == b',' {
pos += 1;
pos = skip_whitespace(json, pos);
i = i.checked_add(1).ok_or(Error::InvalidJsonSyntax {
pos,
msg: "Array length overflow",
})?;
} else if pos < json.len() && json[pos] == b']' {
i = i.checked_add(1).ok_or(Error::InvalidJsonSyntax {
pos,
msg: "Array length overflow",
})?;
} else {
return Err(Error::InvalidJsonSyntax {
pos,
msg: "Expected comma ',' or closing bracket ']'",
});
}
}
}
fn is_array_empty(json: &[u8], start: usize, end: usize) -> Result<bool, Error> {
if start + 1 >= end {
return Err(Error::InvalidJsonSyntax {
pos: start,
msg: "Empty or truncated array offsets",
});
}
let mut pos = start + 1;
while pos < end - 1 {
match json[pos] {
b' ' | b'\t' | b'\n' | b'\r' => pos += 1,
_ => return Ok(false),
}
}
Ok(true)
}
pub fn array_len(json: &[u8], path: &[PathSegment]) -> Result<usize, Error> {
let (start, end) = find_value_offsets(json, path)?;
require_container(json, start, end, b'[', b']', "array", "primitive/object")?;
let mut pos = skip_whitespace(json, start + 1);
if pos >= json.len() {
return Err(Error::InvalidJsonSyntax {
pos,
msg: "Unexpected EOF",
});
}
if json[pos] == b']' {
return Ok(0);
}
let mut count = 1usize;
loop {
pos = skip_value(json, pos)?;
pos = skip_whitespace(json, pos);
if pos >= json.len() {
return Err(Error::InvalidJsonSyntax {
pos,
msg: "Unexpected EOF",
});
}
if json[pos] == b',' {
count = count.checked_add(1).ok_or(Error::InvalidJsonSyntax {
pos,
msg: "Array length overflow",
})?;
pos += 1;
} else if json[pos] == b']' {
break;
} else {
return Err(Error::InvalidJsonSyntax {
pos,
msg: "Expected comma ',' or closing bracket ']'",
});
}
}
Ok(count)
}
pub fn upsert_object_key(
json: &mut Vec<u8>,
path: &[PathSegment],
key: &str,
new_value: &[u8],
) -> Result<(), Error> {
if new_value.is_empty() {
return Err(Error::InvalidJsonSyntax {
pos: 0,
msg: "Upsert value must not be empty",
});
}
let escaped_key = escape_json_key(key);
match find_object_member_offsets(json, path, escaped_key.as_bytes()) {
Ok((_key_start, val_start, val_end)) => {
validate_span(json, val_start, val_end)?;
return splice_range(json, val_start, val_end, new_value);
}
Err(Error::PathNotFound) => {
}
Err(e) => return Err(e),
}
let (start, end) = find_value_offsets(json, path)?;
require_container(json, start, end, b'{', b'}', "object", "primitive/array")?;
let insertion_point = end - 1;
let is_empty = is_object_empty(json, start, end)?;
let mut insertion_content = Vec::new();
if !is_empty {
insertion_content.push(b',');
}
write_json_string(&mut insertion_content, key);
insertion_content.push(b':');
insertion_content.extend_from_slice(new_value);
let delta = insertion_content.len();
grow_and_shift_right(json, insertion_point, delta)?;
json[insertion_point..insertion_point + delta].copy_from_slice(&insertion_content);
Ok(())
}
pub fn upsert_at_path(
json: &mut Vec<u8>,
path: &[PathSegment],
new_value: &[u8],
) -> Result<(), Error> {
if path.is_empty() {
return mutate_value(json, path, new_value);
}
if new_value.is_empty() {
return Err(Error::InvalidJsonSyntax {
pos: 0,
msg: "Upsert value must not be empty",
});
}
if find_value_offsets(json, path).is_ok() {
return mutate_value(json, path, new_value);
}
for i in 0..path.len() - 1 {
let prefix = &path[..=i];
let next = &path[i + 1];
match find_value_offsets(json, prefix) {
Ok((start, _end)) => match next {
PathSegment::Key(_) => {
if byte_at(json, start)? != b'{' {
return Err(Error::TypeMismatch {
expected: "object",
found: "primitive/array",
});
}
}
PathSegment::Index(_) => {
if byte_at(json, start)? != b'[' {
return Err(Error::TypeMismatch {
expected: "array",
found: "primitive/object",
});
}
}
},
Err(Error::PathNotFound) => {
let PathSegment::Key(k) = &path[i] else {
return Err(Error::InvalidPath {
msg: "upsert_at_path cannot create missing array indexes",
});
};
match next {
PathSegment::Key(_) => {
upsert_object_key(json, &path[..i], k, b"{}")?;
}
PathSegment::Index(_) => {
return Err(Error::InvalidPath {
msg: "upsert_at_path cannot create array parents; only object keys",
});
}
}
}
Err(e) => return Err(e),
}
}
let parent = &path[..path.len() - 1];
match &path[path.len() - 1] {
PathSegment::Key(k) => upsert_object_key(json, parent, k, new_value),
PathSegment::Index(idx) => {
let (start, _end) = find_value_offsets(json, parent)?;
if byte_at(json, start)? != b'[' {
return Err(Error::TypeMismatch {
expected: "array",
found: "primitive/object",
});
}
let len = array_len(json, parent)?;
if *idx < len {
let mut full = parent.to_vec();
full.push(PathSegment::Index(*idx));
mutate_value(json, &full, new_value)
} else if *idx == len {
append_to_array(json, parent, new_value)
} else {
Err(Error::IndexOutOfBounds { index: *idx })
}
}
}
}
fn is_object_empty(json: &[u8], start: usize, end: usize) -> Result<bool, Error> {
if start + 1 >= end {
return Err(Error::InvalidJsonSyntax {
pos: start,
msg: "Empty or truncated object offsets",
});
}
let mut pos = start + 1;
while pos < end - 1 {
match json[pos] {
b' ' | b'\t' | b'\n' | b'\r' => pos += 1,
_ => return Ok(false),
}
}
Ok(true)
}
pub fn delete_key(json: &mut Vec<u8>, path: &[PathSegment], key: &str) -> Result<(), Error> {
let escaped_key = escape_json_key(key);
let (key_start, _val_start, val_end) =
find_object_member_offsets(json, path, escaped_key.as_bytes())?;
let mut prev_comma_pos = key_start;
prev_comma_pos = scan_backwards_whitespace(json, prev_comma_pos);
let delete_start;
let delete_end;
if prev_comma_pos > 0 && byte_at(json, prev_comma_pos - 1)? == b',' {
delete_start = prev_comma_pos - 1;
delete_end = val_end;
} else {
let mut next_comma_pos = val_end;
next_comma_pos = skip_whitespace(json, next_comma_pos);
if next_comma_pos < json.len() && json[next_comma_pos] == b',' {
delete_start = key_start;
delete_end = next_comma_pos + 1;
} else {
delete_start = key_start;
delete_end = val_end;
}
}
let (delete_start, delete_end) = expand_delete_whitespace(json, delete_start, delete_end);
delete_range(json, delete_start, delete_end)
}
pub fn delete_index(json: &mut Vec<u8>, path: &[PathSegment], index: usize) -> Result<(), Error> {
let mut target_path = path.to_vec();
target_path.push(PathSegment::Index(index));
let (val_start, val_end) = find_value_offsets(json, &target_path)?;
validate_span(json, val_start, val_end)?;
let mut prev_comma_pos = val_start;
prev_comma_pos = scan_backwards_whitespace(json, prev_comma_pos);
let delete_start;
let delete_end;
if prev_comma_pos > 0 && byte_at(json, prev_comma_pos - 1)? == b',' {
delete_start = prev_comma_pos - 1;
delete_end = val_end;
} else {
let mut next_comma_pos = val_end;
next_comma_pos = skip_whitespace(json, next_comma_pos);
if next_comma_pos < json.len() && json[next_comma_pos] == b',' {
delete_start = val_start;
delete_end = next_comma_pos + 1;
} else {
delete_start = val_start;
delete_end = val_end;
}
}
let (delete_start, delete_end) = expand_delete_whitespace(json, delete_start, delete_end);
delete_range(json, delete_start, delete_end)
}
fn expand_delete_whitespace(json: &[u8], mut start: usize, mut end: usize) -> (usize, usize) {
while start > 0 && matches!(json[start - 1], b' ' | b'\t' | b'\n' | b'\r') {
start -= 1;
}
while end < json.len() && matches!(json[end], b' ' | b'\t' | b'\n' | b'\r') {
end += 1;
}
(start, end)
}
fn validate_span(json: &[u8], start: usize, end: usize) -> Result<(), Error> {
if start > end || end > json.len() {
return Err(Error::InvalidJsonSyntax {
pos: start,
msg: "Invalid value span",
});
}
if start == end {
return Err(Error::InvalidJsonSyntax {
pos: start,
msg: "Empty value span",
});
}
Ok(())
}
fn require_container(
json: &[u8],
start: usize,
end: usize,
open: u8,
close: u8,
expected: &'static str,
found: &'static str,
) -> Result<(), Error> {
validate_span(json, start, end)?;
if byte_at(json, start)? != open {
return Err(Error::TypeMismatch { expected, found });
}
if json[end - 1] != close {
return Err(Error::InvalidJsonSyntax {
pos: end.saturating_sub(1),
msg: "Mismatched container delimiters",
});
}
Ok(())
}
fn splice_range(
json: &mut Vec<u8>,
start: usize,
end: usize,
new_value: &[u8],
) -> Result<(), Error> {
let old_len = end - start;
let new_len = new_value.len();
if old_len == new_len {
json[start..end].copy_from_slice(new_value);
return Ok(());
}
let old_total_len = json.len();
if new_len > old_len {
let delta = new_len - old_len;
grow_and_shift_right(json, end, delta)?;
} else {
let delta = old_len - new_len;
json[start + new_len..].rotate_left(delta);
json.truncate(old_total_len - delta);
}
json[start..start + new_len].copy_from_slice(new_value);
Ok(())
}
fn grow_and_shift_right(json: &mut Vec<u8>, at: usize, delta: usize) -> Result<(), Error> {
if delta == 0 {
return Ok(());
}
if at > json.len() {
return Err(Error::InvalidJsonSyntax {
pos: at,
msg: "Insert position out of bounds",
});
}
let new_len = json.len().checked_add(delta).ok_or(Error::InvalidJsonSyntax {
pos: at,
msg: "Buffer size overflow",
})?;
json.resize(new_len, 0);
json[at..].rotate_right(delta);
Ok(())
}
fn delete_range(json: &mut Vec<u8>, delete_start: usize, delete_end: usize) -> Result<(), Error> {
if delete_start > delete_end || delete_end > json.len() {
return Err(Error::InvalidJsonSyntax {
pos: delete_start,
msg: "Invalid delete span",
});
}
let delta = delete_end - delete_start;
if delta == 0 {
return Ok(());
}
let old_total_len = json.len();
json[delete_start..].rotate_left(delta);
json.truncate(old_total_len - delta);
Ok(())
}
pub fn rename_key(
json: &mut Vec<u8>,
object_path: &[PathSegment],
old_key: &str,
new_key: &str,
) -> Result<(), Error> {
if old_key == new_key {
return Ok(());
}
let escaped_new = escape_json_key(new_key);
if find_object_member_offsets(json, object_path, escaped_new.as_bytes()).is_ok() {
return Err(Error::InvalidJsonSyntax {
pos: 0,
msg: "rename_key: destination key already exists",
});
}
let escaped_old = escape_json_key(old_key);
let (key_start, _val_start, _val_end) =
find_object_member_offsets(json, object_path, escaped_old.as_bytes())?;
let key_content_start = key_start + 1;
let key_end_quote = crate::scan::find_string_end(json, key_content_start)?;
let mut new_lit = Vec::with_capacity(new_key.len() + 2);
write_json_string(&mut new_lit, new_key);
splice_range(json, key_start, key_end_quote + 1, &new_lit)
}
pub fn merge_object_shallow(
json: &mut Vec<u8>,
path: &[PathSegment],
patch: &[u8],
) -> Result<(), Error> {
let (obj_start, _obj_end) = find_value_offsets(json, path)?;
if obj_start >= json.len() || json[obj_start] != b'{' {
return Err(Error::TypeMismatch {
expected: "object",
found: "primitive/array",
});
}
let entries = collect_object_entries(patch)?;
for (key, value) in entries {
upsert_object_key(json, path, &key, &value)?;
}
Ok(())
}
fn collect_object_entries(patch: &[u8]) -> Result<Vec<(String, Vec<u8>)>, Error> {
use crate::scan::find_string_end;
let start = skip_whitespace(patch, 0);
if start >= patch.len() || patch[start] != b'{' {
return Err(Error::TypeMismatch {
expected: "object",
found: "primitive/array",
});
}
let end = skip_value(patch, start)?;
let mut pos = skip_whitespace(patch, start + 1);
let mut entries = Vec::new();
if pos < end && patch[pos] == b'}' {
return Ok(entries);
}
loop {
pos = skip_whitespace(patch, pos);
if pos >= end {
return Err(Error::InvalidJsonSyntax {
pos,
msg: "Unexpected EOF in merge patch object",
});
}
if patch[pos] == b'}' {
break;
}
if patch[pos] != b'"' {
return Err(Error::InvalidJsonSyntax {
pos,
msg: "Expected object key in merge patch",
});
}
let key_content_start = pos + 1;
let key_end = find_string_end(patch, key_content_start)?;
let key_raw = &patch[key_content_start..key_end];
let key = if !key_raw.contains(&b'\\') {
std::str::from_utf8(key_raw)
.map_err(|_| Error::TypeMismatch {
expected: "utf-8 key",
found: "invalid utf-8",
})?
.to_string()
} else {
let mut lit = Vec::with_capacity(key_raw.len() + 2);
lit.push(b'"');
lit.extend_from_slice(key_raw);
lit.push(b'"');
crate::convert::from_json_string(&lit).ok_or(Error::TypeMismatch {
expected: "string key",
found: "invalid escape",
})?
};
pos = skip_whitespace(patch, key_end + 1);
if pos >= end || patch[pos] != b':' {
return Err(Error::InvalidJsonSyntax {
pos,
msg: "Expected colon in merge patch",
});
}
pos = skip_whitespace(patch, pos + 1);
let val_start = pos;
let val_end = skip_value(patch, val_start)?;
entries.push((key, patch[val_start..val_end].to_vec()));
pos = skip_whitespace(patch, val_end);
if pos >= end {
return Err(Error::InvalidJsonSyntax {
pos,
msg: "Unexpected EOF after merge patch value",
});
}
if patch[pos] == b',' {
pos += 1;
} else if patch[pos] == b'}' {
break;
} else {
return Err(Error::InvalidJsonSyntax {
pos,
msg: "Expected comma or '}' in merge patch",
});
}
}
Ok(entries)
}