Extension for ServiceStack.Text to serialize Tuple as a delimited string. This allows for more human readable values under specific situations while still leveraging the benefits of using a tuple.
Custom tuple serialization currently only applies to the json serializer. It works by assigning custom delegates to JsConfig<T>.SerializeFn and JsConfig<T>.DeSerializeFn.
Configure all tuples in the ExampleCode namespace for all assemblies in the current app domain:
newTupleSerializerConfigurator().WithAssemblies(AppDomain.CurrentDomain.GetAssemblies()).WithNamespaceFilter(ns =>ns.StartsWith("ExampleCode")).Configure();Configure a string as a custom delimiter: (The default delimiter is the dash "-" character)
newTupleSerializerConfigurator().WithAssemblies(AppDomain.CurrentDomain.GetAssemblies()).WithNamespaceFilter(ns =>ns.StartsWith("ExampleCode")).WithDelimiter(":").Configure();Configure explicit tuples: (useful for testing)
newTupleSerializerConfigurator().WithTupleTypes(newList<Type>{typeof(Tuple<string,string>)}).Configure();namespaceExampleCode{publicclassExchangeRate{publicTuple<string,string>CurrencyPair{get;set;}publicdoubleRate{get;set;}}[Route("/exchangerates/{currencypair}","GET")]publicclassExchangeRateRequest:IGetReturn<ExchangeRate>{[ApiMember(Name="currencypair",IsRequired=true,ParameterType="path")]publicTuple<string,string>CurrencyPair{get;set;}}}This will request for the latest EUR-USD exchange rate:
http://host:port/exchangerates/EUR-USD
With ServiceStack.Text.TupleSerializer:
{
"CurrencyPair": "EUR-USD",
"Rate": 1.35
}Without ServiceStack.Text.TupleSerializer:
{
"CurrencyPair": {
"Item1": "EUR",
"Item2": "USD"
},
"Rate": 1.35
}- This implementation does not support nested tuples.
.WithNamespaceFilter()only applies to public tuples found in assemblies passed using.WithAssemblies(). Any tuples explicitly identified.WithTupleTypes()will not be filtered by namespace.- Multiple calls to
.WithTupleTypes()are additive and will not replace previously specified values. - Both
.WithTupleTypes()and.WithAssemblies()may be used at the same time, the results will be combined. Configure()should be called before serializing/deserializing anything withServiceStack.Textor the custom methods may not register correctly withJsConfig.
- Install the NuGet Package
- You can check out the code and run build.bat.
- It will create NuGet packages you can consume in
.\ReleasePackagesor you can directly use the resulting binaries.
- It will create NuGet packages you can consume in
- Build requirements
- .Net 4.0
- Powershell 2.0