Created
May 15, 2015 07:48
-
-
Save Lionhunter3k/928d51b732b0a2fbc3ee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Net; | |
| using System.Web; | |
| using System.Web.Mvc; | |
| using Humanizer; | |
| using Newtonsoft.Json; | |
| using Newtonsoft.Json.Converters; | |
| namespace Mvc | |
| { | |
| public class JSONCustomDateConverter : DateTimeConverterBase | |
| { | |
| private TimeZoneInfo _timeZoneInfo; | |
| private string _dateFormat; | |
| public JSONCustomDateConverter(string dateFormat, TimeZoneInfo timeZoneInfo) | |
| { | |
| _dateFormat = dateFormat; | |
| _timeZoneInfo = timeZoneInfo; | |
| } | |
| public JSONCustomDateConverter(string dateFormat) | |
| { | |
| _dateFormat = dateFormat; | |
| } | |
| public override bool CanConvert(Type objectType) | |
| { | |
| return objectType == typeof(DateTime) || objectType == typeof(Nullable<DateTime>); | |
| } | |
| public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | |
| { | |
| throw new NotImplementedException(); | |
| } | |
| public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | |
| { | |
| if (this._timeZoneInfo != null) | |
| { | |
| writer.WriteValue(TimeZoneInfo.ConvertTimeFromUtc(Convert.ToDateTime(value), _timeZoneInfo).ToString(_dateFormat)); | |
| } | |
| else | |
| { | |
| writer.WriteValue(Convert.ToDateTime(value).ToString(this._dateFormat)); | |
| } | |
| writer.Flush(); | |
| } | |
| } | |
| public class PrettyEnumStrings : StringEnumConverter | |
| { | |
| public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | |
| { | |
| if (value is Enum) | |
| { | |
| writer.WriteValue((value as Enum).Humanize()); | |
| writer.Flush(); | |
| } | |
| else | |
| base.WriteJson(writer, value, serializer); | |
| } | |
| } | |
| public class JsonNetResult : JsonResult | |
| { | |
| public JsonNetResult(object model) | |
| : base() | |
| { | |
| base.Data = model; | |
| base.JsonRequestBehavior = System.Web.Mvc.JsonRequestBehavior.AllowGet; | |
| SerializerSettings = new JsonSerializerSettings() { DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat }; | |
| Formatting = Newtonsoft.Json.Formatting.Indented; | |
| } | |
| public JsonNetResult(object model, bool prettyDateTime = false, bool enumAsString = false, bool? prettyEnums = null) | |
| : base() | |
| { | |
| base.Data = model; | |
| base.JsonRequestBehavior = System.Web.Mvc.JsonRequestBehavior.AllowGet; | |
| if (prettyDateTime) | |
| SerializerSettings = new JsonSerializerSettings() { Converters = new List<JsonConverter>() { new JSONCustomDateConverter("dd.MM.yyyy") } }; | |
| else | |
| SerializerSettings = new JsonSerializerSettings() { DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat }; | |
| Formatting = Newtonsoft.Json.Formatting.Indented; | |
| if (enumAsString) | |
| if (prettyEnums.HasValue && prettyEnums.Value) | |
| this.SerializerSettings.Converters.Add(new PrettyEnumStrings()); | |
| else | |
| this.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter()); | |
| } | |
| public HttpStatusCode? StatusCode { get; set; } | |
| public JsonSerializerSettings SerializerSettings { get; set; } | |
| public Formatting Formatting { get; set; } | |
| public override void ExecuteResult(ControllerContext context) | |
| { | |
| if (context == null) | |
| throw new ArgumentNullException("context"); | |
| HttpResponseBase response = context.HttpContext.Response; | |
| response.ContentType = !string.IsNullOrEmpty(ContentType) | |
| ? ContentType | |
| : "application/json"; | |
| if (ContentEncoding != null) | |
| response.ContentEncoding = ContentEncoding; | |
| if (StatusCode.HasValue) | |
| { | |
| response.StatusCode = (int)StatusCode.Value; | |
| response.TrySkipIisCustomErrors = true; | |
| } | |
| if (Data != null) | |
| { | |
| JsonTextWriter writer = new JsonTextWriter(response.Output) { Formatting = Formatting }; | |
| JsonSerializer serializer = JsonSerializer.Create(SerializerSettings); | |
| serializer.Serialize(writer, Data); | |
| writer.Flush(); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment