@@ -15,6 +15,7 @@ pub use crate::json::ser::to_string;
15
15
use crate :: types:: { LimboText , OwnedValue , TextSubtype } ;
16
16
use indexmap:: IndexMap ;
17
17
use jsonb:: Error as JsonbError ;
18
+ use ser:: to_string_pretty;
18
19
use serde:: { Deserialize , Serialize } ;
19
20
20
21
#[ derive( Serialize , Deserialize , Debug , PartialEq , Clone ) ]
@@ -31,7 +32,7 @@ pub enum Val {
31
32
Object ( Vec < ( String , Val ) > ) ,
32
33
}
33
34
34
- pub fn get_json ( json_value : & OwnedValue ) -> crate :: Result < OwnedValue > {
35
+ pub fn get_json ( json_value : & OwnedValue , indent : Option < & str > ) -> crate :: Result < OwnedValue > {
35
36
match json_value {
36
37
OwnedValue :: Text ( ref t) => {
37
38
// optimization: once we know the subtype is a valid JSON, we do not have
@@ -41,7 +42,10 @@ pub fn get_json(json_value: &OwnedValue) -> crate::Result<OwnedValue> {
41
42
}
42
43
43
44
let json_val = get_json_value ( json_value) ?;
44
- let json = to_string ( & json_val) . unwrap ( ) ;
45
+ let json = match indent {
46
+ Some ( indent) => to_string_pretty ( & json_val, indent) . unwrap ( ) ,
47
+ None => to_string ( & json_val) . unwrap ( ) ,
48
+ } ;
45
49
46
50
Ok ( OwnedValue :: Text ( LimboText :: json ( Rc :: new ( json) ) ) )
47
51
}
@@ -57,7 +61,10 @@ pub fn get_json(json_value: &OwnedValue) -> crate::Result<OwnedValue> {
57
61
OwnedValue :: Null => Ok ( OwnedValue :: Null ) ,
58
62
_ => {
59
63
let json_val = get_json_value ( json_value) ?;
60
- let json = to_string ( & json_val) . unwrap ( ) ;
64
+ let json = match indent {
65
+ Some ( indent) => to_string_pretty ( & json_val, indent) . unwrap ( ) ,
66
+ None => to_string ( & json_val) . unwrap ( ) ,
67
+ } ;
61
68
62
69
Ok ( OwnedValue :: Text ( LimboText :: json ( Rc :: new ( json) ) ) )
63
70
}
@@ -536,7 +543,7 @@ mod tests {
536
543
#[ test]
537
544
fn test_get_json_valid_json5 ( ) {
538
545
let input = OwnedValue :: build_text ( Rc :: new ( "{ key: 'value' }" . to_string ( ) ) ) ;
539
- let result = get_json ( & input) . unwrap ( ) ;
546
+ let result = get_json ( & input, None ) . unwrap ( ) ;
540
547
if let OwnedValue :: Text ( result_str) = result {
541
548
assert ! ( result_str. value. contains( "\" key\" :\" value\" " ) ) ;
542
549
assert_eq ! ( result_str. subtype, TextSubtype :: Json ) ;
@@ -548,7 +555,7 @@ mod tests {
548
555
#[ test]
549
556
fn test_get_json_valid_json5_double_single_quotes ( ) {
550
557
let input = OwnedValue :: build_text ( Rc :: new ( "{ key: ''value'' }" . to_string ( ) ) ) ;
551
- let result = get_json ( & input) . unwrap ( ) ;
558
+ let result = get_json ( & input, None ) . unwrap ( ) ;
552
559
if let OwnedValue :: Text ( result_str) = result {
553
560
assert ! ( result_str. value. contains( "\" key\" :\" value\" " ) ) ;
554
561
assert_eq ! ( result_str. subtype, TextSubtype :: Json ) ;
@@ -560,7 +567,7 @@ mod tests {
560
567
#[ test]
561
568
fn test_get_json_valid_json5_infinity ( ) {
562
569
let input = OwnedValue :: build_text ( Rc :: new ( "{ \" key\" : Infinity }" . to_string ( ) ) ) ;
563
- let result = get_json ( & input) . unwrap ( ) ;
570
+ let result = get_json ( & input, None ) . unwrap ( ) ;
564
571
if let OwnedValue :: Text ( result_str) = result {
565
572
assert ! ( result_str. value. contains( "{\" key\" :9e999}" ) ) ;
566
573
assert_eq ! ( result_str. subtype, TextSubtype :: Json ) ;
@@ -572,7 +579,7 @@ mod tests {
572
579
#[ test]
573
580
fn test_get_json_valid_json5_negative_infinity ( ) {
574
581
let input = OwnedValue :: build_text ( Rc :: new ( "{ \" key\" : -Infinity }" . to_string ( ) ) ) ;
575
- let result = get_json ( & input) . unwrap ( ) ;
582
+ let result = get_json ( & input, None ) . unwrap ( ) ;
576
583
if let OwnedValue :: Text ( result_str) = result {
577
584
assert ! ( result_str. value. contains( "{\" key\" :-9e999}" ) ) ;
578
585
assert_eq ! ( result_str. subtype, TextSubtype :: Json ) ;
@@ -584,7 +591,7 @@ mod tests {
584
591
#[ test]
585
592
fn test_get_json_valid_json5_nan ( ) {
586
593
let input = OwnedValue :: build_text ( Rc :: new ( "{ \" key\" : NaN }" . to_string ( ) ) ) ;
587
- let result = get_json ( & input) . unwrap ( ) ;
594
+ let result = get_json ( & input, None ) . unwrap ( ) ;
588
595
if let OwnedValue :: Text ( result_str) = result {
589
596
assert ! ( result_str. value. contains( "{\" key\" :null}" ) ) ;
590
597
assert_eq ! ( result_str. subtype, TextSubtype :: Json ) ;
@@ -596,7 +603,7 @@ mod tests {
596
603
#[ test]
597
604
fn test_get_json_invalid_json5 ( ) {
598
605
let input = OwnedValue :: build_text ( Rc :: new ( "{ key: value }" . to_string ( ) ) ) ;
599
- let result = get_json ( & input) ;
606
+ let result = get_json ( & input, None ) ;
600
607
match result {
601
608
Ok ( _) => panic ! ( "Expected error for malformed JSON" ) ,
602
609
Err ( e) => assert ! ( e. to_string( ) . contains( "malformed JSON" ) ) ,
@@ -606,7 +613,7 @@ mod tests {
606
613
#[ test]
607
614
fn test_get_json_valid_jsonb ( ) {
608
615
let input = OwnedValue :: build_text ( Rc :: new ( "{\" key\" :\" value\" }" . to_string ( ) ) ) ;
609
- let result = get_json ( & input) . unwrap ( ) ;
616
+ let result = get_json ( & input, None ) . unwrap ( ) ;
610
617
if let OwnedValue :: Text ( result_str) = result {
611
618
assert ! ( result_str. value. contains( "\" key\" :\" value\" " ) ) ;
612
619
assert_eq ! ( result_str. subtype, TextSubtype :: Json ) ;
@@ -618,7 +625,7 @@ mod tests {
618
625
#[ test]
619
626
fn test_get_json_invalid_jsonb ( ) {
620
627
let input = OwnedValue :: build_text ( Rc :: new ( "{key:\" value\" " . to_string ( ) ) ) ;
621
- let result = get_json ( & input) ;
628
+ let result = get_json ( & input, None ) ;
622
629
match result {
623
630
Ok ( _) => panic ! ( "Expected error for malformed JSON" ) ,
624
631
Err ( e) => assert ! ( e. to_string( ) . contains( "malformed JSON" ) ) ,
@@ -629,7 +636,7 @@ mod tests {
629
636
fn test_get_json_blob_valid_jsonb ( ) {
630
637
let binary_json = b"\x40 \0 \0 \x01 \x10 \0 \0 \x03 \x10 \0 \0 \x03 \x61 \x73 \x64 \x61 \x64 \x66 " . to_vec ( ) ;
631
638
let input = OwnedValue :: Blob ( Rc :: new ( binary_json) ) ;
632
- let result = get_json ( & input) . unwrap ( ) ;
639
+ let result = get_json ( & input, None ) . unwrap ( ) ;
633
640
if let OwnedValue :: Text ( result_str) = result {
634
641
assert ! ( result_str. value. contains( "\" asd\" :\" adf\" " ) ) ;
635
642
assert_eq ! ( result_str. subtype, TextSubtype :: Json ) ;
@@ -642,7 +649,7 @@ mod tests {
642
649
fn test_get_json_blob_invalid_jsonb ( ) {
643
650
let binary_json: Vec < u8 > = vec ! [ 0xA2 , 0x62 , 0x6B , 0x31 , 0x62 , 0x76 ] ; // Incomplete binary JSON
644
651
let input = OwnedValue :: Blob ( Rc :: new ( binary_json) ) ;
645
- let result = get_json ( & input) ;
652
+ let result = get_json ( & input, None ) ;
646
653
match result {
647
654
Ok ( _) => panic ! ( "Expected error for malformed JSON" ) ,
648
655
Err ( e) => assert ! ( e. to_string( ) . contains( "malformed JSON" ) ) ,
@@ -652,7 +659,7 @@ mod tests {
652
659
#[ test]
653
660
fn test_get_json_non_text ( ) {
654
661
let input = OwnedValue :: Null ;
655
- let result = get_json ( & input) . unwrap ( ) ;
662
+ let result = get_json ( & input, None ) . unwrap ( ) ;
656
663
if let OwnedValue :: Null = result {
657
664
// Test passed
658
665
} else {
@@ -809,7 +816,7 @@ mod tests {
809
816
#[ test]
810
817
fn test_json_array_length_simple_json_subtype ( ) {
811
818
let input = OwnedValue :: build_text ( Rc :: new ( "[1,2,3]" . to_string ( ) ) ) ;
812
- let wrapped = get_json ( & input) . unwrap ( ) ;
819
+ let wrapped = get_json ( & input, None ) . unwrap ( ) ;
813
820
let result = json_array_length ( & wrapped, None ) . unwrap ( ) ;
814
821
815
822
if let OwnedValue :: Integer ( res) = result {
0 commit comments