json_dotpath 1.1.0

Dotted path access to nested JSON objects (serde_json::Value)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
use serde::de::DeserializeOwned;
use serde::Serialize;
use serde_json::{Map, Value};
use std::mem;
use thiserror::Error;

#[cfg(test)]
#[macro_use]
extern crate serde_derive;

/// Errors from dot_path methods
#[derive(Debug, Error)]
pub enum Error {
    /// Path hit a value in the JSON object that is not array or map
    /// and could not continue the traversal.
    ///
    /// (e.g. `foo.bar` in `{"foo": 123}`)
    #[error("Unexpected value reached while traversing path")]
    BadPathElement,

    /// Array index out of range
    #[error("Invalid array index: {0}")]
    BadIndex(usize),

    /// Invalid (usually empty) key used in Map or Array.
    /// If the key is valid but out of bounds, `BadIndex` will be used.
    #[error("Invalid key: {0}")]
    InvalidKey(String),

    /// Error serializing or deserializing a value
    #[error("Invalid array or map key")]
    SerdeError(#[from] serde_json::Error),
}

use crate::Error::{BadIndex, BadPathElement, InvalidKey};

pub type Result<T> = std::result::Result<T, Error>;

/// Convert Some(Value::Null) to None.
trait NullToNone<T> {
    fn null_to_none(self) -> Option<T>;
}

impl NullToNone<Value> for Option<Value> {
    fn null_to_none(self) -> Option<Value> {
        match self {
            None | Some(Value::Null) => None,
            Some(v) => Some(v),
        }
    }
}

impl<'a> NullToNone<&'a Value> for Option<&'a Value> {
    fn null_to_none(self) -> Option<&'a Value> {
        match self {
            None | Some(&Value::Null) => None,
            Some(v) => Some(v),
        }
    }
}

/// Access and mutate nested JSON elements by dotted paths
///
/// The path is composed of keys separated by dots, e.g. `foo.bar.1`.
///
/// All symbols in a path may be escaped by backslash (`\`) to have them treated literally,
/// e.g. to access a key containing a period.
///
/// Arrays are indexed by numeric strings or special keys (see `dot_get()` and `dot_set()`).
///
/// This trait is implemented for `serde_json::Value`, specifically the
/// `Map`, `Array`, and `Null` variants. Empty path can also be used to access a scalar.
///
/// Methods on this trait do not panic, errors are passed to the caller.
pub trait DotPaths {
    /// Get an item by path, if present.
    ///
    /// If the element does not exist or is `null`, None is returned.
    /// Accessing array index out of range raises `Err(BadIndex)`.
    ///
    /// The path does not need to reach a leaf node, i.e. it is possible to extract a subtree
    /// of a JSON object this way.
    ///
    /// # Special symbols
    /// - `>` ... last element of an array
    /// - `<` ... first element of an array (same as `0`)
    fn dot_get<T>(&self, path: &str) -> Result<Option<T>>
    where
        T: DeserializeOwned;

    /// Get an item by path, or a default value if it does not exist.
    ///
    /// This method is best suited for JSON objects (`Map`) or nullable fields.
    ///
    /// See `dot_get()` for more details.
    fn dot_get_or<T>(&self, path: &str, def: T) -> Result<T>
    where
        T: DeserializeOwned,
    {
        self.dot_get(path).map(|o| o.unwrap_or(def))
    }

    /// Get an item, or a default value using the Default trait
    ///
    /// This method is best suited for JSON objects (`Map`) or nullable fields.
    ///
    /// See `dot_get()` for more details.
    fn dot_get_or_default<T>(&self, path: &str) -> Result<T>
    where
        T: DeserializeOwned + Default,
    {
        self.dot_get_or(path, T::default())
    }

    /// Check if a value exists under a dotted path.
    /// Returns error if the path is invalid, a string key was used to index into an array.
    ///
    /// # Special symbols
    /// - `>` ... last element of an array
    /// - `<` ... first element of an array (same as `0`)
    fn dot_has_checked(&self, path: &str) -> Result<bool>;

    /// Check if a value exists under a dotted path.
    /// Returns false also when the path is invalid.
    ///
    /// Use `dot_has_checked` if you want to distinguish non-existent values from path errors.
    ///
    /// # Special symbols
    /// - `>` ... last element of an array
    /// - `<` ... first element of an array (same as `0`)
    fn dot_has(&self, path: &str) -> bool {
        self.dot_has_checked(path)
            .unwrap_or_default()
    }

    /// Get a mutable reference to an item
    ///
    /// If the path does not exist but a value on the path can be created (i.e. because the path
    /// reaches `null`, array or object), a `null` value is inserted in that location (creating
    /// its parent nodes as needed) and a mutable reference to this new `null` node is returned.
    ///
    /// The path does not need to reach a leaf node, i.e. it is possible to extract a subtree
    /// of a JSON object this way.
    ///
    /// # Special keys
    /// - `>` ... last element of an array
    /// - `<` ... first element of an array (same as `0`)
    fn dot_get_mut(&mut self, path: &str) -> Result<&mut Value>;

