1use crate::code_map::{JsonCodeMapOffset, JsonMapped};
2use crate::lexical::{JsonBorrowLexical, JsonLexicalEq, JsonLexicalHash, JsonLexicalPartialEq};
3use crate::{JsonCodeMap, JsonFragment, JsonValue};
4use btree_indexmap::{BTreeIndexMultiMap, Comparable};
5use core::fmt;
6use core::hash::{Hash, Hasher};
7
8pub use btree_indexmap::multi_map::IndexesIter;
9
10pub const KEY_CAPACITY: usize = 16;
15
16pub type Key = smallstr::SmallString<[u8; KEY_CAPACITY]>;
18
19pub type Entry = (Key, JsonValue);
21
22pub type EntryRef<'a> = (&'a Key, &'a JsonValue);
24
25fn entry_ref((k, v): &Entry) -> EntryRef<'_> {
26 (k, v)
27}
28
29fn get_entry_fragment<'a>(entry: &'a Entry, index: usize) -> Result<JsonFragment<'a>, usize> {
30 match index {
31 0 => Ok(JsonFragment::Entry(entry_ref(entry))),
32 1 => Ok(JsonFragment::Key(&entry.0)),
33 _ => entry.1.get_fragment(index - 2),
34 }
35}
36
37pub type MappedEntry = JsonMapped<(JsonMapped<Key>, JsonMapped<JsonValue>)>;
39
40fn mapped_entry(
41 (key, value): Entry,
42 offset: usize,
43 key_offset: usize,
44 value_offset: usize,
45) -> MappedEntry {
46 JsonMapped(
47 (JsonMapped(key, key_offset), JsonMapped(value, value_offset)),
48 offset,
49 )
50}
51
52pub type MappedEntryRef<'a> = JsonMapped<(JsonMapped<&'a Key>, JsonMapped<&'a JsonValue>)>;
54
55fn mapped_entry_ref(
56 (key, value): EntryRef,
57 offset: usize,
58 key_offset: usize,
59 value_offset: usize,
60) -> MappedEntryRef {
61 JsonMapped(
62 (JsonMapped(key, key_offset), JsonMapped(value, value_offset)),
63 offset,
64 )
65}
66
67pub type IndexedMappedEntry<'a> = (usize, MappedEntryRef<'a>);
68
69pub type IndexedMappedValue<'a> = (usize, JsonMapped<&'a JsonValue>);
70
71#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
83pub struct JsonObject {
84 map: BTreeIndexMultiMap<Key, JsonValue>,
85}
86
87impl Default for JsonObject {
88 fn default() -> Self {
89 Self {
90 map: BTreeIndexMultiMap::new(),
91 }
92 }
93}
94
95impl JsonObject {
96 pub fn new() -> Self {
97 Self::default()
98 }
99
100 pub fn from_vec(entries: Vec<(Key, JsonValue)>) -> Self {
101 Self {
102 map: entries.into_iter().collect(),
103 }
104 }
105
106 pub fn capacity(&self) -> usize {
107 self.map.capacity()
108 }
109
110 pub fn len(&self) -> usize {
111 self.map.len()
112 }
113
114 pub fn is_empty(&self) -> bool {
115 self.map.is_empty()
116 }
117
118 pub fn get_fragment(&self, mut index: usize) -> Result<JsonFragment<'_>, usize> {
119 for e in self.map.as_entries() {
120 match get_entry_fragment(e, index) {
121 Ok(value) => return Ok(value),
122 Err(i) => index = i,
123 }
124 }
125
126 Err(index)
127 }
128
129 pub fn entries(&self) -> &[Entry] {
130 self.map.as_entries()
131 }
132
133 pub fn iter(&self) -> Iter<'_> {
134 self.map.iter()
135 }
136
137 pub fn iter_mut(&mut self) -> IterMut<'_> {
138 self.map.iter_mut()
139 }
140
141 pub fn iter_mapped<'m>(&self, code_map: &'m JsonCodeMap, offset: usize) -> IterMapped<'_, 'm> {
142 IterMapped {
143 entries: self.map.iter(),
144 code_map,
145 offset: offset + 1,
146 }
147 }
148
149 pub fn into_iter_mapped<'m>(
150 self,
151 code_map: &'m JsonCodeMap,
152 offset: usize,
153 ) -> IntoIterMapped<'m> {
154 IntoIterMapped {
155 entries: self.map.into_iter(),
156 code_map,
157 offset: offset + 1,
158 }
159 }
160
161 pub fn iter_sorted(&self) -> IterSorted<'_> {
162 self.map.iter_sorted()
163 }
164
165 pub fn iter_mut_sorted(&mut self) -> IterMutSorted<'_> {
166 self.map.iter_mut_sorted()
167 }
168
169 pub fn into_iter_sorted(self) -> IntoIterSorted {
170 self.map.into_iter_sorted()
171 }
172
173 pub fn contains_key<Q>(&self, key: &Q) -> bool
177 where
178 Q: ?Sized + Comparable<Key>,
179 {
180 self.map.contains_key(key)
181 }
182
183 pub fn get<Q>(&self, key: &Q) -> Get<'_>
187 where
188 Q: ?Sized + Comparable<Key>,
189 {
190 self.map.get(key)
191 }
192
193 pub fn key_occurrences<Q>(&self, key: &Q) -> usize
205 where
206 Q: ?Sized + Comparable<Key>,
207 {
208 self.map.key_occurences(key)
209 }
210
211 pub fn find_duplicate_key(&self) -> Option<&str> {
223 for (key, _) in self.iter() {
224 if self.key_occurrences(key.as_str()) > 1 {
225 return Some(key.as_str());
226 }
227 }
228
229 None
230 }
231
232 pub fn get_mut<Q>(&mut self, key: &Q) -> GetMut<'_>
236 where
237 Q: ?Sized + Comparable<Key>,
238 {
239 self.map.get_mut(key)
240 }
241
242 pub fn get_unique<Q>(&self, key: &Q) -> Result<Option<&JsonValue>, DuplicateEntry<EntryRef<'_>>>
248 where
249 Q: ?Sized + Comparable<Key>,
250 {
251 let mut entries = self.map.get_entries(key);
252
253 match entries.next() {
254 Some(entry) => match entries.next() {
255 Some(duplicate) => Err(DuplicateEntry::new(entry, duplicate)),
256 None => Ok(Some(entry.1)),
257 },
258 None => Ok(None),
259 }
260 }
261
262 pub fn get_unique_mut<Q>(
268 &mut self,
269 key: &Q,
270 ) -> Result<Option<&mut JsonValue>, DuplicateEntry<EntryRef<'_>>>
271 where
272 Q: ?Sized + Comparable<Key>,
273 {
274 let mut entries = self.map.get_entries_mut(key);
275
276 match entries.next() {
277 Some(entry) => match entries.next() {
278 Some(duplicate) => {
279 let entry = (entry.0, &*entry.1);
280 let duplicate = (duplicate.0, &*duplicate.1);
281 Err(DuplicateEntry::new(entry, duplicate))
282 }
283 None => Ok(Some(entry.1)),
284 },
285 None => Ok(None),
286 }
287 }
288
289 pub fn get_entries<Q>(&self, key: &Q) -> GetEntries<'_>
293 where
294 Q: ?Sized + Comparable<Key>,
295 {
296 self.map.get_entries(key)
297 }
298
299 pub fn get_unique_entry<Q>(
305 &self,
306 key: &Q,
307 ) -> Result<Option<EntryRef<'_>>, DuplicateEntry<EntryRef<'_>>>
308 where
309 Q: ?Sized + Comparable<Key>,
310 {
311 let mut entries = self.get_entries(key);
312
313 match entries.next() {
314 Some(entry) => match entries.next() {
315 Some(duplicate) => Err(DuplicateEntry::new(entry, duplicate)),
316 None => Ok(Some(entry)),
317 },
318 None => Ok(None),
319 }
320 }
321
322 pub fn get_indexed<Q>(&self, key: &Q) -> GetIndexed<'_>
326 where
327 Q: ?Sized + Comparable<Key>,
328 {
329 self.map.get_indexed(key)
330 }
331
332 pub fn get_indexed_entries<Q>(&self, key: &Q) -> GetIndexedEntries<'_>
336 where
337 Q: ?Sized + Comparable<Key>,
338 {
339 self.map.get_indexed_entries(key)
340 }
341
342 pub fn get_or_insert_with<Q>(
345 &mut self,
346 key: impl Into<Key>,
347 f: impl FnOnce() -> JsonValue,
348 ) -> Get<'_> {
349 self.map.get_or_insert_with(key.into(), f)
350 }
351
352 pub fn get_or_insert_mut_with<Q>(
356 &mut self,
357 key: impl Into<Key>,
358 f: impl FnOnce() -> JsonValue,
359 ) -> GetMut<'_> {
360 self.map.get_or_insert_mut_with(key.into(), f)
361 }
362
363 pub fn index_of<Q>(&self, key: &Q) -> Option<usize>
364 where
365 Q: ?Sized + Comparable<Key>,
366 {
367 self.map.index_of(key)
368 }
369
370 pub fn indexes_of<Q>(&self, key: &Q) -> IndexesIter<'_>
371 where
372 Q: ?Sized + Comparable<Key>,
373 {
374 self.map.indexes_of(key)
375 }
376
377 pub fn redundant_index_of<Q>(&self, key: &Q) -> Option<usize>
378 where
379 Q: ?Sized + Comparable<Key>,
380 {
381 self.map.indexes_of(key).nth(1)
382 }
383
384 pub fn get_mapped_entries<'m, Q>(
389 &self,
390 code_map: &'m JsonCodeMap,
391 offset: usize,
392 key: &Q,
393 ) -> GetMappedEntries<'_, 'm>
394 where
395 Q: ?Sized + Comparable<Key>,
396 {
397 GetMappedEntries {
398 indexes: self.map.indexes_of(key),
399 object: self,
400 code_map,
401 offset: offset + 1,
402 last_index: 0,
403 }
404 }
405
406 pub fn get_unique_mapped_entry<Q>(
411 &self,
412 code_map: &JsonCodeMap,
413 offset: usize,
414 key: &Q,
415 ) -> Result<Option<MappedEntryRef<'_>>, DuplicateEntry<MappedEntryRef<'_>>>
416 where
417 Q: ?Sized + Comparable<Key>,
418 {
419 let mut entries = self.get_mapped_entries(code_map, offset, key);
420
421 match entries.next() {
422 Some(entry) => match entries.next() {
423 Some(duplicate) => Err(DuplicateEntry::new(entry, duplicate)),
424 None => Ok(Some(entry)),
425 },
426 None => Ok(None),
427 }
428 }
429
430 pub fn get_mapped_entries_with_index<'m, Q>(
436 &self,
437 code_map: &'m JsonCodeMap,
438 offset: usize,
439 key: &Q,
440 ) -> GetIndexedMappedEntries<'_, 'm>
441 where
442 Q: ?Sized + Comparable<Key>,
443 {
444 GetIndexedMappedEntries {
445 indexes: self.map.indexes_of(key),
446 object: self,
447 code_map,
448 offset: offset + 1,
449 last_index: 0,
450 }
451 }
452
453 pub fn get_unique_mapped_entry_with_index<Q>(
458 &self,
459 code_map: &JsonCodeMap,
460 offset: usize,
461 key: &Q,
462 ) -> Result<Option<IndexedMappedEntry<'_>>, DuplicateEntry<IndexedMappedEntry<'_>>>
463 where
464 Q: ?Sized + Comparable<Key>,
465 {
466 let mut entries = self.get_mapped_entries_with_index(code_map, offset, key);
467
468 match entries.next() {
469 Some(entry) => match entries.next() {
470 Some(duplicate) => Err(DuplicateEntry::new(entry, duplicate)),
471 None => Ok(Some(entry)),
472 },
473 None => Ok(None),
474 }
475 }
476
477 pub fn get_mapped<'m, Q>(
482 &self,
483 code_map: &'m JsonCodeMap,
484 offset: usize,
485 key: &Q,
486 ) -> GetMapped<'_, 'm>
487 where
488 Q: ?Sized + Comparable<Key>,
489 {
490 GetMapped {
491 indexes: self.map.indexes_of(key),
492 object: self,
493 code_map,
494 offset: offset + 1,
495 last_index: 0,
496 }
497 }
498
499 pub fn get_unique_mapped<Q>(
504 &self,
505 code_map: &JsonCodeMap,
506 offset: usize,
507 key: &Q,
508 ) -> Result<Option<JsonMapped<&JsonValue>>, DuplicateEntry<JsonMapped<&JsonValue>>>
509 where
510 Q: ?Sized + Comparable<Key>,
511 {
512 let mut entries = self.get_mapped(code_map, offset, key);
513
514 match entries.next() {
515 Some(entry) => match entries.next() {
516 Some(duplicate) => Err(DuplicateEntry::new(entry, duplicate)),
517 None => Ok(Some(entry)),
518 },
519 None => Ok(None),
520 }
521 }
522
523 pub fn get_indexed_mapped<'m, Q>(
529 &self,
530 code_map: &'m JsonCodeMap,
531 offset: usize,
532 key: &Q,
533 ) -> GetIndexedMapped<'_, 'm>
534 where
535 Q: ?Sized + Comparable<Key>,
536 {
537 GetIndexedMapped {
538 indexes: self.map.indexes_of(key),
539 object: self,
540 code_map,
541 offset: offset + 1,
542 last_index: 0,
543 }
544 }
545
546 pub fn get_unique_mapped_with_index<Q>(
551 &self,
552 code_map: &JsonCodeMap,
553 offset: usize,
554 key: &Q,
555 ) -> Result<Option<IndexedMappedValue<'_>>, DuplicateEntry<IndexedMappedValue<'_>>>
556 where
557 Q: ?Sized + Comparable<Key>,
558 {
559 let mut entries = self.get_indexed_mapped(code_map, offset, key);
560
561 match entries.next() {
562 Some(entry) => match entries.next() {
563 Some(duplicate) => Err(DuplicateEntry::new(entry, duplicate)),
564 None => Ok(Some(entry)),
565 },
566 None => Ok(None),
567 }
568 }
569
570 pub fn first(&self) -> Option<EntryRef<'_>> {
571 self.map.first()
572 }
573
574 pub fn last(&self) -> Option<EntryRef<'_>> {
575 self.map.last()
576 }
577
578 pub fn push_back(&mut self, key: impl Into<Key>, value: JsonValue) -> (usize, bool) {
587 self.map.push_back(key.into(), value)
588 }
589
590 pub fn push_entry_back(&mut self, (key, value): Entry) -> (usize, bool) {
591 self.map.push_back(key, value)
592 }
593
594 pub fn push_front(&mut self, key: impl Into<Key>, value: JsonValue) -> bool {
603 self.map.push_front(key.into(), value)
604 }
605
606 pub fn push_entry_front(&mut self, (key, value): Entry) -> bool {
607 self.map.push_front(key, value)
608 }
609
610 pub fn swap_remove_at(&mut self, i: usize) -> Option<Entry> {
612 self.map.swap_remove_at(i)
613 }
614
615 pub fn shift_remove_at(&mut self, i: usize) -> Option<Entry> {
617 self.map.shift_remove_at(i)
618 }
619
620 pub fn shift_insert(&mut self, key: impl Into<Key>, value: JsonValue) -> ShiftInsert<'_> {
626 self.map.shift_insert(key.into(), value)
627 }
628
629 pub fn swap_insert(&mut self, key: impl Into<Key>, value: JsonValue) -> SwapInsert<'_> {
630 self.map.swap_insert(key.into(), value)
631 }
632
633 pub fn shift_insert_front(
638 &mut self,
639 key: impl Into<Key>,
640 value: JsonValue,
641 ) -> ShiftInsertFront<'_> {
642 self.map.shift_insert_front(key.into(), value)
643 }
644
645 pub fn swap_insert_front(
646 &mut self,
647 key: impl Into<Key>,
648 value: JsonValue,
649 ) -> SwapInsertFront<'_> {
650 self.map.swap_insert_front(key.into(), value)
651 }
652
653 pub fn shift_insert_back_full(
654 &mut self,
655 key: impl Into<Key>,
656 value: JsonValue,
657 ) -> (usize, ShiftInsertBack<'_>) {
658 self.map.shift_insert_back_full(key.into(), value)
659 }
660
661 pub fn shift_insert_back(
662 &mut self,
663 key: impl Into<Key>,
664 value: JsonValue,
665 ) -> ShiftInsertBack<'_> {
666 self.map.shift_insert_back(key.into(), value)
667 }
668
669 pub fn insert(&mut self, key: impl Into<Key>, value: JsonValue) -> ShiftInsertBack<'_> {
671 self.shift_insert_back(key, value)
672 }
673
674 pub fn swap_insert_back_full(
675 &mut self,
676 key: impl Into<Key>,
677 value: JsonValue,
678 ) -> (usize, SwapInsertBack<'_>) {
679 self.map.swap_insert_back_full(key.into(), value)
680 }
681
682 pub fn swap_insert_back(
683 &mut self,
684 key: impl Into<Key>,
685 value: JsonValue,
686 ) -> SwapInsertBack<'_> {
687 self.map.swap_insert_back(key.into(), value)
688 }
689
690 pub fn shift_remove_indexed_entries<Q>(&mut self, key: &Q) -> ShiftRemoveIndexedEntries<'_>
691 where
692 Q: ?Sized + Comparable<Key>,
693 {
694 self.map.shift_remove_indexed_entries(key)
695 }
696
697 pub fn shift_remove_entries<Q>(&mut self, key: &Q) -> ShiftRemoveEntries<'_>
699 where
700 Q: ?Sized + Comparable<Key>,
701 {
702 self.map.shift_remove_entries(key)
703 }
704
705 pub fn shift_remove<Q>(&mut self, key: &Q) -> ShiftRemove<'_>
707 where
708 Q: ?Sized + Comparable<Key>,
709 {
710 self.map.shift_remove(key)
711 }
712
713 pub fn remove<Q>(&mut self, key: &Q) -> ShiftRemove<'_>
715 where
716 Q: ?Sized + Comparable<Key>,
717 {
718 self.shift_remove(key)
719 }
720
721 pub fn remove_unique<Q>(&mut self, key: &Q) -> Result<Option<Entry>, DuplicateEntry<Entry>>
723 where
724 Q: ?Sized + Comparable<Key>,
725 {
726 self.shift_remove_unique(key)
727 }
728
729 pub fn swap_remove_indexed_entries<Q>(&mut self, key: &Q) -> SwapRemoveIndexedEntries<'_>
730 where
731 Q: ?Sized + Comparable<Key>,
732 {
733 self.map.swap_remove_indexed_entries(key)
734 }
735
736 pub fn swap_remove_entries<Q>(&mut self, key: &Q) -> SwapRemoveEntries<'_>
737 where
738 Q: ?Sized + Comparable<Key>,
739 {
740 self.map.swap_remove_entries(key)
741 }
742
743 pub fn swap_remove<Q>(&mut self, key: &Q) -> SwapRemove<'_>
744 where
745 Q: ?Sized + Comparable<Key>,
746 {
747 self.map.swap_remove(key)
748 }
749
750 pub fn swap_remove_unique<Q>(&mut self, key: &Q) -> Result<Option<Entry>, DuplicateEntry<Entry>>
754 where
755 Q: ?Sized + Comparable<Key>,
756 {
757 let mut entries = self.swap_remove_entries(key);
758
759 match entries.next() {
760 Some(entry) => match entries.next() {
761 Some(duplicate) => Err(DuplicateEntry::new(entry, duplicate)),
762 None => Ok(Some(entry)),
763 },
764 None => Ok(None),
765 }
766 }
767
768 pub fn shift_remove_unique<Q>(
772 &mut self,
773 key: &Q,
774 ) -> Result<Option<Entry>, DuplicateEntry<Entry>>
775 where
776 Q: ?Sized + Comparable<Key>,
777 {
778 let mut entries = self.shift_remove_entries(key);
779
780 match entries.next() {
781 Some(entry) => match entries.next() {
782 Some(duplicate) => Err(DuplicateEntry::new(entry, duplicate)),
783 None => Ok(Some(entry)),
784 },
785 None => Ok(None),
786 }
787 }
788
789 pub fn sort(&mut self) {
793 self.map.sort();
794 }
795
796 #[cfg(feature = "canonicalize")]
802 pub fn canonicalize_with(&mut self, buffer: &mut ryu_js::Buffer) {
803 for (_, item) in self.iter_mut() {
804 item.canonicalize_with(buffer);
805 }
806
807 self.sort()
808 }
809
810 #[cfg(feature = "canonicalize")]
813 pub fn canonicalize(&mut self) {
814 let mut buffer = ryu_js::Buffer::new();
815 self.canonicalize_with(&mut buffer)
816 }
817}
818
819pub type Iter<'a> = btree_indexmap::multi_map::Iter<'a, Key, JsonValue>;
820
821pub type IterMut<'a> = btree_indexmap::multi_map::IterMut<'a, Key, JsonValue>;
822
823pub type IntoIter = btree_indexmap::multi_map::IntoIter<Key, JsonValue>;
824
825pub type IterSorted<'a> = btree_indexmap::multi_map::IterSorted<'a, Key, JsonValue>;
826
827pub type IterMutSorted<'a> = btree_indexmap::multi_map::IterMutSorted<'a, Key, JsonValue>;
828
829pub type IntoIterSorted = btree_indexmap::multi_map::IntoIterSorted<Key, JsonValue>;
830
831pub type Get<'a> = btree_indexmap::multi_map::Get<'a, Key, JsonValue>;
832
833pub type GetIndexed<'a> = btree_indexmap::multi_map::GetIndexed<'a, Key, JsonValue>;
834
835pub type GetEntries<'a> = btree_indexmap::multi_map::GetEntries<'a, Key, JsonValue>;
836
837pub type GetIndexedEntries<'a> = btree_indexmap::multi_map::GetIndexedEntries<'a, Key, JsonValue>;
838
839pub type GetMut<'a> = btree_indexmap::multi_map::GetMut<'a, Key, JsonValue>;
840
841pub type ShiftInsert<'a> = btree_indexmap::multi_map::ShiftInsert<'a, Key, JsonValue>;
842
843pub type SwapInsert<'a> = btree_indexmap::multi_map::SwapInsert<'a, Key, JsonValue>;
844
845pub type ShiftInsertBack<'a> = btree_indexmap::multi_map::ShiftInsertBack<'a, Key, JsonValue>;
846
847pub type SwapInsertBack<'a> = btree_indexmap::multi_map::SwapInsertBack<'a, Key, JsonValue>;
848
849pub type ShiftInsertFront<'a> = btree_indexmap::multi_map::ShiftInsertFront<'a, Key, JsonValue>;
850
851pub type SwapInsertFront<'a> = btree_indexmap::multi_map::SwapInsertFront<'a, Key, JsonValue>;
852
853pub type ShiftRemoveIndexedEntries<'a> =
854 btree_indexmap::multi_map::ShiftRemoveIndexedEntries<'a, Key, JsonValue>;
855
856pub type ShiftRemoveEntries<'a> = btree_indexmap::multi_map::ShiftRemoveEntries<'a, Key, JsonValue>;
857
858pub type ShiftRemove<'a> = btree_indexmap::multi_map::ShiftRemove<'a, Key, JsonValue>;
859
860pub type SwapRemoveIndexedEntries<'a> =
861 btree_indexmap::multi_map::SwapRemoveIndexedEntries<'a, Key, JsonValue>;
862
863pub type SwapRemoveEntries<'a> = btree_indexmap::multi_map::SwapRemoveEntries<'a, Key, JsonValue>;
864
865pub type SwapRemove<'a> = btree_indexmap::multi_map::SwapRemove<'a, Key, JsonValue>;
866
867pub struct IterMapped<'a, 'm> {
868 entries: Iter<'a>,
869 code_map: &'m JsonCodeMap,
870 offset: usize,
871}
872
873impl<'a, 'm> Iterator for IterMapped<'a, 'm> {
874 type Item = MappedEntryRef<'a>;
875
876 fn next(&mut self) -> Option<Self::Item> {
877 self.entries.next().map(|e| {
878 let offset = self.offset;
879 self.offset += 2 + self.code_map.get(self.offset + 2).unwrap().volume;
880 mapped_entry_ref(e, offset, offset + 1, offset + 2)
881 })
882 }
883}
884
885pub struct IntoIterMapped<'m> {
886 entries: IntoIter,
887 code_map: &'m JsonCodeMap,
888 offset: JsonCodeMapOffset,
889}
890
891impl<'m> Iterator for IntoIterMapped<'m> {
892 type Item = MappedEntry;
893
894 fn next(&mut self) -> Option<Self::Item> {
895 self.entries.next().map(|e| {
896 let offset = self.offset;
897 self.offset += 2 + self.code_map.get(self.offset + 2).unwrap().volume;
898 mapped_entry(e, offset, offset + 1, offset + 2)
899 })
900 }
901}
902
903macro_rules! mapped_entries_iter {
904 ($($id:ident <$lft:lifetime> {
905 type Item = $item:ty ;
906
907 fn next(&mut $self:ident, $index:ident) { $e:expr }
908 })*) => {
909 $(
910 pub struct $id<$lft, 'm> {
911 indexes: IndexesIter<$lft>,
912 object: &$lft JsonObject,
913 code_map: &'m JsonCodeMap,
914 offset: JsonCodeMapOffset,
915 last_index: usize
916 }
917
918 impl<$lft, 'm> Iterator for $id<$lft, 'm> {
919 type Item = $item;
920
921 fn next(&mut $self) -> Option<Self::Item> {
922 $self.indexes.next().map(|$index| {
923 while $self.last_index < $index {
924 $self.last_index += 1;
925 $self.offset += 2 + $self.code_map.get($self.offset+2).unwrap().volume;
926 }
927
928 $e
929 })
930 }
931 }
932 )*
933 };
934}
935
936mapped_entries_iter! {
937 GetMappedEntries<'a> {
938 type Item = MappedEntryRef<'a>;
939
940 fn next(&mut self, index) {
941 mapped_entry_ref(entry_ref(&self.object.entries()[index]), self.offset, self.offset+1, self.offset+2)
942 }
943 }
944
945 GetIndexedMappedEntries<'a> {
946 type Item = (usize, MappedEntryRef<'a>);
947
948 fn next(&mut self, index) {
949 (
950 index,
951 mapped_entry_ref(entry_ref(&self.object.entries()[index]), self.offset, self.offset+1, self.offset+2)
952 )
953 }
954 }
955
956 GetMapped<'a> {
957 type Item = JsonMapped<&'a JsonValue>;
958
959 fn next(&mut self, index) {
960 JsonMapped(
961 &self.object.entries()[index].1,
962 self.offset+2,
963 )
964 }
965 }
966
967 GetIndexedMapped<'a> {
968 type Item = (usize, JsonMapped<&'a JsonValue>);
969
970 fn next(&mut self, index) {
971 (
972 index,
973 JsonMapped(
974 &self.object.entries()[index].1,
975 self.offset+2
976 )
977 )
978 }
979 }
980}
981
982impl JsonBorrowLexical for JsonObject {}
983
984impl JsonLexicalPartialEq for JsonObject {
985 fn lexical_eq(&self, other: &Self) -> bool {
986 self.map.as_entries().eq(other.map.as_entries())
987 }
988}
989
990impl JsonLexicalEq for JsonObject {}
991
992impl JsonLexicalHash for JsonObject {
993 fn lexical_hash<H: Hasher>(&self, state: &mut H) {
994 self.map.as_entries().hash(state)
995 }
996}
997
998impl fmt::Debug for JsonObject {
999 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1000 f.debug_map().entries(self.map.iter()).finish()
1001 }
1002}
1003
1004impl From<Vec<Entry>> for JsonObject {
1005 fn from(entries: Vec<Entry>) -> Self {
1006 Self::from_vec(entries)
1007 }
1008}
1009
1010impl<'a> IntoIterator for &'a JsonObject {
1011 type Item = EntryRef<'a>;
1012 type IntoIter = Iter<'a>;
1013
1014 fn into_iter(self) -> Self::IntoIter {
1015 self.iter()
1016 }
1017}
1018
1019impl<'a> IntoIterator for &'a mut JsonObject {
1020 type Item = (&'a Key, &'a mut JsonValue);
1021 type IntoIter = IterMut<'a>;
1022
1023 fn into_iter(self) -> Self::IntoIter {
1024 self.iter_mut()
1025 }
1026}
1027
1028impl IntoIterator for JsonObject {
1029 type Item = Entry;
1030 type IntoIter = std::vec::IntoIter<Entry>;
1031
1032 fn into_iter(self) -> Self::IntoIter {
1033 self.map.into_iter()
1034 }
1035}
1036
1037impl Extend<Entry> for JsonObject {
1038 fn extend<I: IntoIterator<Item = Entry>>(&mut self, iter: I) {
1039 for entry in iter {
1040 self.push_entry_back(entry);
1041 }
1042 }
1043}
1044
1045impl FromIterator<Entry> for JsonObject {
1046 fn from_iter<I: IntoIterator<Item = Entry>>(iter: I) -> Self {
1047 let mut object = JsonObject::default();
1048 object.extend(iter);
1049 object
1050 }
1051}
1052
1053#[derive(Debug)]
1055pub struct DuplicateEntry<T = Entry>(pub Box<T>, pub Box<T>);
1056
1057impl<T> DuplicateEntry<T> {
1058 pub fn new(a: T, b: T) -> Self {
1059 Self(Box::new(a), Box::new(b))
1060 }
1061}
1062
1063impl fmt::Display for DuplicateEntry {
1064 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1065 write!(f, "duplicate entry `{}`", self.0.0)
1066 }
1067}
1068
1069pub type DuplicateEntryRef<'a> = DuplicateEntry<EntryRef<'a>>;
1070
1071impl std::error::Error for DuplicateEntry {}
1072
1073#[cfg(test)]
1074mod tests {
1075 use crate::lexical::JsonBorrowLexical;
1076
1077 use super::*;
1078
1079 #[test]
1080 fn remove() {
1081 let mut object = JsonObject::new();
1082 object.insert("a", JsonValue::Null);
1083
1084 object.remove("a");
1085 object.remove("a");
1086 }
1087
1088 #[test]
1089 fn unordered_eq1() {
1090 let mut a = JsonObject::new();
1091 a.push_back("a", JsonValue::Null);
1092 a.push_back("b", JsonValue::Null);
1093
1094 let mut b = JsonObject::new();
1095 b.push_back("b", JsonValue::Null);
1096 b.push_back("a", JsonValue::Null);
1097
1098 assert_eq!(a, b);
1099 assert_ne!(a.as_lexical(), b.as_lexical())
1100 }
1101
1102 #[test]
1103 fn unordered_eq2() {
1104 let mut a = JsonObject::new();
1105 a.push_back("a", JsonValue::Null);
1106 a.push_back("a", JsonValue::Null);
1107
1108 let mut b = JsonObject::new();
1109 b.push_back("a", JsonValue::Null);
1110 b.push_back("a", JsonValue::Null);
1111
1112 assert_eq!(a, b);
1113 assert_eq!(a.as_lexical(), b.as_lexical())
1114 }
1115
1116 #[test]
1117 fn insert_front1() {
1118 let mut a = JsonObject::new();
1119 a.push_back("a", JsonValue::Null);
1120 a.push_back("b", JsonValue::Null);
1121 a.push_back("c", JsonValue::Null);
1122 a.shift_insert_front("b", JsonValue::Null);
1123
1124 let mut b = JsonObject::new();
1125 b.push_back("b", JsonValue::Null);
1126 b.push_back("a", JsonValue::Null);
1127 b.push_back("c", JsonValue::Null);
1128
1129 assert_eq!(a, b);
1130 }
1131
1132 #[test]
1133 fn insert_front2() {
1134 let mut a = JsonObject::new();
1135 a.push_back("a", JsonValue::Null);
1136 a.push_back("a", JsonValue::Null);
1137 a.push_back("c", JsonValue::Null);
1138 a.shift_insert_front("a", JsonValue::Null);
1139
1140 let mut b = JsonObject::new();
1141 b.push_back("a", JsonValue::Null);
1142 b.push_back("c", JsonValue::Null);
1143
1144 assert_eq!(a, b);
1145 }
1146
1147 #[test]
1148 fn mapped_entries() {
1149 use crate::JsonParse;
1150 let (json, code_map) = crate::JsonValue::parse_str(
1151 r#"{ "0": [null, null], "1": { "foo": 0, "bar": 1 }, "0": null }"#,
1152 )
1153 .unwrap();
1154 let object = json.into_object().unwrap();
1155
1156 let offsets: Vec<_> = object
1157 .get_mapped_entries(&code_map, 0, "0")
1158 .map(|JsonMapped((key, value), offset)| (offset, key.offset(), value.offset()))
1159 .collect();
1160
1161 assert_eq!(offsets, [(1, 2, 3), (15, 16, 17)]);
1162
1163 let offsets: Vec<_> = object
1164 .get_mapped_entries(&code_map, 0, "1")
1165 .map(|JsonMapped((key, value), offset)| (offset, key.offset(), value.offset()))
1166 .collect();
1167
1168 assert_eq!(offsets, [(6, 7, 8)]);
1169
1170 let offsets: Vec<_> = object
1171 .iter_mapped(&code_map, 0)
1172 .map(|JsonMapped((key, value), offset)| (offset, key.offset(), value.offset()))
1173 .collect();
1174
1175 assert_eq!(offsets, [(1, 2, 3), (6, 7, 8), (15, 16, 17)]);
1176 }
1177}