Code Monkey home page Code Monkey logo

servicestack.text.enummemberserializer's Introduction

ServiceStack.Text.EnumMemberSerializer

Extension for ServiceStack.Text to allow using EnumMemberAttribute to serialize and deserialize enumerations. This allows you to use more human readable values while still leveraging the benefits of using enumerations.

Custom enumeration serialization currently only applies to the json serializer. It works by assigning custom delegates to JsConfig<T>.SerializeFn and JsConfig<T>.DeSerializeFn.

To maintain compatibility with previous versions of this library and avoid breaking changes, serilaizers for nullable enums are not configured by default. To configure nullable enum serialization, use the WithNullableEnumSerializers() method on the EnumMemberConfigurator.

#Example Configuration

Configure explicit enumerations:

new EnumSerializerConfigurator()
  .WithEnumTypes(new Type[] { typeof(ReturnPolicy) })
  .WithNullableEnumSerializers() //Optional if you want support for ReturnPolicy? 
  .Configure();

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

new EnumSerializerConfigurator()
  .WithAssemblies(AppDomain.CurrentDomain.GetAssemblies())
  .WithNamespaceFilter(ns => ns.StartsWith("ExampleCode"))
  .WithNullableEnumSerializers() //Optional if you want support for nullable enums
  .Configure();

#Example Enumeration

using System.Runtime.Serialization;

namespace ExampleCode
{
  public enum ReturnPolicy
  {
    NotSet = 0,
    [EnumMember(Value = @"90 Days w/Receipt")]
    _90DayswReceipt = 1,
    [EnumMember(Value = @"15% Restocking Fee")]
    _15RestockingFee = 2,
    [EnumMember(Value = @"Exchange Only")]
    ExchangeOnly = 3,
    [EnumMember(Value = @"As-Is")]
    AsIs = 4,
    ...
  }
}

#Example Dto

namespace ExampleCode
{
  public class ProductInfo
  {
    public string ProductName { get; set; }
    public ReturnPolicy ReturnPolicy { get; set; }
    ...
  }
}

#Url Examples

These will search for all product returnable within 90 days with receipt (all of these will work):

http://myhost/products?returnpolicy=90%20Days%20w%2FReceipt
http://myhost/products?returnpolicy=90%20DaYS%20w%2FReceIPt
http://myhost/products?returnpolicy=_90DayswReceipt
http://myhost/products?returnpolicy=1

#Example Response

With ServiceStack.Text.EnumMemberSerializer:

[
   {
     "ProductName": "Hammer",
     "ReturnPolicy": "90 Days w/Receipt"
   },
   {
     "ProductName": "Chisel",
     "ReturnPolicy": "90 Days w/Receipt"
   }
]

Without ServiceStack.Text.EnumMemberSerializer:

[
   {
     "ProductName": "Hammer",
     "ReturnPolicy": "_90DayswReceipt"
   },
   {
     "ProductName": "Chisel",
     "ReturnPolicy": "_90DayswReceipt"
   }
]

#Considerations

  • Only configures public enumerations.
  • .WithNamespaceFilter() only applies to filtering public enums found in the assemblies passed in using .WithAssemblies(). Any enumerations explicitly identified .WithEnumTypes() will not be filtered by namespace. The namespace filter applies to all provided assemblies.
  • Multiple calls to .WithEnumTypes() and .WithNamespaceFilter() will be added and not replace previous specified values.
  • This manipulates the static JsConfig<T>. Other code called later may overwrite the custom serialization/deserialization delegates.
  • Both .WithEnumTypes() 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 be setup correctly in JsConfig
  • Unit Tests pass using ServiceStack v4.

#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

servicestack.text.enummemberserializer's People

Contributors

anthonycarl avatar kevindaviscfc avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.