    /// Insert an item by path. The original value is dropped, if any.
    ///
    /// # Special symbols
    /// Arrays can be modified using special keys in the path:
    /// - `+` or `>>` ... append
    /// - `-` or `<<` ... prepend
    /// - `>n` ... insert after an index `n`
    /// - `<n` ... insert before an index `n`
    /// - `>` ... last element of an array
    /// - `<` ... first element of an array (same as `0`)
    fn dot_set<T>(&mut self, path: &str, value: T) -> Result<()>
    where
        T: Serialize;

    /// Replace a value by path with a new value.
    /// The value types do not have to match.
    ///
    /// Returns `Ok(None)` if the path was previously empty or `null`.
    ///
    /// # Special keys
    /// - `>` ... last element of an array
    /// - `<` ... first element of an array (same as `0`)
    fn dot_replace<NEW, OLD>(&mut self, path: &str, value: NEW) -> Result<Option<OLD>>
    where
        NEW: Serialize,
        OLD: DeserializeOwned;

    /// Get an item using a path, removing it from the object.
    ///
    /// Value becomes `null` when taken by an empty path, map entry is removed,
    /// and array item is extracted, shifting the remainder forward.
    ///
    /// Returns `Ok(None)` if the path was previously empty or `null`.
    ///
    /// # Special keys
    /// - `>` ... last element of an array
    /// - `<` ... first element of an array (same as `0`)
    fn dot_take<T>(&mut self, path: &str) -> Result<Option<T>>
    where
        T: DeserializeOwned;

    /// Remove and drop an item matching a key.
    /// Returns true if any item was removed.
    ///
    /// # Special keys
    /// - `>` ... last element of an array
    /// - `<` ... first element of an array (same as `0`)
    fn dot_remove(&mut self, path: &str) -> Result<()> {
        let _ = self.dot_take::<Value>(path)?; // Original value is dropped
        Ok(())
    }
}

/// Split the path string by dot, if present.
///
/// Returns a tuple of (before_dot, after_dot), removing escapes
fn path_split(path: &str) -> (String, Option<&str>) {
    let mut buf = String::new();
    let mut escaped = false;
    for (n, c) in path.char_indices() {
        match c {
            _ if escaped => {
                buf.push(c);
                escaped = false;
            }
            '\\' => {
                escaped = true;
            }
            '.' => {
                return (buf, Some(&path[n + 1..]));
            }
            _ => {
                buf.push(c);
            }
        }
    }

    // trailing slash is discarded
    (buf, None)
}

impl DotPaths for serde_json::Value {
    fn dot_get<T>(&self, path: &str) -> Result<Option<T>>
    where
        T: DeserializeOwned,
    {
        match self {
            Value::Array(vec) => vec.dot_get(path),
            Value::Object(map) => map.dot_get(path),
            Value::Null => Ok(None),
            _ => {
                if path.is_empty() {
                    // Get the whole value.
                    // We know it's not null - checked above
                    Ok(Some(serde_json::from_value(self.to_owned())?))
                } else {
                    // Path continues, but we can't traverse into a scalar
                    Err(BadPathElement)
                }
            }
        }
    }

    fn dot_has_checked(&self, path: &str) -> Result<bool> {
        match self {
            Value::Array(vec) => vec.dot_has_checked(path),
            Value::Object(map) => map.dot_has_checked(path),
            Value::Null => Ok(false),
            _ => {
                if path.is_empty() {
                    Ok(true)
                } else {
                    // Path continues, but we can't traverse into a scalar
                    Ok(false)
                }
            }
        }
    }

    fn dot_get_mut(&mut self, path: &str) -> Result<&mut Value> {
        match self {
            Value::Array(vec) => vec.dot_get_mut(path),
            Value::Object(map) => map.dot_get_mut(path),
            _ => {
                if path.is_empty() {
                    Ok(self)
                } else {
                    if self.is_null() {
                        // Spawn parents
                        self.dot_set(path, Value::Null)?;
                        // Now it will succeed
                        return self.dot_get_mut(path);
                    }

                    // Path continues, but we can't traverse into a scalar
                    Err(BadPathElement)
                }
            }
        }
    }

    fn dot_replace<NEW, OLD>(&mut self, path: &str, value: NEW) -> Result<Option<OLD>>
    where
        NEW: Serialize,
        OLD: DeserializeOwned,
    {
        match self {
            Value::Array(vec) => vec.dot_replace(path, value),
            Value::Object(map) => map.dot_replace(path, value),
            Value::Null => {
                // spawn new
                *self = new_by_path_root(path, value)?;
                Ok(None)
            }
            _ => {
                if path.is_empty() {
                    let new = serde_json::to_value(value)?;
                    let old = mem::replace(self, new);
                    Ok(serde_json::from_value(old)?)
                } else {
                    // Path continues, but we can't traverse into a scalar
                    Err(BadPathElement)
                }
            }
        }
    }

