This repository was archived by the owner on Dec 24, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed
tests/ServiceStack.Text.Tests Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -517,6 +517,16 @@ public static string ToJsv<T>(this T obj)
517
517
return TypeSerializer . SerializeToString ( obj ) ;
518
518
}
519
519
520
+ public static string ToJsv < T > ( this T obj , Action < Config > configure )
521
+ {
522
+ var config = new Config ( ) ;
523
+ configure ( config ) ;
524
+ using ( JsConfig . With ( config ) )
525
+ {
526
+ return ToJsv ( obj ) ;
527
+ }
528
+ }
529
+
520
530
public static string ToSafeJsv < T > ( this T obj )
521
531
{
522
532
return TypeSerializer . HasCircularReferences ( obj )
@@ -533,6 +543,16 @@ public static T FromJsvSpan<T>(this ReadOnlySpan<char> jsv)
533
543
{
534
544
return TypeSerializer . DeserializeFromSpan < T > ( jsv ) ;
535
545
}
546
+
547
+ public static string ToJson < T > ( this T obj , Action < Config > configure )
548
+ {
549
+ var config = new Config ( ) ;
550
+ configure ( config ) ;
551
+ using ( JsConfig . With ( config ) )
552
+ {
553
+ return ToJson ( obj ) ;
554
+ }
555
+ }
536
556
537
557
public static string ToJson < T > ( this T obj )
538
558
{
@@ -563,6 +583,16 @@ public static string ToCsv<T>(this T obj)
563
583
return CsvSerializer . SerializeToString ( obj ) ;
564
584
}
565
585
586
+ public static string ToCsv < T > ( this T obj , Action < Config > configure )
587
+ {
588
+ var config = new Config ( ) ;
589
+ configure ( config ) ;
590
+ using ( JsConfig . With ( config ) )
591
+ {
592
+ return ToCsv ( obj ) ;
593
+ }
594
+ }
595
+
566
596
public static T FromCsv < T > ( this string csv )
567
597
{
568
598
return CsvSerializer . DeserializeFromString < T > ( csv ) ;
Original file line number Diff line number Diff line change @@ -75,6 +75,17 @@ public void TestJsonDataWithJsConfigScope()
75
75
AssertObjectJson ( ) ;
76
76
}
77
77
78
+ [ Test ]
79
+ public void TestJsonDataWithJsConfigScope_ext ( )
80
+ {
81
+ Assert . That ( CreateObject ( ) . ToJson ( config => config . TextCase = TextCase . SnakeCase ) ,
82
+ Is . EqualTo ( "{\" id\" :1,\" root_id\" :100,\" display_name\" :\" Test object\" }" ) ) ;
83
+ Assert . That ( CreateObject ( ) . ToJson ( config => config . TextCase = TextCase . CamelCase ) ,
84
+ Is . EqualTo ( "{\" id\" :1,\" rootId\" :100,\" displayName\" :\" Test object\" }" ) ) ;
85
+ Assert . That ( CreateObject ( ) . ToJson ( config => config . TextCase = TextCase . PascalCase ) ,
86
+ Is . EqualTo ( "{\" Id\" :1,\" RootId\" :100,\" DisplayName\" :\" Test object\" }" ) ) ;
87
+ }
88
+
78
89
[ Test ]
79
90
public void TestCloneObjectWithJsConfigScope ( )
80
91
{
You can’t perform that action at this time.
0 commit comments