1

I am using ASP.Net Web API with JSON.Net to serialize. I had to configure the serializer to handle ISO dates properly like this:

var iso = new IsoDateTimeConverter { DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffK" }; GlobalConfiguration.Configuration.Formatters.JsonFormatter .SerializerSettings.Converters.Add(iso); 

This works fine when I am passing my objects down via the WebAPI. My problem, however, is that I have another place where I want to explicitly call the serialization:

@Html.Raw(JsonConvert.SerializeObject(Model)); 

In this case, it doesn't use the configuration I set up. I am aware that I can pass the iso converter into the SerializeObject call, but I prefer to avoid this and get a hold of a configured serialzer for obvious reasons.

Any ideas?

1 Answer 1

3

If you're going to do JSON serialization yourself, you have to pass the settings you want explicitly. There's no way around it. The best I can think of if you want to reuse the same serializer settings is to do something like this:

JsonConvert.SerializeObject(Model, GlobalConfiguration.Configuration.Formatters. JsonFormatter.SerializerSettings) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.