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)
517517 return TypeSerializer . SerializeToString ( obj ) ;
518518 }
519519
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+
520530 public static string ToSafeJsv < T > ( this T obj )
521531 {
522532 return TypeSerializer . HasCircularReferences ( obj )
@@ -533,6 +543,16 @@ public static T FromJsvSpan<T>(this ReadOnlySpan<char> jsv)
533543 {
534544 return TypeSerializer . DeserializeFromSpan < T > ( jsv ) ;
535545 }
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+ }
536556
537557 public static string ToJson < T > ( this T obj )
538558 {
@@ -563,6 +583,16 @@ public static string ToCsv<T>(this T obj)
563583 return CsvSerializer . SerializeToString ( obj ) ;
564584 }
565585
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+
566596 public static T FromCsv < T > ( this string csv )
567597 {
568598 return CsvSerializer . DeserializeFromString < T > ( csv ) ;
Original file line number Diff line number Diff line change @@ -75,6 +75,17 @@ public void TestJsonDataWithJsConfigScope()
7575 AssertObjectJson ( ) ;
7676 }
7777
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+
7889 [ Test ]
7990 public void TestCloneObjectWithJsConfigScope ( )
8091 {
You can’t perform that action at this time.
0 commit comments