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
use serde::de::DeserializeOwned;
use serde::Serialize;
use serde_json::{Map, Value};
use std::cmp::Ordering;
use std::mem;

#[macro_use] extern crate serde_derive;

/// Access and mutate nested JSON elements by dotted paths
///
/// The path is composed of keys separated by dots, e.g. `foo.bar.1`.
///
/// 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.
pub trait DotPaths {
    /// Get an item by path, if present.
    ///
    /// JSON `null` becomes `None`, same as unpopulated path.
    ///
    /// # Special keys
    /// Arrays can be indexed by special keys for reading:
    /// - `>` ... last element
    ///
    /// # Panics
    /// - If the path attempts to index into a scalar (e.g. `"foo.bar"` in `{"foo": 123}`)
    /// - If the path uses invalid key in an array or map
    fn dot_get<T>(&self, path: &str) -> Option<T>
    where
        T: DeserializeOwned;

    /// Get an item, or a default value.
    ///
    /// # Special keys
    /// see `dot_get()`
    ///
    /// # Panics
    /// see `dot_get()`
    fn dot_get_or<T>(&self, path: &str, def: T) -> T
    where
        T: DeserializeOwned,
    {
        self.dot_get(path).unwrap_or(def)
    }

    /// Get an item, or a default value using the Default trait
    ///
    /// # Special keys
    /// see `dot_get()`
    ///
    /// # Panics
    /// see `dot_get()`
    fn dot_get_or_default<T>(&self, path: &str) -> T
    where
        T: DeserializeOwned + Default,
    {
        self.dot_get(path).unwrap_or_default()
    }

    /// Get a mutable reference to an item
    ///
    /// # Special keys
    /// see `dot_get()`
    ///
    /// # Panics
    /// see `dot_get()`
    fn dot_get_mut(&mut self, path: &str) -> Option<&mut Value>;

    /// Insert an item by path.
    ///
    /// # Special keys
    /// Arrays can be indexed by special keys:
    /// - `+` or `>` ... append
    /// - `-` or `<` ... prepend
    /// - `>n` ... insert after an index `n`
    /// - `<n` ... insert before an index `n`
    ///
    /// # Panics
    /// see `dot_get()`
    fn dot_set<T>(&mut self, path: &str, value: T)
    where
        T: Serialize;

    /// Replace a value by path with a new value.
    /// The value types do not have to match.
    ///
    /// # Panics
    /// see `dot_get()`
    fn dot_replace<T, U>(&mut self, path: &str, value: T) -> Option<U>
    where
        T: Serialize,
        U: DeserializeOwned;

    /// Get an item using a path, removing it from the store.
    /// If no item was stored under this path, then None is returned.
    ///
    /// # Panics
    /// see `dot_get()`
    fn dot_take<T>(&mut self, path: &str) -> Option<T>
    where
        T: DeserializeOwned;

    /// Remove an item matching a key.
    /// Returns true if any item was removed.
    ///
    /// # Panics
    /// see `dot_get()`
    fn dot_remove(&mut self, path: &str) -> bool {
        self.dot_take::<Value>(path).is_some()
    }
}

/// Split the path string by dot, if present.
///
/// Returns a tuple of (before_dot, after_dot)
fn path_split(path: &str) -> (&str, Option<&str>) {
    let dot = path.find('.');
    match dot {
        None => (path, None),
        Some(pos) => (&path[0..pos], Some(&path[pos + 1..])),
    }
}

impl DotPaths for serde_json::Value {
    fn dot_get<T>(&self, path: &str) -> Option<T>
    where
        T: DeserializeOwned,
    {
        match self {
            Value::Array(vec) => vec.dot_get(path),
            Value::Object(map) => map.dot_get(path),
            Value::Null => None,
            _ => {
                if path.is_empty() {
                    serde_json::from_value(self.to_owned()).ok()
                } else {
                    panic!("Node is not array or object!");
                }
            }
        }
    }

    fn dot_get_mut(&mut self, path: &str) -> Option<&mut Value> {
        match self {
            Value::Array(vec) => vec.dot_get_mut(path),
            Value::Object(map) => map.dot_get_mut(path),
            Value::Null => None,
            _ => {
                if path.is_empty() {
                    Some(self)
                } else {
                    panic!("Node is not array or object!");
                }
            }
        }
    }

    fn dot_set<T>(&mut self, path: &str, value: T)
    where
        T: Serialize,
    {
        match self {
            Value::Array(vec) => {
                vec.dot_set(path, value);
            }
            Value::Object(map) => {
                map.dot_set(path, value);
            }
            Value::Null => {
                mem::replace(self, new_by_path_root(path, value));
            }
            _ => {
                if path.is_empty() {
                    mem::replace(self, serde_json::to_value(value).expect("Serialize error"));
                } else {
                    panic!("Node is not an array, object, or null!");
                }
            }
        }
    }