    fn dot_take<T>(&mut self, path: &str) -> Result<Option<T>>
    where
        T: DeserializeOwned,
    {
        match self {
            Value::Array(vec) => vec.dot_take(path),
            Value::Object(map) => map.dot_take(path),
            Value::Null => Ok(None),
            _ => {
                if path.is_empty() {
                    // This won't happen with array or object, they really remove the value.
                    // Value is replaced with NULL only when dot_take() is called
                    // with an empty path.
                    let old = mem::replace(self, Value::Null); // we know it's not null, checked above
                    Ok(Some(serde_json::from_value(old)?))
                } else {
                    // Path continues, but we can't traverse into a scalar
                    Err(BadPathElement)
                }
            }
        }
    }

    fn dot_set<T>(&mut self, path: &str, value: T) -> Result<()>
    where
        T: Serialize,
    {
        match self {
            // Special case for Vec, which implements additional path symbols
            Value::Array(a) => a.dot_set(path, value),
            Value::Object(m) => m.dot_set(path, value),
            _ => {
                let _ = self.dot_replace::<T, Value>(path, value)?; // Original value is dropped
                Ok(())
            }
        }
    }
}

/// Create a Value::Object or Value::Array based on a nested path.
///
/// Builds the parent path to a non-existent key in set-type operations.
fn new_by_path_root<T>(path: &str, value: T) -> Result<Value>
where
    T: Serialize,
{
    if path.is_empty() {
        return Ok(serde_json::to_value(value)?);
    }

    let escaped = path.starts_with('\\');
    let (sub1, _) = path_split(path);
    if !escaped && ["0", "+", "-", "<", ">", "<<", ">>"].contains(&sub1.as_str()) {
        // new vec
        let mut new_vec = vec![];
        new_vec.dot_set(path, value)?;
        Ok(Value::Array(new_vec))
    } else {
        // new map
        let mut new_map = Map::new();
        new_map.dot_set(path, value)?;
        Ok(Value::Object(new_map))
    }
}

impl DotPaths for serde_json::Map<String, serde_json::Value> {
    fn dot_get<T>(&self, path: &str) -> Result<Option<T>>
    where
        T: DeserializeOwned,
    {
        let (my, sub) = path_split(path);

        if my.is_empty() {
            return Err(InvalidKey(my));
        }

        if let Some(sub_path) = sub {
            match self.get(&my).null_to_none() {
                None => Ok(None),
                Some(child) => child.dot_get(sub_path),
            }
        } else {
            match self.get(&my).null_to_none() {
                None => Ok(None),
                Some(m) => Ok(Some(serde_json::from_value::<T>(m.to_owned())?)),
            }
        }
    }

    fn dot_has_checked(&self, path: &str) -> Result<bool> {
        let (my, sub) = path_split(path);

        if my.is_empty() {
            return Err(InvalidKey(my));
        }

        if let Some(sub_path) = sub {
            match self.get(&my).null_to_none() {
                None => Ok(false),
                Some(child) => child.dot_has_checked(sub_path),
            }
        } else {
            match self.get(&my).null_to_none() {
                None => Ok(false),
                Some(_) => Ok(true),
            }
        }
    }

    #[allow(clippy::collapsible_if)]
    fn dot_get_mut(&mut self, path: &str) -> Result<&mut Value> {
        let (my, sub) = path_split(path);

        if my.is_empty() {
            return Err(InvalidKey(my));
        }

        if let Some(sub_path) = sub {
            if self.contains_key(&my) {
                self.get_mut(&my)
                    .unwrap() // OK
                    .dot_get_mut(sub_path)
            } else {
                // create a subtree
                self.insert(my.clone(), new_by_path_root(sub_path, Value::Null)?);

                // get reference to the new Null
                self.get_mut(&my)
                    .unwrap() // OK, we just inserted it
                    .dot_get_mut(sub_path)
            }
        } else {
            if self.contains_key(&my) {
                Ok(self.get_mut(&my).unwrap())
            } else {
                // create a null value
                self.insert(my.clone(), Value::Null);
                Ok(self.get_mut(&my).unwrap())
            }
        }
    }

    fn dot_set<T>(&mut self, path: &str, value: T) -> Result<()> where
        T: Serialize {
        let (my, sub) = path_split(path);

        if my.is_empty() {
            return Err(InvalidKey(my));
        }

        if let Some(subpath) = sub {
            if self.contains_key(&my) {
                self.get_mut(&my).unwrap().dot_set(subpath, value)
            } else {
                // Build new subpath
                let _ = self.insert(my, new_by_path_root(subpath, value)?); // always returns None here
                Ok(())
            }
        } else {
            let packed = serde_json::to_value(value)?;
            self.insert(my, packed);
            Ok(())
        }
    }

