Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Extension for ServiceStack.Text to serialize Tuple as a delimited string.
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
src
tools
.gitignore
LICENSE
LICENSE_ServiceStack
MajorMinorVersion.txt
README.md
build.bat build tools
default.ps1

README.md

ServiceStack.Text.TupleSerializer

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.

Examples

Host Configuration

Configure all tuples in the ExampleCode namespace for all assemblies in the current app domain:

new TupleSerializerConfigurator()
  .WithAssemblies(AppDomain.CurrentDomain.GetAssemblies())
  .WithNamespaceFilter(ns => ns.StartsWith("ExampleCode"))
  .Configure();

Configure a string as a custom delimiter: (The default delimiter is the dash "-" character)

new TupleSerializerConfigurator()
  .WithAssemblies(AppDomain.CurrentDomain.GetAssemblies())
  .WithNamespaceFilter(ns => ns.StartsWith("ExampleCode"))
  .WithDelimiter(":")
  .Configure();

Configure explicit tuples: (useful for testing)

new TupleSerializerConfigurator()
   .WithTupleTypes(new List<Type>{typeof(Tuple<string, string>)})
   .Configure();

Service Integration

Dtos

namespace ExampleCode
{
    public class ExchangeRate
    {
        public Tuple<string, string> CurrencyPair { get; set; }
        public double Rate { get; set; }
    }

    [Route("/exchangerates/{currencypair}", "GET")]
    public class ExchangeRateRequest : IGetReturn<ExchangeRate>
    {
        [ApiMember(Name = "currencypair", IsRequired = true, ParameterType = "path")]
        public Tuple<string, string> CurrencyPair { get; set; }
    }
}

Request URL

This will request for the latest EUR-USD exchange rate:

http://host:port/exchangerates/EUR-USD

Response Body

With ServiceStack.Text.TupleSerializer:

{
  "CurrencyPair": "EUR-USD",
  "Rate": 1.35
}

Without ServiceStack.Text.TupleSerializer:

{
  "CurrencyPair": {
    "Item1": "EUR",
    "Item2": "USD"
  },
  "Rate": 1.35
}

Notes

  • 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 with ServiceStack.Text or the custom methods may not register correctly with JsConfig.

Using the Code

  • Install the NuGet Package
  • You can check out the code and run build.bat.
    • It will create NuGet packages you can consume in .\ReleasePackages or you can directly use the resulting binaries.
  • Build requirements
    • .Net 4.0
    • Powershell 2.0
Something went wrong with that request. Please try again.