Customizing JSON Serialization: Enums as Strings

1,550 views
Skip to first unread message

Corneliu I. Tusnea

unread,
Jan 2, 2012, 6:57:36 AM1/2/12
to servic...@googlegroups.com
Hi,

I'd like to customize my JSON serialization out of a service to export the Enums as string values versus the integer number.

Is this possible?

Kind Regards,
Corneliu

Corneliu I. Tusnea
OneSaas - Cloud Integrations Made Easy
Phone US/International: +1 800 255 OneSaas (+1 800 255 6637)
Phone AU: 1300 OneSaas (1300 663 7227)
Schedule a meeting with me: http://tungle.me/corneliu 

Demis Bellot

unread,
Jan 2, 2012, 1:29:16 PM1/2/12
to servic...@googlegroups.com
Enums with the [Flags] attribute get serialized as numbers, Enums without the attribute are serialized as strings.

Corneliu I. Tusnea

unread,
Jan 3, 2012, 3:22:49 AM1/3/12
to servic...@googlegroups.com
Can I "convince" it to serialize Flags as strings?

"Value1 | Value2 | Value3" ?

Kind Regards,
Corneliu

Corneliu I. Tusnea
OneSaas - Cloud Integrations Made Easy
Phone US/International: +1 800 255 OneSaas (+1 800 255 6637)
Phone AU: 1300 OneSaas (1300 663 7227)
Schedule a meeting with me: http://tungle.me/corneliu 



Demis Bellot

unread,
Jan 3, 2012, 3:39:38 AM1/3/12
to servic...@googlegroups.com
Your current options are:

 - Convert it to a string (and therefore serialize it as you want)
 - Create a duplicate set of enums without the flag attribute (i.e. specific for a DTO)
 - Embed it in a type and customize the De/Serialization with JsConfig<T>.SerializeFn, e.g:
 - Convert it to a struct type and override ToString/Ctor or T.Parse methods:
   http://www.servicestack.net/docs/text-serializers/json-serializer (from title Using Structs to Customize JSON)
Cheers,

TM

unread,
Feb 9, 2012, 8:18:58 AM2/9/12
to ServiceStack .NET Open Source REST Web Services Framework
Hi,

is there a way how I could tell the serializer to output number for a
Nullable<Enum>?

Cheers?

On 3 Jan., 09:39, Demis Bellot <demis.bel...@gmail.com> wrote:
> Your current options are:
>
>  - Convert it to a string (and therefore serialize it as you want)
>  - Create a duplicate set of enums without the flag attribute (i.e.
> specific for a DTO)
>  - Embed it in a type and customize the De/Serialization with
> JsConfig<T>.SerializeFn, e.g:
>
> https://github.com/ServiceStack/ServiceStack.Text/blob/master/src/Ser...
>  - Convert it to a struct type and override ToString/Ctor or T.Parse
> methods:
>    http://www.servicestack.net/docs/text-serializers/json-serializer(from
> title Using Structs to Customize JSON)
>  - Create a fork and change the source code here:
>
> https://github.com/ServiceStack/ServiceStack.Text/blob/master/src/Ser...
>
> Cheers,
>
> On Tue, Jan 3, 2012 at 3:22 AM, Corneliu I. Tusnea <corne...@onesaas.com>wrote:
>
>
>
>
>
>
>
>
>
> > Can I "convince" it to serialize Flags as strings?
>
> > "Value1 | Value2 | Value3" ?
>
> > Kind Regards,
> > Corneliu
>
> > Corneliu I. Tusnea
> > OneSaas - Cloud Integrations Made Easy
> >www.OneSaas.com
> > Phone US/International: *+1 800 255 OneSaas* (+1 800 255 6637)
> > Phone AU: *1300 OneSaas* (1300 663 7227)
> > Mobile: +61 (0) 410 835 593
> > Schedule a meeting with me:http://tungle.me/corneliu
>
> > On Tue, Jan 3, 2012 at 5:29 AM, Demis Bellot <demis.bel...@gmail.com>wrote:
>
> >> Enums with the [Flags] attribute get serialized as numbers, Enums without
> >> the attribute are serialized as strings.
>
> >> On Mon, Jan 2, 2012 at 6:57 AM, Corneliu I. Tusnea <corne...@onesaas.com>wrote:
>
> >>> Hi,
>
> >>> I'd like to customize my JSON serialization out of a service to export
> >>> the Enums as string values versus the integer number.
>
> >>> Is this possible?
>
> >>> Kind Regards,
> >>> Corneliu
>
> >>> Corneliu I. Tusnea
> >>> OneSaas - Cloud Integrations Made Easy
> >>>www.OneSaas.com
> >>> Phone US/International: *+1 800 255 OneSaas* (+1 800 255 6637)
> >>> Phone AU: *1300 OneSaas* (1300 663 7227)

Demis Bellot

unread,
Feb 9, 2012, 11:00:23 AM2/9/12
to servic...@googlegroups.com
You could try: JsConfig<Nullable<MyEnum>>.SerializeFn/ .DeserializeFn 
Reply all
Reply to author
Forward
0 new messages