    fn dot_replace<NEW, OLD>(&mut self, path: &str, value: NEW) -> Result<Option<OLD>>
    where
        NEW: Serialize,
        OLD: DeserializeOwned,
    {
        let (my, sub) = path_split(path);

        if my.is_empty() {
            return Err(InvalidKey(my));
        }

        if let Some(subpath) = sub {
            if self.contains_key(&my) {
                self.get_mut(&my).unwrap().dot_replace(subpath, value)
            } else {
                // Build new subpath
                let _ = self.insert(my, new_by_path_root(subpath, value)?); // always returns None here
                Ok(None)
            }
        } else {
            let packed = serde_json::to_value(value)?;
            match self.insert(my, packed).null_to_none() {
                None => Ok(None),
                Some(old) => Ok(serde_json::from_value(old)?),
            }
        }
    }

    fn dot_take<T>(&mut self, path: &str) -> Result<Option<T>>
    where
        T: DeserializeOwned,
    {
        let (my, sub) = path_split(path);

        if my.is_empty() {
            return Err(InvalidKey(my));
        }

        if let Some(subpath) = sub {
            if let Some(item) = self.get_mut(&my) {
                item.dot_take(subpath)
            } else {
                // no such element
                Ok(None)
            }
        } else {
            match self.remove(&my).null_to_none() {
                None => Ok(None),
                Some(old) => Ok(serde_json::from_value(old)?),
            }
        }
    }
}

impl DotPaths for Vec<serde_json::Value> {
    fn dot_get<T>(&self, path: &str) -> Result<Option<T>>
    where
        T: DeserializeOwned,
    {
        let (my, sub) = path_split(path);

        if my.is_empty() {
            return Err(InvalidKey(my));
        }

        if self.is_empty() {
            return Ok(None);
        }

        let index: usize = match my.as_str() {
            ">" => self.len() - 1, // non-empty checked above
            "<" => 0,
            _ => my.parse().map_err(|_| {
                InvalidKey(my)
            })?,
        };

        if index >= self.len() {
            return Err(BadIndex(index));
        }

        if let Some(subpath) = sub {
            match self.get(index).null_to_none() {
                None => Ok(None),
                Some(child) => child.dot_get(subpath),
            }
        } else {
            match self.get(index).null_to_none() {
                None => Ok(None),
                Some(value) => Ok(serde_json::from_value(value.to_owned())?),
            }
        }
    }

    fn dot_has_checked(&self, path: &str) -> Result<bool> {
        let (my, sub) = path_split(path);

        if my.is_empty() {
            return Err(InvalidKey(my));
        }

        if self.is_empty() {
            return Ok(false);
        }

        let index: usize = match my.as_str() {
            ">" => self.len() - 1, // non-empty checked above
            "<" => 0,
            _ => my.parse().map_err(|_| {
                InvalidKey(my)
            })?,
        };

        if index >= self.len() {
            return Ok(false);
        }

        if let Some(subpath) = sub {
            match self.get(index).null_to_none() {
                None => Ok(false),
                Some(child) => child.dot_has_checked(subpath),
            }
        } else {
            match self.get(index).null_to_none() {
                // null is reported as unset
                None => Ok(false),
                Some(_) => Ok(true),
            }
        }
    }

    #[allow(clippy::collapsible_if)]
    fn dot_get_mut(&mut self, path: &str) -> Result<&mut Value> {
        let (my, sub) = path_split(path);

        if my.is_empty() {
            return Err(InvalidKey(my));
        }

        let index: usize = match my.as_str() {
            ">" => {
                if self.is_empty() {
                    0
                } else {
                    self.len() - 1
                }
            }
            "<" => 0,
            _ => my.parse().map_err(|_| {
                InvalidKey(my)
            })?,
        };

        if index > self.len() {
            return Err(BadIndex(index));
        }

        if let Some(subpath) = sub {
            if index < self.len() {
                self.get_mut(index).unwrap().dot_get_mut(subpath)
            } else {
                // create a subtree
                self.push(new_by_path_root(subpath, Value::Null)?);

                // get reference to the new Null
                self.get_mut(index)
                    .unwrap() // we just inserted it
                    .dot_get_mut(subpath)
            }
        } else {
            if index < self.len() {
                Ok(self.get_mut(index).unwrap())
            } else {
                // create a subtree
                self.push(Value::Null);

                // get reference to the new Null
                Ok(self.get_mut(index).unwrap()) // unwrap is safe now
            }
        }
    }

