Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_jdat/src/string/json_edit.rs

20.7 KiB, 36 runs

created by r1870400018:75865, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! One JSON object's text, edited member by member in place.
2//!
3//! A file of records, one JSON object to a line, is often written by more than one program, and
4//! a newer writer adds members an older one has never heard of. A writer that decodes a record,
5//! changes a field and encodes it again drops those members, reorders the rest and changes the
6//! spacing. [`JsonObjectText`] changes the bytes of the members it is asked to and no others.
7//!
8//! The text is held to RFC 8259 as `Dat::decode_json_strict` holds it, and refused on the same
9//! grounds, an object naming a member twice among them. The walk here builds no daticle: it reads
10//! the grammar and records where each top-level member lies.
11
12use crate::{
13 bdat::limits::DecodeLimits,
14 string::enc::escape_json_string,
15};
16
17use oxedyne_fe2o3_core::prelude::*;
18
19use std::{
20 ops::Range,
21 str,
22};
23
24
25/// One top-level member of a [`JsonObjectText`]: its name as the text says it, unescaped, the byte
26/// its name's opening quote stands at, and the span of its value.
27#[derive(Clone, Debug, Eq, PartialEq)]
28pub struct JsonMember {
29 pub key: String,
30 pub start: usize,
31 pub val: Range<usize>,
32}
33
34/// One JSON object held as its text and edited member by member, in place.
35///
36/// A member is set, added or removed and every byte outside it stays as it was: the other
37/// members, known to the caller or not, their order, the whitespace and the line endings.
38///
39/// Anything that is not exactly one JSON object, with only JSON whitespace around it, is refused,
40/// and so is an object that names a member twice, since there is then no one member to edit. Only
41/// top-level members are edited; a member of the same name nested deeper is never touched.
42#[derive(Clone, Debug)]
43pub struct JsonObjectText {
44 text: String,
45 members: Vec<JsonMember>,
46 close: usize, // the byte of the object's closing brace
47 limits: DecodeLimits,
48}
49
50impl JsonObjectText {
51
52 /// Reads `text` as one JSON object under the text limits.
53 pub fn new(text: &str) -> Outcome<Self> {
54 Self::new_with_limits(text, &DecodeLimits::text())
55 }
56
57 /// Reads `text` as one JSON object; `limits` bound its length and nesting, the object at depth
58 /// 1, here and after each edit.
59 pub fn new_with_limits(text: &str, limits: &DecodeLimits) -> Outcome<Self> {
60 let (members, close) = res!(Self::scan(text, limits));
61 Ok(Self { text: text.to_string(), members, close, limits: *limits })
62 }
63
64 pub fn as_str(&self) -> &str { &self.text }
65 pub fn into_string(self) -> String { self.text }
66 pub fn members(&self) -> &[JsonMember] { &self.members }
67
68 /// The names of the top-level members, in the order written.
69 pub fn keys(&self) -> Vec<String> {
70 self.members.iter().map(|m| m.key.clone()).collect()
71 }
72
73 /// The text of the value of the member named `key`, as written.
74 pub fn get(&self, key: &str) -> Option<&str> {
75 self.find(key).map(|i| &self.text[self.members[i].val.clone()])
76 }
77
78 /// The value of the top-level member named `key` decoded, when that value is a JSON string:
79 /// every escape, `\uXXXX` and surrogate pairs among them, read as RFC 8259 reads it. Nothing
80 /// when there is no such member or its value is not a string.
81 pub fn get_str(&self, key: &str) -> Option<String> {
82 let val = match self.get(key) {
83 Some(v) if v.starts_with('"') => v,
84 _ => return None,
85 };
86 // The text was read whole when it was taken, so this string decodes.
87 let mut w = Walk { b: val.as_bytes(), pos: 0, limits: self.limits };
88 w.string().ok()
89 }
90
91 /// Gives the member named `key` the value `val`, JSON text, in place of the value it had, or
92 /// adds it after the last member when there is none. `val` must be exactly one JSON value,
93 /// and a refused `val` changes nothing.
94 pub fn set(&mut self, key: &str, val: &str) -> Outcome<()> {
95 res!(self.limits.check_len(val.len()));
96 let mut w = Walk { b: val.as_bytes(), pos: 0, limits: self.limits };
97 w.ws();
98 let v0 = w.pos;
99 res!(w.value(2));
100 let v1 = w.pos;
101 w.ws();
102 if w.pos < w.b.len() {
103 return Err(err!(
104 "The value for the member {:?} must be one JSON value, and more follows it at \
105 byte {} of {}.", key, w.pos, w.b.len();
106 Invalid, Input, Decode));
107 }
108 let val = &val[v0..v1];
109 let mut text = self.text.clone();
110 match self.find(key) {
111 Some(i) => text.replace_range(self.members[i].val.clone(), val),
112 None => {
113 let (at, sep) = match self.members.last() {
114 Some(m) => (m.val.end, ","),
115 None => (self.close, ""),
116 };
117 text.insert_str(at, &fmt!("{}\"{}\":{}", sep, escape_json_string(key), val));
118 },
119 }
120 self.commit(text)
121 }
122
123 /// Removes the member named `key` with the comma that joined it, the one before it or, for
124 /// the first member, the one after. Whether there was such a member.
125 pub fn remove(&mut self, key: &str) -> Outcome<bool> {
126 let i = match self.find(key) {
127 Some(i) => i,
128 None => return Ok(false),
129 };
130 let ms = &self.members;
131 let span = if i > 0 {
132 ms[i - 1].val.end..ms[i].val.end
133 } else if let Some(next) = ms.get(1) {
134 ms[0].start..next.start
135 } else {
136 ms[0].start..ms[0].val.end
137 };
138 let mut text = self.text.clone();
139 text.replace_range(span, "");
140 res!(self.commit(text));
141 Ok(true)
142 }
143
144 fn find(&self, key: &str) -> Option<usize> {
145 self.members.iter().position(|m| m.key == key)
146 }
147
148 // An edit is read back before it is kept, so the text always holds one object.
149 fn commit(&mut self, text: String) -> Outcome<()> {
150 let (members, close) = res!(Self::scan(&text, &self.limits));
151 self.text = text;
152 self.members = members;
153 self.close = close;
154 Ok(())
155 }
156
157 fn scan(text: &str, limits: &DecodeLimits) -> Outcome<(Vec<JsonMember>, usize)> {
158 res!(limits.check_len(text.len()));
159 let mut w = Walk { b: text.as_bytes(), pos: 0, limits: *limits };
160 w.ws();
161 if w.peek() != Some(b'{') {
162 return Err(err!(
163 "The text of a JSON object opens with '{{', and byte {} holds {}.",
164 w.pos, w.found();
165 Invalid, Input, Decode));
166 }
167 res!(limits.check_depth(1, w.pos));
168 let (members, close) = res!(w.object(1));
169 w.ws();
170 if w.pos < w.b.len() {
171 return Err(err!(
172 "The text of a JSON object holds one object, and more follows it at byte {} of \
173 {}.", w.pos, w.b.len();
174 Invalid, Input, Decode));
175 }
176 Ok((members, close))
177 }
178}
179
180/// The text of the value of the top-level member named `key` in `text`, borrowed from it, and
181/// nothing when the object has no such member.
182///
183/// `text` is held to the grammar and `limits` that [`JsonObjectText::new_with_limits`] holds it
184/// to, and the member is found by its unescaped name, but no value is copied or decoded: a large
185/// object read for one small member costs one walk, and a caller holding the text in wiped memory
186/// leaves no copy of any value behind. Member names are read, to refuse one named twice.
187pub fn member<'a>(text: &'a str, key: &str, limits: &DecodeLimits) -> Outcome<Option<&'a str>> {
188 let (members, _) = res!(JsonObjectText::scan(text, limits));
189 Ok(members.into_iter().find(|m| m.key == key).map(|m| &text[m.val]))
190}
191
192/// The span of each element of the JSON array that `val` holds in `text`, in the order written,
193/// and none for an empty one.
194///
195/// `val` is the span of an array within `text`, as [`JsonObjectText`] gives a member's value, and
196/// the items are found by the grammar and `limits` it holds the text to, no item copied: a caller
197/// splicing one item of a large list in place keeps every byte outside that item as it was.
198pub fn element_spans(
199 text: &str,
200 val: &Range<usize>,
201 limits: &DecodeLimits,
202)
203 -> Outcome<Vec<Range<usize>>>
204{
205 res!(limits.check_len(text.len()));
206 if val.start >= val.end || val.end > text.len() {
207 return Err(err!(
208 "The span {}..{} is not a value in {} bytes of JSON text.", val.start, val.end,
209 text.len();
210 Invalid, Input, Decode));
211 }
212 let mut w = Walk { b: text.as_bytes(), pos: val.start, limits: *limits };
213 let spans = res!(w.elements(1));
214 if w.pos != val.end {
215 return Err(err!(
216 "The JSON array at byte {} ends at byte {}, and its value span ends at {}.", val.start,
217 w.pos, val.end;
218 Invalid, Input, Decode));
219 }
220 Ok(spans)
221}
222
223/// A walk through JSON text that checks the grammar and builds no value.
224struct Walk<'a> {
225 b: &'a [u8],
226 pos: usize,
227 limits: DecodeLimits,
228}
229
230impl<'a> Walk<'a> {
231
232 fn peek(&self) -> Option<u8> {
233 self.b.get(self.pos).copied()
234 }
235
236 fn ws(&mut self) {
237 while let Some(b' ' | b'\t' | b'\n' | b'\r') = self.peek() {
238 self.pos += 1;
239 }
240 }
241
242 fn digits(&mut self) -> usize {
243 let start = self.pos;
244 while let Some(b'0'..=b'9') = self.peek() {
245 self.pos += 1;
246 }
247 self.pos - start
248 }
249
250 /// What the byte at the cursor is, for an error.
251 fn found(&self) -> String {
252 match self.peek() {
253 Some(b) if b.is_ascii_graphic() => fmt!("'{}'", b as char),
254 Some(b) => fmt!("byte 0x{:02x}", b),
255 None => fmt!("the end of the text"),
256 }
257 }
258
259 fn value(&mut self, depth: usize) -> Outcome<()> {
260 res!(self.limits.check_depth(depth, self.pos));
261 match self.peek() {
262 Some(b'{') => self.object(depth).map(|_| ()),
263 Some(b'[') => self.array(depth),
264 Some(b'"') => self.string_to(None),
265 Some(b't') => self.literal("true"),
266 Some(b'f') => self.literal("false"),
267 Some(b'n') => self.literal("null"),
268 Some(b'-' | b'0'..=b'9') => self.number(),
269 _ => Err(err!(
270 "A JSON value begins with '{{', '[', '\"', a digit, '-', true, false or null, \
271 and byte {} holds {}.", self.pos, self.found();
272 Invalid, Input, Decode)),
273 }
274 }
275
276 /// The members of the object whose '{' is at the cursor, and the byte of its '}'.
277 fn object(&mut self, depth: usize) -> Outcome<(Vec<JsonMember>, usize)> {
278 let open = self.pos;
279 self.pos += 1;
280 let mut members: Vec<JsonMember> = Vec::new();
281 self.ws();
282 if self.peek() == Some(b'}') {
283 self.pos += 1;
284 return Ok((members, self.pos - 1));
285 }
286 loop {
287 self.ws();
288 if self.peek() != Some(b'"') {
289 return Err(err!(
290 "A member of the JSON object opened at byte {} is named by a string, and \
291 byte {} holds {}.", open, self.pos, self.found();
292 Invalid, Input, Decode));
293 }
294 let start = self.pos;
295 let key = res!(self.string());
296 self.ws();
297 if self.peek() != Some(b':') {
298 return Err(err!(
299 "A member name in the JSON object opened at byte {} is followed by ':', and \
300 byte {} holds {}.", open, self.pos, self.found();
301 Invalid, Input, Decode));
302 }
303 self.pos += 1;
304 self.ws();
305 let v0 = self.pos;
306 res!(self.value(depth + 1));
307 if members.iter().any(|m| m.key == key) {
308 return Err(err!(
309 "The JSON object opened at byte {} names the member {:?} a second time, at \
310 byte {}.", open, key, start;
311 Invalid, Input, Duplicate));
312 }
313 members.push(JsonMember { key, start, val: v0..self.pos });
314 self.ws();
315 match self.peek() {
316 Some(b',') => self.pos += 1,
317 Some(b'}') => {
318 self.pos += 1;
319 return Ok((members, self.pos - 1));
320 },
321 _ => return Err(err!(
322 "A member of the JSON object opened at byte {} is followed by ',' or '}}', \
323 and byte {} holds {}.", open, self.pos, self.found();
324 Invalid, Input, Decode)),
325 }
326 }
327 }
328
329 fn array(&mut self, depth: usize) -> Outcome<()> {
330 self.elements(depth).map(|_| ())
331 }
332
333 /// The spans of the elements of the array whose '[' is at the cursor, and the byte after its
334 /// ']'; an empty array has none.
335 fn elements(&mut self, depth: usize) -> Outcome<Vec<Range<usize>>> {
336 res!(self.limits.check_depth(depth, self.pos));
337 let open = self.pos;
338 if self.peek() != Some(b'[') {
339 return Err(err!(
340 "A JSON array opens with '[', and byte {} holds {}.", self.pos, self.found();
341 Invalid, Input, Decode));
342 }
343 self.pos += 1;
344 let mut spans: Vec<Range<usize>> = Vec::new();
345 self.ws();
346 if self.peek() == Some(b']') {
347 self.pos += 1;
348 return Ok(spans);
349 }
350 loop {
351 self.ws();
352 let v0 = self.pos;
353 res!(self.value(depth + 1));
354 spans.push(v0..self.pos);
355 self.ws();
356 match self.peek() {
357 Some(b',') => self.pos += 1,
358 Some(b']') => {
359 self.pos += 1;
360 return Ok(spans);
361 },
362 _ => return Err(err!(
363 "An item of the JSON array opened at byte {} is followed by ',' or ']', and \
364 byte {} holds {}.", open, self.pos, self.found();
365 Invalid, Input, Decode)),
366 }
367 }
368 }
369
370 fn literal(&mut self, word: &str) -> Outcome<()> {
371 if self.b[self.pos..].starts_with(word.as_bytes()) {
372 self.pos += word.len();
373 Ok(())
374 } else {
375 Err(err!(
376 "Byte {} begins a word that is not the JSON literal {}.", self.pos, word;
377 Invalid, Input, Decode))
378 }
379 }
380
381 fn number(&mut self) -> Outcome<()> {
382 let start = self.pos;
383 if self.peek() == Some(b'-') {
384 self.pos += 1;
385 }
386 match self.peek() {
387 Some(b'0') => {
388 self.pos += 1;
389 if let Some(b'0'..=b'9') = self.peek() {
390 return Err(err!(
391 "The JSON number at byte {} has a leading zero.", start;
392 Invalid, Input, Decode));
393 }
394 },
395 Some(b'1'..=b'9') => { self.digits(); },
396 _ => return Err(err!(
397 "The JSON number at byte {} has no digit before byte {}, which holds {}.",
398 start, self.pos, self.found();
399 Invalid, Input, Decode)),
400 }
401 if self.peek() == Some(b'.') {
402 self.pos += 1;
403 if self.digits() == 0 {
404 return Err(err!(
405 "The JSON number at byte {} has no digit after its '.'.", start;
406 Invalid, Input, Decode));
407 }
408 }
409 if let Some(b'e' | b'E') = self.peek() {
410 self.pos += 1;
411 if let Some(b'+' | b'-') = self.peek() {
412 self.pos += 1;
413 }
414 if self.digits() == 0 {
415 return Err(err!(
416 "The JSON number at byte {} has no digit in its exponent.", start;
417 Invalid, Input, Decode));
418 }
419 }
420 Ok(())
421 }
422
423 /// The string whose '"' is at the cursor, unescaped.
424 fn string(&mut self) -> Outcome<String> {
425 let mut out = String::new();
426 res!(self.string_to(Some(&mut out)));
427 Ok(out)
428 }
429
430 /// Reads the string whose '"' is at the cursor, unescaped into `out` when there is one. A
431 /// value is read with none, so the walk copies no value it passes.
432 fn string_to(&mut self, mut out: Option<&mut String>) -> Outcome<()> {
433 let open = self.pos;
434 self.pos += 1;
435 let mut run = self.pos; // start of the unescaped run being read
436 loop {
437 match self.peek() {
438 Some(b'"') => {
439 let txt = res!(str::from_utf8(&self.b[run..self.pos]), Decode, UTF8);
440 if let Some(o) = out.as_mut() {
441 o.push_str(txt);
442 }
443 self.pos += 1;
444 return Ok(());
445 },
446 Some(b'\\') => {
447 let txt = res!(str::from_utf8(&self.b[run..self.pos]), Decode, UTF8);
448 self.pos += 1;
449 let c = res!(self.escape());
450 if let Some(o) = out.as_mut() {
451 o.push_str(txt);
452 o.push(c);
453 }
454 run = self.pos;
455 },
456 Some(b) if b < 0x20 => return Err(err!(
457 "The JSON string opened at byte {} holds the control byte 0x{:02x} at byte \
458 {}, which must be escaped.", open, b, self.pos;
459 Invalid, Input, Decode)),
460 Some(_) => self.pos += 1,
461 None => return Err(err!(
462 "The JSON string opened at byte {} is never closed.", open;
463 Invalid, Input, Decode)),
464 }
465 }
466 }
467
468 /// The character an escape stands for, cursor just past its backslash.
469 fn escape(&mut self) -> Outcome<char> {
470 let at = self.pos - 1;
471 let c = match self.peek() {
472 Some(c) => c,
473 None => return Err(err!(
474 "The JSON text ends inside the escape at byte {}.", at;
475 Invalid, Input, Decode)),
476 };
477 self.pos += 1;
478 Ok(match c {
479 b'"' => '"',
480 b'\\' => '\\',
481 b'/' => '/',
482 b'b' => '\u{0008}',
483 b'f' => '\u{000C}',
484 b'n' => '\n',
485 b'r' => '\r',
486 b't' => '\t',
487 b'u' => {
488 let unit = res!(self.hex4());
489 let cp = match unit {
490 0xD800..=0xDBFF => {
491 if !self.b[self.pos..].starts_with(b"\\u") {
492 return Err(err!(
493 "The escape at byte {} is a high surrogate with no low \
494 surrogate escape after it.", at;
495 Invalid, Input, Decode));
496 }
497 self.pos += 2;
498 let low = res!(self.hex4());
499 if !(0xDC00..=0xDFFF).contains(&low) {
500 return Err(err!(
501 "The escape at byte {} is a high surrogate followed by \
502 \\u{:04X}, which is not a low surrogate.", at, low;
503 Invalid, Input, Decode));
504 }
505 0x10000 + ((unit - 0xD800) << 10) + (low - 0xDC00)
506 },
507 0xDC00..=0xDFFF => return Err(err!(
508 "The escape at byte {} is a low surrogate with no high surrogate \
509 before it.", at;
510 Invalid, Input, Decode)),
511 _ => unit,
512 };
513 match char::from_u32(cp) {
514 Some(ch) => ch,
515 None => return Err(err!(
516 "The escape at byte {} names U+{:X}, which is not a character.", at, cp;
517 Invalid, Input, Decode)),
518 }
519 },
520 _ => return Err(err!(
521 "The escape at byte {} is '\\{}', which JSON does not have.", at,
522 (c as char).escape_default();
523 Invalid, Input, Decode)),
524 })
525 }
526
527 fn hex4(&mut self) -> Outcome<u32> {
528 let mut v = 0u32;
529 for _ in 0..4 {
530 let d = match self.peek() {
531 Some(b @ b'0'..=b'9') => b - b'0',
532 Some(b @ b'a'..=b'f') => b - b'a' + 10,
533 Some(b @ b'A'..=b'F') => b - b'A' + 10,
534 _ => return Err(err!(
535 "A \\u escape takes four hex digits, and byte {} holds {}.",
536 self.pos, self.found();
537 Invalid, Input, Decode)),
538 };
539 v = (v << 4) | d as u32;
540 self.pos += 1;
541 }
542 Ok(v)
543 }
544}