    fn dot_replace<T, U>(&mut self, path: &str, value: T) -> Option<U>
    where
        T: Serialize,
        U: DeserializeOwned,
    {
        match self {
            Value::Array(vec) => vec.dot_replace(path, value),
            Value::Object(map) => map.dot_replace(path, value),
            Value::Null => {
                self.dot_set(path, value);
                None
            }
            _ => {
                if path.is_empty() {
                    let new = serde_json::to_value(value).expect("Serialize error");
                    let old = mem::replace(self, new);
                    Some(serde_json::from_value(old).expect("Unserialize error"))
                } else {
                    panic!("Node is not an array, object, or null!")
                }
            }
        }
    }

    fn dot_take<T>(&mut self, path: &str) -> Option<T>
    where
        T: DeserializeOwned,
    {
        match self {
            Value::Array(vec) => vec.dot_take(path),
            Value::Object(map) => map.dot_take(path),
            Value::Null => None,
            _ => {
                if path.is_empty() {
                    let old = mem::replace(self, Value::Null);
                    Some(serde_json::from_value(old).expect("Unserialize error"))
                } else {
                    panic!("Node is not an array, object, or null!")
                }
            }
        }
    }
}

/// 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) -> Value
where
    T: Serialize,
{
    if path.is_empty() {
        return serde_json::to_value(value).expect("Serialize error");
    }

    let (sub1, _) = path_split(path);
    if sub1 == "0" || sub1 == "+" || sub1 == "<" || sub1 == ">" {
        // new vec
        let mut new_vec = vec![];
        new_vec.dot_set(path, value);
        Value::Array(new_vec)
    } else {
        // new map
        let mut new_map = Map::new();
        new_map.dot_set(path, value);
        Value::Object(new_map)
    }
}