    #[allow(clippy::collapsible_if)]
    fn dot_set<T>(&mut self, path: &str, value: T) -> Result<()>
    where
        T: Serialize,
    {
        // implemented separately from replace because of the special index handling
        let (my_s, sub) = path_split(path);

        if my_s.is_empty() {
            return Err(InvalidKey(my_s));
        }

        let my = my_s.as_str(); // so it can be cropped

        let mut insert = false;

        let index = match my {
            "<" => 0, // first
            ">" => {
                if self.is_empty() {
                    0
                } else {
                    self.len() - 1
                }
            }
            "-" | "<<" => {
                // prepend
                insert = true;
                0
            }
            "+" | ">>" => {
                // append
                self.len()
            }
            _ if my.starts_with('>') => {
                // insert after
                insert = true;
                (&my[1..]).parse::<usize>()
                    .map_err(|_| {
                        InvalidKey(my_s)
                    })? + 1
            }
            _ if my.starts_with('<') => {
                // insert before
                insert = true;
                (&my[1..]).parse::<usize>()
                    .map_err(|_| {
                        InvalidKey(my_s)
                    })?
            }
            _ => my.parse::<usize>()
                .map_err(|_| {
                    InvalidKey(my_s)
                })?,
        };

        if index > self.len() {
            // equal is OK, that means append. More is an error
            return Err(BadIndex(index));
        }

        if let Some(subpath) = sub {
            if index < self.len() {
                if insert {
                    self.insert(index, new_by_path_root(subpath, value)?);
                } else {
                    // replace
                    self[index].dot_set(subpath, value)?;
                }
            } else {
                // Append
                self.push(new_by_path_root(subpath, value)?);
            }
        } else {
            if index < self.len() {
                if insert {
                    self.insert(index, serde_json::to_value(value)?);
                } else {
                    // replace
                    self[index] = serde_json::to_value(value)?; // old value is dropped
                }
            } else {
                // Append
                self.push(serde_json::to_value(value)?);
            }
        }

        Ok(())
    }

    fn dot_replace<T, U>(&mut self, path: &str, value: T) -> Result<Option<U>>
    where
        T: Serialize,
        U: DeserializeOwned,
    {
        let (my, sub) = path_split(path);

        if my.is_empty() {
            return Err(InvalidKey(my));
        }

        let index: usize = match my.as_str() {
            ">" => {
                if self.is_empty() {
                    0
                } else {
                    self.len() - 1 // last element
                }
            }
            "<" => 0,
            _ => my.parse().map_err(|_| {
                InvalidKey(my)
            })?,
        };

        if index >= self.len() {
            // appending is not supported in replace
            return Err(BadIndex(index));
        }

        if let Some(subpath) = sub {
            self.get_mut(index)
                .unwrap() // Bounds checked above
                .dot_replace(subpath, value)
        } else {
            // No subpath
            let new = serde_json::to_value(value)?;
            let old = mem::replace(&mut self[index], new);
            if old.is_null() {
                Ok(None)
            } else {
                Ok(serde_json::from_value(old)?)
            }
        }
    }