/// Check if a key is valid to use by dot paths in Value::Object.
/// The key must start with an alpha character or underscore and must not contain period.
fn validate_map_key(key: &str) {
    if key.parse::<usize>().is_ok() {
        panic!("Numeric keys are not allowed in maps: {}", key);
    }

    if !key.starts_with(|p: char| p.is_ascii_alphabetic() || p == '_') || key.contains('.') {
        panic!("Invalid map key: {}", key);
    }
}

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

        if let Some(sub_path) = sub {
            self.get(my)
                .map(|child| child.dot_get(sub_path)) // this produces Option<Option<T>>
                .unwrap_or_default()
        } else {
            self.get(my)
                .map(ToOwned::to_owned)
                .map(serde_json::from_value)
                .transpose() // Option<Result> to Result<Option>
                .unwrap_or_default() // get rid of the result
        }
    }

    fn dot_get_mut(&mut self, path: &str) -> Option<&mut Value> {
        let (my, sub) = path_split(path);
        validate_map_key(my);

        if let Some(sub_path) = sub {
            self.get_mut(my)
                .map(|m| m.get_mut(sub_path))
                .unwrap_or_default()
        } else {
            self.get_mut(my)
        }
    }

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

        if let Some(subpath) = sub {
            if self.contains_key(my) {
                self.get_mut(my).unwrap().dot_set(subpath, value);
            } else {
                self.insert(my.into(), new_by_path_root(subpath, value));
            }
        } else {
            let packed = serde_json::to_value(value).expect("Serialize error");
            self.insert(my.into(), packed);
        }
    }

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

        if let Some(subpath) = sub {
            if self.contains_key(my) {
                self.get_mut(my).unwrap().dot_replace(subpath, value)
            } else {
                self.dot_set(path, value);
                None
            }
        } else {
            let packed = serde_json::to_value(value).expect("Serialize error");
            self.insert(my.to_string(), packed)
                .map(serde_json::from_value)
                .transpose()
                .expect("Unserialize error")
        }
    }

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

        if let Some(subpath) = sub {
            if let Some(item) = self.get_mut(my) {
                item.dot_take(subpath)
            } else {
                None
            }
        } else {
            self.remove(my)
                .map(serde_json::from_value)
                .transpose() // Option<Result> to Result<Option>
                .expect("Unserialize error") // get rid of the result
        }
    }
}

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

        let index: usize = if my == ">" {
            self.len() - 1
        } else {
            my.parse().unwrap()
        };

        if let Some(subpath) = sub {
            self.get(index)
                .map(ToOwned::to_owned)
                .map(|child| child.dot_get(subpath))
                .unwrap_or_default()
        } else {
            self.get(index)
                .map(ToOwned::to_owned)
                .map(serde_json::from_value)
                .transpose()
                .unwrap_or_default()
        }
    }

    fn dot_get_mut(&mut self, path: &str) -> Option<&mut Value> {
        let (my, sub) = path_split(path);

        let my: usize = my
            .parse()
            .unwrap_or_else(|_| panic!("Cannot index an array by \"{}\"", my));

        if let Some(subpath) = sub {
            self.get_mut(my)
                .map(|m: &mut Value| m.get_mut(subpath))
                .unwrap_or_default()
        } else {
            self.get_mut(my)
        }
    }

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

        if my.is_empty() {
            panic!("Cannot index array by empty key");
        }

        let mut insert = false;
        let mut add = 0isize;

        if my.starts_with('<') {
            // "<n" means insert before n
            // "<" means prepend
            insert = true;
            my = &my[1..];
        } else if my.starts_with('>') {
            insert = true;
            my = &my[1..];
            if my.is_empty() {
                // ">" means append
                add = self.len() as isize;
            } else {
                // ">n" means insert after
                add = 1;
            }
        }

        let mut my = match my {
            // append
            "+" => self.len(),
            // prepend
            "-" => {
                insert = true;
                0
            }
            // empty (this happens only if the < or > was removed above)
            "" => 0,
            _ => my
                .parse()
                .unwrap_or_else(|_| panic!("Cannot index an array by \"{}\"", my)),
        };

        my = (my as isize + add) as usize;

        match my.cmp(&self.len()) {
            Ordering::Less => {
                if insert {
                    // insert
                    if let Some(subpath) = sub {
                        self.insert(my, new_by_path_root(subpath, value));
                    } else {
                        self.insert(my, serde_json::to_value(value).expect("Serialize error"));
                    }
                } else {
                    // replace
                    if let Some(subpath) = sub {
                        self[my].dot_set(subpath, value);
                    } else {
                        self[my] = serde_json::to_value(value).expect("Serialize error");
                    }
                }
            }
            Ordering::Equal => {
                if let Some(subpath) = sub {
                    self.push(new_by_path_root(subpath, value))
                } else {
                    self.push(serde_json::to_value(value).expect("Serialize error"))
                }
            }
            Ordering::Greater => {
                panic!("Index out of bounds.");
            }
        }
    }

    fn dot_replace<T, U>(&mut self, path: &str, value: T) -> Option<U>
    where
        T: Serialize,
        U: DeserializeOwned,
    {
        let (my, sub) = path_split(path);
        let index = my
            .parse::<usize>()
            .unwrap_or_else(|_| panic!("Cannot index an array by \"{}\"", my));

        if let Some(subpath) = sub {
            if index < self.len() {
                let a_mut: &mut Value = self.get_mut(index).unwrap();
                a_mut.dot_replace(subpath, value)
            } else {
                // use the append implementation & validations from dot_set
                self.dot_set(path, value);
                None
            }
        } else {
            // No subpath
            if index < self.len() {
                let other = serde_json::to_value(value).expect("Serialize error");
                let old = mem::replace(&mut self[index], other);
                Some(serde_json::from_value(old).expect("Deserialize error"))
            } else {
                // use the append implementation & validations from dot_set
                self.dot_set(my, value);
                None
            }
        }
    }

    fn dot_take<T>(&mut self, path: &str) -> Option<T>
    where
        T: DeserializeOwned,
    {
        let (my, sub) = path_split(path);
        let my = my
            .parse()
            .unwrap_or_else(|_| panic!("Cannot index an array by \"{}\"", my));

        if my >= self.len() {
            return None;
        }

        if let Some(subpath) = sub {
            let value: &mut Value = &mut self[my];
            value.dot_take(subpath)
        } else {
            // bounds are checked above
            serde_json::from_value(self.remove(my)).expect("Deserialize error")
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::DotPaths;
    use serde_json::json;
    use serde_json::Value;
    use serde::{Serialize,Deserialize};

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

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

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

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

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

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

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

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

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

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

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

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

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

        vec.dot_set("0.+.0", "xyz");
        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");
        assert_eq!(json!([[["first"]]]), vec);
        vec.dot_set(">.<.>", "second");
        assert_eq!(json!([[["first"]], [["second"]]]), vec);
    }

    #[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"));
        assert_eq!(Some("second".to_string()), vec.dot_get("0.0.1"));

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

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

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

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

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

        // test Take
        assert_eq!(Some("b".to_string()), vec.dot_take("1"));
        assert_eq!(None, vec.dot_take::<Value>("4"));
        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"));
        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_eq!(true, vec.dot_remove("1"));
        assert_eq!(false, vec.dot_remove("4"));
        assert_eq!(json!(["a", "c"]), vec);

        let mut vec = json!([[["a"], "b"], "c"]);
        assert_eq!(true, vec.dot_remove("0.0.0"));
        assert_eq!(false, vec.dot_remove("0.0.0"));
        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"));

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

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

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

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

    #[test]
    #[should_panic]
    fn set_map_panic_bad_index() {
        let mut vec = json!({});
        vec.dot_set("0", "first");
    }

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

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

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

    #[test]
    fn remove_from_map() {
        let mut vec = json!({"one": "two", "x": "y"});
        assert_eq!(true, vec.dot_remove("one"));
        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"));
        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"));
        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"));
        assert_eq!(
            Some(json!({"bbb": "mm"})),
            vec.dot_replace("x", "betelgeuze")
        );
        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")
        );
        assert_eq!(json!({"one": "two", "x": {"bbb": ["betelgeuze"]}}), vec);
    }

    #[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"
        }));

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

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

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

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

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