    fn dot_take<T>(&mut self, path: &str) -> Result<Option<T>>
    where
        T: DeserializeOwned,
    {
        let (my, sub) = path_split(path);

        if my.is_empty() {
            return Err(InvalidKey(my));
        }

        let index: usize = match my.as_str() {
            ">" => {
                if self.is_empty() {
                    0
                } else {
                    self.len() - 1
                }
            }
            "<" => 0,
            _ => my.parse().map_err(|_| {
                InvalidKey(my)
            })?,
        };

        if index >= self.len() {
            return Err(BadIndex(index));
        }

        if let Some(subpath) = sub {
            self[index].dot_take(subpath)
        } else {
            // bounds are checked above
            Ok(serde_json::from_value(self.remove(index))?)
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::DotPaths;
    use serde_json::json;
    use serde_json::Value;

    #[test]
    fn get_scalar_with_empty_path() {
        let value = Value::String("Hello".to_string());
        assert_eq!(Some("Hello".to_string()), value.dot_get("").unwrap());
    }

    #[test]
    fn cant_get_scalar_with_path() {
        let value = Value::String("Hello".to_string());
        assert!(value.dot_get::<Value>("1.2.3").is_err());
    }

    #[test]
    fn set_null() {
        let mut item = Value::Null;
        item.dot_set("", "foo").unwrap();
        assert_eq!(Value::String("foo".into()), item);
    }

    #[test]
    fn replace_null() {
        let mut item = Value::Null;
        assert_eq!(None, item.dot_replace::<_, Value>("", "foo").unwrap());
        assert_eq!(Value::String("foo".into()), item);
    }

    #[test]
    fn take_null() {
        let mut item = Value::Null;
        assert_eq!(None, item.dot_take::<Value>("").unwrap());
        assert_eq!(Value::Null, item);

        let mut item = Value::Bool(true);
        assert_eq!(Some(true), item.dot_take::<bool>("").unwrap());
        assert_eq!(Value::Null, item);
    }

    #[test]
    fn set_vec() {
        let mut vec = Value::Array(vec![]);
        vec.dot_set("0", "first").unwrap();
        vec.dot_set("0", "second").unwrap(); // this replaces it
        vec.dot_set("1", "third").unwrap();
        vec.dot_set("+", "append").unwrap();
        vec.dot_set(">>", "append2").unwrap();
        vec.dot_set("-", "prepend").unwrap();
        vec.dot_set("<<", "prepend2").unwrap();
        vec.dot_set("<0", "prepend3").unwrap();
        vec.dot_set(">1", "insert after 1").unwrap();
        vec.dot_set(">0", "after0").unwrap();
        vec.dot_set("<2", "before2").unwrap();
        assert_eq!(
            json!([
                "prepend3",
                "after0",
                "before2",
                "prepend2",
                "insert after 1",
                "prepend",
                "second",
                "third",
                "append",
                "append2"
            ]),
            vec
        );
    }

    #[test]
    fn set_vec_err_bad_index() {
        let mut vec = Value::Array(vec![]);
        assert!(vec.dot_set("1", "first").is_err());
    }

    #[test]
    fn set_vec_err_index_not_numeric() {
        let mut vec = Value::Array(vec![]);
        assert!(vec.dot_set("abc", "first").is_err());
    }

    #[test]
    fn set_vec_spawn() {
        let mut vec = Value::Array(vec![]);

        vec.dot_set("0.0.0", "first").unwrap();
        vec.dot_set("+", "append").unwrap();
        vec.dot_set("<1", "in between").unwrap();
        assert_eq!(json!([[["first"]], "in between", "append"]), vec);

        vec.dot_set("0.0.+", "second").unwrap();
        assert_eq!(json!([[["first", "second"]], "in between", "append"]), vec);

        vec.dot_set("0.+", "mmm").unwrap();
        assert_eq!(
            json!([[["first", "second"], "mmm"], "in between", "append"]),
            vec
        );

        vec.dot_set("0.+.0", "xyz").unwrap();
        assert_eq!(
            json!([
                [["first", "second"], "mmm", ["xyz"]],
                "in between",
                "append"
            ]),
            vec
        );

        // append and prepend can also spawn a new array
        let mut vec = Value::Array(vec![]);
        vec.dot_set(">>.>>.>>", "first").unwrap();
        assert_eq!(json!([[["first"]]]), vec);
        vec.dot_set(">>.<<.>>", "second").unwrap();
        assert_eq!(json!([[["first"]], [["second"]]]), vec);
    }

    #[test]
    fn array_append1() {
        let mut test_array = Value::Array(vec![]);
        test_array.dot_set(">>", Value::String(String::from("Go to class"))).unwrap();
        test_array.dot_set(">>", Value::String(String::from("Fish"))).unwrap();
        assert_eq!(json!(["Go to class","Fish"]), test_array);
    }

    #[test]
    fn array_append2() {
        let mut test_array = Value::Array(vec![]);
        test_array.dot_set("+", Value::String(String::from("Go to class"))).unwrap();
        test_array.dot_set("+", Value::String(String::from("Fish"))).unwrap();
        assert_eq!(json!(["Go to class","Fish"]), test_array);
    }

    #[test]
    fn array_append_in_object1() {
        let mut test_inner_array = Value::Null;

        test_inner_array.dot_set("todos.+", Value::String(String::from("Go to class"))).unwrap();
        assert_eq!(json!({"todos" : ["Go to class"] }), test_inner_array);

        test_inner_array.dot_set("todos.+", Value::String(String::from("Fish"))).unwrap();
        assert_eq!(json!({"todos" : ["Go to class","Fish"] }), test_inner_array);
    }

    #[test]
    fn array_append_in_object2() {
        let mut test_inner_array = Value::Null;
        test_inner_array.dot_set("name", Value::String(String::from("Google"))).unwrap();
        assert_eq!(json!({"name" : "Google"}), test_inner_array);

        test_inner_array.dot_set("todos.+", Value::String(String::from("Go to class"))).unwrap();
        assert_eq!(json!({"name" : "Google", "todos" : ["Go to class"] }), test_inner_array);

        test_inner_array.dot_set("todos.+", Value::String(String::from("Fish"))).unwrap();
        assert_eq!(json!({"name" : "Google", "todos" : ["Go to class","Fish"] }), test_inner_array);
    }

    #[test]
    fn get_vec() {
        let vec = json!([
            [["first", "second"], "mmm", ["xyz"]],
            "in between",
            "append"
        ]);
        assert_eq!(Some("first".to_string()), vec.dot_get("0.0.0").unwrap());
        assert_eq!(Some("second".to_string()), vec.dot_get("0.0.1").unwrap());

        // this one doesn't exist
        assert!(vec.dot_get::<String>("0.0.3").is_err());

        // get last
        assert_eq!(Some(json!(["xyz"])), vec.dot_get("0.>").unwrap());

        // retrieve a Value
        assert_eq!(
            Some(json!([["first", "second"], "mmm", ["xyz"]])),
            vec.dot_get("0").unwrap()
        );
        assert_eq!(
            Some(json!(["first", "second"])),
            vec.dot_get("0.0").unwrap()
        );
    }

    #[test]
    fn get_escapes() {
        let vec = json!({
            "foo.bar": {
                "\\slashes\\\\ya.yy": 123,
                "#hash": {
                    "foobar": "<aaa>"
                }
            }
        });
        assert_eq!(
            Some(123),
            vec.dot_get("foo\\.bar.\\\\slashes\\\\\\\\ya\\.yy").unwrap()
        );
        assert_eq!(
            Some("<aaa>".to_string()),
            vec.dot_get("foo\\.bar.\\#hash.foobar").unwrap()
        );
    }

    #[test]
    fn get_vec_err_index_scalar() {
        let vec = json!([
            [["first", "second"], "mmm", ["xyz"]],
            "in between",
            "append"
        ]);
        assert!(vec.dot_get::<Value>("0.0.1.4").is_err());
    }

    #[test]
    fn take_from_vec() {
        let mut vec = json!(["a", "b", "c"]);

        // test Take
        assert_eq!(Some("b".to_string()), vec.dot_take("1").unwrap());
        assert!(vec.dot_take::<Value>("4").is_err());
        assert_eq!(json!(["a", "c"]), vec);

        let mut vec = json!([[["a"], "b"], "c"]);
        assert_eq!(Some("a".to_string()), vec.dot_take("0.0.0").unwrap());
        assert_eq!(json!([[[], "b"], "c"]), vec); // empty vec is left in place
    }

    #[test]
    fn remove_from_vec() {
        let mut vec = json!(["a", "b", "c"]);

        assert!(vec.dot_remove("1").is_ok());
        assert!(vec.dot_remove("4").is_err()); // out of bounds
        assert_eq!(json!(["a", "c"]), vec);

        let mut vec = json!([[["a"], "b"], "c"]);
        assert!(vec.dot_remove("0.0.0").is_ok());
        assert!(vec.dot_remove("0.0.0").is_err()); // doesn't exist
        assert_eq!(json!([[[], "b"], "c"]), vec); // empty vec is left in place
    }

    #[test]
    fn replace_in_vec() {
        let mut vec = json!(["a", "b", "c"]);

        assert_eq!(Some("b".to_string()), vec.dot_replace("1", "BBB").unwrap());

        let mut vec = json!([[["a"], "b"], "c"]);
        assert_eq!(
            Some("a".to_string()),
            vec.dot_replace("0.0.0", "AAA").unwrap()
        );
        assert_eq!(json!([[["AAA"], "b"], "c"]), vec);
    }

    #[test]
    fn set_map() {
        let mut vec = json!({});
        vec.dot_set("foo", "bar").unwrap();
        assert_eq!(json!({"foo": "bar"}), vec);

        let mut vec = json!({});
        vec.dot_set("foo.bar.baz", "binx").unwrap();
        assert_eq!(json!({"foo": {"bar": {"baz": "binx"}}}), vec);

        vec.dot_set("foo.bar.abc.0", "aaa").unwrap();
        assert_eq!(json!({"foo": {"bar": {"baz": "binx","abc": ["aaa"]}}}), vec);
    }

    #[test]
    fn set_map_err_bad_index() {
        let mut map = json!({});
        assert!(map.dot_set("0", "first").is_ok());
        assert_eq!(json!({"0": "first"}), map);

        assert_eq!(Some("first".to_string()), map.dot_get("0").unwrap());
    }

    #[test]
    fn get_map() {
        let vec = json!({"one": "two"});
        assert_eq!(Some("two".to_string()), vec.dot_get("one").unwrap());

        let vec = json!({"one": {"two": "three"}});
        assert_eq!(Some("three".to_string()), vec.dot_get("one.two").unwrap());

        let vec = json!({"one": "two"});
        assert_eq!(None, vec.dot_get::<Value>("xxx").unwrap());
    }

    #[test]
    fn map_escaped_keys() {
        let mut map = json!({});
        map.dot_set("\\0.\\1", 123).unwrap();
        assert_eq!(json!({"0": {"1": 123}}), map);
        map.dot_set("\\0.\\2", 456).unwrap();

        assert_eq!(Some(123), map.dot_get("\\0.\\1").unwrap());
        assert_eq!(Some(123), map.dot_take("\\0.\\1").unwrap());
        assert_eq!(json!({"0": {"2": 456}}), map);
        assert!(map.dot_remove("\\0.\\2").is_ok());

        map.dot_set("\\0.\\\\2", 456).unwrap();
        assert_eq!(json!({"0": {"\\2": 456}}), map);
    }

    #[test]
    fn remove_from_map() {
        let mut vec = json!({"one": "two", "x": "y"});
        assert!(vec.dot_remove("one").is_ok());
        assert_eq!(json!({"x": "y"}), vec);
    }

    #[test]
    fn take_from_map() {
        let mut vec = json!({"one": "two", "x": "y"});
        assert_eq!(Some("two".to_string()), vec.dot_take("one").unwrap());
        assert_eq!(json!({"x": "y"}), vec);
    }

    #[test]
    fn replace_in_map() {
        let mut vec = json!({"one": "two", "x": "y"});
        assert_eq!(
            Some("two".to_string()),
            vec.dot_replace("one", "fff").unwrap()
        );
        assert_eq!(json!({"one": "fff", "x": "y"}), vec);

        // replace value for string
        let mut vec = json!({"one": "two", "x": {"bbb": "y"}});
        assert_eq!(
            Some("y".to_string()),
            vec.dot_replace("x.bbb", "mm").unwrap()
        );
        assert_eq!(
            Some(json!({"bbb": "mm"})),
            vec.dot_replace("x", "betelgeuze").unwrap()
        );
        assert_eq!(json!({"one": "two", "x": "betelgeuze"}), vec);

        // nested replace
        let mut vec = json!({"one": "two", "x": {"bbb": ["y"]}});
        assert_eq!(
            Some("y".to_string()),
            vec.dot_replace("x.bbb.0", "betelgeuze").unwrap()
        );
        assert_eq!(json!({"one": "two", "x": {"bbb": ["betelgeuze"]}}), vec);
    }

    #[test]
    fn value_get_mut() {
        // Borrow Null as mutable
        let mut obj = Value::Null;
        let m = obj.dot_get_mut("").unwrap();
        *m = Value::from(123);
        assert_eq!(Value::from(123), obj);

        // Create a parents path
        let mut obj = Value::Null;
        let _ = obj.dot_get_mut("foo.0").unwrap();
        assert_eq!(json!({ "foo": [null] }), obj);
    }

    #[test]
    fn map_get_mut() {
        // Spawn empty element
        let mut obj = serde_json::Map::<String, Value>::new();
        let _ = obj.dot_get_mut("foo").unwrap();
        assert_eq!(json!({ "foo": null }), Value::Object(obj));

        // Spawn a subtree
        let mut obj = serde_json::Map::<String, Value>::new();
        let m = obj.dot_get_mut("foo.bar.baz").unwrap();
        m.dot_set("dog", "cat").unwrap();

        assert_eq!(
            json!({"foo": {"bar": {"baz": {"dog": "cat"}}}}),
            Value::Object(obj)
        );
    }

    #[test]
    fn vec_get_mut() {
        // Spawn empty element
        let mut obj = Vec::<Value>::new();
        let _ = obj.dot_get_mut(">").unwrap();
        assert_eq!(json!([null]), Value::Array(obj));

        // Spawn a subtree
        let mut obj = Vec::<Value>::new();
        let m = obj.dot_get_mut("0.foo.bar").unwrap();
        m.dot_set("dog", "cat").unwrap();

        assert_eq!(json!([{"foo": {"bar": {"dog": "cat"}}}]), Value::Array(obj));
    }

    #[test]
    fn has() {
        let value = json!({
            "one": "two",
            "x": [1, 2, {"foo": 123}]
        });
        assert!(value.dot_has("one"));
        assert!(!value.dot_has("two"));
        assert!(value.dot_has("x"));
        assert!(value.dot_has("x.0"));
        assert!(value.dot_has("x.<"));
        assert!(value.dot_has("x.>"));
        assert!(value.dot_has("x.>.foo"));
        assert!(!value.dot_has("x.banana"));
        assert!(!value.dot_has("x.>.foo.bar"));
        assert!(value.dot_has_checked("x.banana").is_err());

        if let Ok(false) = value.dot_has_checked("x.9999") {
            //
        } else {
            panic!("dot_has_checked failed");
        }
    }

    #[test]
    fn stamps() {
        let mut stamps = Value::Null;
        // null will become Value::Array(vec![])

        #[derive(Serialize, Deserialize, PartialEq, Default)]
        struct Stamp {
            country: String,
            year: u32,
            color: String,
            #[serde(rename = "face value")]
            face_value: String,
        };

        stamps
            .dot_set(
                "0",
                json!({
                    "country": "British Mauritius",
                    "year": 1847,
                    "color": "orange",
                    "face value": "1 penny"
                }),
            )
            .unwrap();

        // append
        stamps
            .dot_set(
                "+",
                Stamp {
                    country: "British Mauritius".to_string(),
                    year: 1847,
                    color: "blue".to_string(),
                    face_value: "2 pence".to_string(),
                },
            )
            .unwrap();

        assert_eq!(
            "orange",
            stamps.dot_get::<String>("0.color").unwrap().unwrap()
        );
        assert_eq!(
            "blue",
            stamps.dot_get::<String>("1.color").unwrap().unwrap()
        );

        assert_eq!(1847, stamps.dot_get::<Stamp>("1").unwrap().unwrap().year);

        // Remove the first stamp's "face value" attribute
        assert_eq!(
            Some("1 penny".to_string()),
            stamps.dot_get("0.face value").unwrap()
        );
        stamps.dot_remove("0.face value").unwrap();
        assert_eq!(
            Option::<Value>::None,
            stamps.dot_get("0.face value").unwrap()
        );

        // change the second stamp's year
        let old_year: u32 = stamps.dot_replace("1.year", 1850).unwrap().unwrap();
        assert_eq!(1847, old_year);
        assert_eq!(1850, stamps.dot_get::<u32>("1.year").unwrap().unwrap());
    }
}