I would like to format output from DateTime when there is no time part as yyyy-MM-dd i.s.o. yyyy-MM-ddT00:00:00 default in my case.
The controller API is :
[HttpGet("date")]
public async Task<DateTime> GetDateAsync(Date)
{
// dummy code
return DateTime.Now.Date;
}
In Swagger the output is seen as yyyy-MM-ddT00:00:00
I suppose this is due to
internal const string DefaultDateFormatString = #"yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK"; in Newtonsoft.Json.JsonSerializerSettings
But I want it to be like yyyy-MM-dd where the time part is not there:
if (value is DateTime d)
{
if (d.TotalSeconds == 0)
{
writer.WriteValue(d.ToString("yyyy-MM-dd"));
}
}
So I would like to know how to customize the serialization under the above condition and then configure that at startup.
This only for data output from the controllers.
I am using ASP.NET 3.1 and using Newtonsoft.Json
Thanks
I created a converter and hooked it up at start-up:
//...
AddNewtonsoftJson(options =>
{
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
options.SerializerSettings.Converters.Add(new JsonDateOnlyConverter());
})
public class JsonDateOnlyConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(DateTime) || objectType == typeof(DateTime?));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var token = JToken.Load(reader);
var t = token.ToString();
if (token.Type == JTokenType.Date )
{
return token.ToObject<DateTime>();
}
if (token.Type == JTokenType.String)
{
if (string.IsNullOrEmpty(t) || string.IsNullOrWhiteSpace(t))
{
return null;
}
if (DateTime.TryParse(t, out var val))
{
return val;
}
}
if (token.Type == JTokenType.Null && (objectType == typeof(DateTime?) || objectType == typeof(DateTime)))
{
return null;
}
throw new JsonSerializationException("Unexpected token type: " + t);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value is DateTime d)
{
if (d.Date.Ticks == d.Ticks)
{
writer.WriteValue(d.ToString("yyyy-MM-dd"));
}
}
}
}
Related
I have a converter like this
class MultiFormatDateConverter : JsonConverter
{
public List<string> DateTimeFormats { get; set; }
public override bool CanConvert(Type objectType)
{
return objectType == typeof(DateTime);
}
public override bool CanWrite
{
get { return false; }
}
public override void WriteJson(JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
{
string dateString = (string)reader.Value;
DateTime date;
foreach (string format in DateTimeFormats)
{
// adjust this as necessary to fit your needs
if (DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
return date;
}
throw new System.Text.Json.JsonException("Unable to parse \"" + dateString + "\" as a date.");
}
}
and here is the configuration
var settings = new JsonSerializerSettings();
settings.DateParseHandling = DateParseHandling.None;
settings.Converters.Add(new MultiFormatDateConverter
{
DateTimeFormats = new List<string> { "yyyyMMddTHHmmssZ", "yyyy-MM-ddTHH:mm","MMMM yyyy","dd/MM/yyyy","dd/MM/yy","MMM-yy","MMM yy"
}
});
and here is how I am calling it:
List<KipReport> rpt730 = JsonConvert.DeserializeObject<List<KipReport>>(responseBody, settings);
This is the JSON and class
[
{
"Name":"Alex",
"MonthWorked":"January 2021",
"LastEdtDate":"16/02/2021",
"LastEditBy":"san"
}
]
class KipReport
{
public string Name { get; set; }
public DateTime? MonthWorked { get; set; }
public DateTime? LastEditDate { get; set; }
}
Mine is a web API and here is the controller which calls the function. Please note it calls the function as Task.Run()
[HttpGet]
public async Task<IActionResult> Get()
{
await Task.Run(()=>_kReport.GetKReports());
return Accepted();
}
When executing it says
16/03/2021 is not a valid date format
Then I used this way for converting than a converter
var settings = new IsoDateTimeConverter { DateTimeFormat = "dd/MM/yyyy" };
Then error is with January 2021 is not a valid date
Does it means, it's not considering the converter??
Since I have a different format for dates I am using a converter.
So for Web API/Task.Run do we need to do anything specific for the Custom converter?
Your properties are of type DateTime? (i.e. nullable value types) so in CanConvert you must check for objectType == typeof(DateTime?) as well as objectType == typeof(DateTime). Then, in Read(), if the incoming objectType is typeof(DateTime?) you should return null in the event of a null JSON token.
The following fixed converter does this and also skips comments:
class MultiFormatDateConverter : JsonConverter
{
public List<string> DateTimeFormats { get; set; } = new ();
public override bool CanConvert(Type objectType) =>
objectType == typeof(DateTime) || objectType == typeof(DateTime?);
public override bool CanWrite => false;
public override void WriteJson(JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) => throw new NotImplementedException();
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
{
if (reader.MoveToContent().TokenType == JsonToken.Null)
return objectType == typeof(DateTime?) ? null : throw new System.Text.Json.JsonException("Unable to parse null as a date.");
else if (reader.TokenType != JsonToken.String)
throw new System.Text.Json.JsonException("Unable to parse token \"" + reader.TokenType + "\" as a date.");
string dateString = (string)reader.Value;
foreach (string format in DateTimeFormats)
{
// adjust this as necessary to fit your needs
if (DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out var date))
return date;
}
throw new System.Text.Json.JsonException("Unable to parse \"" + dateString + "\" as a date.");
}
}
public static partial class JsonExtensions
{
public static JsonReader MoveToContent(this JsonReader reader)
{
while ((reader.TokenType == JsonToken.Comment || reader.TokenType == JsonToken.None) && reader.Read())
;
return reader;
}
}
Notes:
In your JSON you have a property named "LastEdtDate" while the corresponding c# property is LastEditDate. The JSON property name is missing the letter i in Edit and so will not get bound to the c# property. I assume this is a typo in the question, but if not, you will need to add [JsonProperty("LastEdtDate")] to LastEditDate.
Demo fiddle here.
I have a CotractResolver that returns properties for Time part of each DateTime property (so that user can set time separately).
I have a TimeValueProvider that has a SetValue method as follows:
public void SetValue(object target, object value)
{
try
{
var time = value as string;
var originalValue = _propertyInfo.GetValue(target);
if (value == null)
{
_propertyInfo.SetValue(target, originalValue);
}
else if (string.IsNullOrWhiteSpace(time))
{
var originalDateTime = (DateTime?) originalValue ?? SqlDateTime.MinValue.Value;
_propertyInfo.SetValue(target,
new DateTime(originalDateTime.Year, originalDateTime.Month, originalDateTime.Day, 0, 0, 0));
}
else
{
var currentValue = GetCurrentValue(_propertyInfo.GetValue(target));
var convertedDate = TimeSpan.Parse(time, new DateTimeFormatInfo {LongTimePattern = "HH:mm:ss"});
var finalValue = new DateTime(currentValue.Year, currentValue.Month, currentValue.Day,
convertedDate.Hours, convertedDate.Days, convertedDate.Seconds);
_propertyInfo.SetValue(target, finalValue);
}
}
catch (InvalidDataException)
{
throw new ValidationException(new[]
{
new ValidationError
{
ErrorMessage = "Time is not correct",
FieldName = _propertyInfo.Name,
TypeName = _propertyInfo.DeclaringType.FullName
}
});
}
}
The problem is whenever I pass an invalid number as time say for example 99:99 an exception is thrown by TimeSpan.Parse but I am not getting it outside this method thus Json.Net deserializes the object.
I have chekced my code and couldn't find any general exception handling in place that causes such behavior.
Am I missing something about contract resolvers and value providers here ?
UPDATE: here's how I have configured Json.net :
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new EntityContractResolver();
config.Formatters.JsonFormatter.SerializerSettings.ObjectCreationHandling = ObjectCreationHandling.Replace;
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
Your problem is that you are trying to parse the JSON string inside IValueProvider.SetValue(). However, the value provider is only called after the JSON has been deserialized. Its purpose is to set the deserialized value inside the container object. Thus your current SetValue() method never actually does anything, because:
The incoming object value will be a DateTime not a string if deserialization was successful.
The method will not be called at all if the date string was invalid, because an exception will already have been thrown.
What you need to do instead is use a custom JsonConverter to parse the JSON date string and combine it with the existing value. JsonConverter.ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) has an argument existingValue that contains the current value of the property, so this is straightforward:
public class DateTimeConverter : JsonConverter
{
public override bool CanWrite { get { return false; } }
public override bool CanConvert(Type objectType)
{
return objectType == typeof(DateTime) || objectType == typeof(DateTime?);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;
var token = JToken.Load(reader);
if (token.Type == JTokenType.Date)
{
// Json.NET already parsed the date successfully. Return it.
return (DateTime)token;
}
else
{
TimeSpan span;
if (token.Type == JTokenType.TimeSpan)
{
// Not sure this is actually implemented, see
// http://stackoverflow.com/questions/13484540/how-to-parse-a-timespan-value-in-newtonsoft-json/13505910#13505910
span = (TimeSpan)token;
}
else
{
var timeString = (string)token;
if (String.IsNullOrWhiteSpace(timeString))
span = new TimeSpan();
else
{
try
{
span = TimeSpan.Parse(timeString, new DateTimeFormatInfo { LongTimePattern = "HH:mm:ss" });
}
catch (Exception ex)
{
throw new ValidationException(ex.Message);
}
}
}
var currentValue = (DateTime?)existingValue ?? SqlDateTime.MinValue.Value;
// Combine currentValue & TimeSpan and return. REPLACE THIS WITH YOUR OWN LOGIC.
// I don't really know how you want to do this.
return new DateTime(currentValue.Year, currentValue.Month, currentValue.Day) + span;
}
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
Then apply it in your EntityContractResolver as follows:
public class EntityContractResolver : DefaultContractResolver
{
DateTimeConverter converter = null;
DateTimeConverter Converter
{
get
{
if (converter == null)
converter = Interlocked.CompareExchange(ref converter, new DateTimeConverter(), null);
return converter;
}
}
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var jProperty = base.CreateProperty(member, memberSerialization);
if (jProperty.PropertyType == typeof(DateTime) || jProperty.PropertyType == typeof(DateTime?))
{
jProperty.Converter = jProperty.MemberConverter = Converter;
}
return jProperty;
}
}
Sample fiddle.
How could i deserialize json into a List of enum in C#?
I wrote the following code:
//json "types" : [ "hotel", "spa" ]
public enum eType
{
[Description("hotel")]
kHotel,
[Description("spa")]
kSpa
}
public class HType
{
List<eType> m_types;
[JsonProperty("types")]
public List<eType> HTypes {
get
{
return m_types;
}
set
{
// i did this to try and decide in the setter
// what enum value should be for each type
// making use of the Description attribute
// but throws an exception
}
}
}
//other class
var hTypes = JsonConvert.DeserializeObject<HType>(json);
A custom converter may help.
var hType = JsonConvert.DeserializeObject<HType>(
#"{""types"" : [ ""hotel"", ""spa"" ]}",
new MyEnumConverter());
public class HType
{
public List<eType> types { set; get; }
}
public enum eType
{
[Description("hotel")]
kHotel,
[Description("spa")]
kSpa
}
public class MyEnumConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(eType);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var eTypeVal = typeof(eType).GetMembers()
.Where(x => x.GetCustomAttributes(typeof(DescriptionAttribute)).Any())
.FirstOrDefault(x => ((DescriptionAttribute)x.GetCustomAttribute(typeof(DescriptionAttribute))).Description == (string)reader.Value);
if (eTypeVal == null) return Enum.Parse(typeof(eType), (string)reader.Value);
return Enum.Parse(typeof(eType), eTypeVal.Name);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
Here is my version of an enum converter for ANY enum type... it will handle either a numeric value or a string value for the incoming value. As well as nullable vs non-nullable results.
public class MyEnumConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
if (!objectType.IsEnum)
{
var underlyingType = Nullable.GetUnderlyingType(objectType);
if (underlyingType != null && underlyingType.IsEnum)
objectType = underlyingType;
}
return objectType.IsEnum;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (!objectType.IsEnum)
{
var underlyingType = Nullable.GetUnderlyingType(objectType);
if (underlyingType != null && underlyingType.IsEnum)
objectType = underlyingType;
}
var value = reader.Value;
string strValue;
if (value == null || string.IsNullOrWhiteSpace(value.ToString()))
{
if (existingValue == null || Nullable.GetUnderlyingType(existingValue.GetType()) != null)
return null;
strValue = "0";
}
else
strValue = value.ToString();
int intValue;
if (int.TryParse(strValue, out intValue))
return Enum.ToObject(objectType, intValue);
return Enum.Parse(objectType, strValue);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
How could i deserialize json into a List of enum in C#?
I wrote the following code:
//json "types" : [ "hotel", "spa" ]
public enum eType
{
[Description("hotel")]
kHotel,
[Description("spa")]
kSpa
}
public class HType
{
List<eType> m_types;
[JsonProperty("types")]
public List<eType> HTypes {
get
{
return m_types;
}
set
{
// i did this to try and decide in the setter
// what enum value should be for each type
// making use of the Description attribute
// but throws an exception
}
}
}
//other class
var hTypes = JsonConvert.DeserializeObject<HType>(json);
A custom converter may help.
var hType = JsonConvert.DeserializeObject<HType>(
#"{""types"" : [ ""hotel"", ""spa"" ]}",
new MyEnumConverter());
public class HType
{
public List<eType> types { set; get; }
}
public enum eType
{
[Description("hotel")]
kHotel,
[Description("spa")]
kSpa
}
public class MyEnumConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(eType);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var eTypeVal = typeof(eType).GetMembers()
.Where(x => x.GetCustomAttributes(typeof(DescriptionAttribute)).Any())
.FirstOrDefault(x => ((DescriptionAttribute)x.GetCustomAttribute(typeof(DescriptionAttribute))).Description == (string)reader.Value);
if (eTypeVal == null) return Enum.Parse(typeof(eType), (string)reader.Value);
return Enum.Parse(typeof(eType), eTypeVal.Name);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
Here is my version of an enum converter for ANY enum type... it will handle either a numeric value or a string value for the incoming value. As well as nullable vs non-nullable results.
public class MyEnumConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
if (!objectType.IsEnum)
{
var underlyingType = Nullable.GetUnderlyingType(objectType);
if (underlyingType != null && underlyingType.IsEnum)
objectType = underlyingType;
}
return objectType.IsEnum;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (!objectType.IsEnum)
{
var underlyingType = Nullable.GetUnderlyingType(objectType);
if (underlyingType != null && underlyingType.IsEnum)
objectType = underlyingType;
}
var value = reader.Value;
string strValue;
if (value == null || string.IsNullOrWhiteSpace(value.ToString()))
{
if (existingValue == null || Nullable.GetUnderlyingType(existingValue.GetType()) != null)
return null;
strValue = "0";
}
else
strValue = value.ToString();
int intValue;
if (int.TryParse(strValue, out intValue))
return Enum.ToObject(objectType, intValue);
return Enum.Parse(objectType, strValue);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
I have a web api application using asp.net mvc web api that recieve some decimal numbers in viewmodels. I would like to create a custom model binder for decimal type and get it working for all decimals numbers. I have a viewModel like this:
public class ViewModel
{
public decimal Factor { get; set; }
// other properties
}
And the front-end application can send a json with a invalid decimal number like: 457945789654987654897654987.79746579651326549876541326879854
I would like to response with a 400 - Bad Request error and a custom message. I tried create a custom model binder implementing the System.Web.Http.ModelBinding.IModelBinder and registring on the global.asax but does not work. I would like to get it working for all decimals in my code, look what I tried:
public class DecimalValidatorModelBinder : System.Web.Http.ModelBinding.IModelBinder
{
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
var input = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (input != null && !string.IsNullOrEmpty(input.AttemptedValue))
{
if (bindingContext.ModelType == typeof(decimal))
{
decimal result;
if (!decimal.TryParse(input.AttemptedValue, NumberStyles.Number, Thread.CurrentThread.CurrentCulture, out result))
{
actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.BadRequest, ErrorHelper.GetInternalErrorList("Invalid decimal number"));
return false;
}
}
}
return true; //base.BindModel(controllerContext, bindingContext);
}
}
Adding on the Application_Start:
GlobalConfiguration.Configuration.BindParameter(typeof(decimal), new DecimalValidatorModelBinder());
What can I do?
Thank you.
By default Web API reads a complex type from the request body using a media-type formatter. So it doesn't go through a model binder in this case.
for JSON you can create JsonConverter (in case you are sticking by default with JSON.NET:
public class DoubleConverter : JsonConverter
{
public override bool CanWrite
{
get { return false; }
}
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(double) || objectType == typeof(double?));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JToken token = JToken.Load(reader);
if (token.Type == JTokenType.Float || token.Type == JTokenType.Integer)
{
return token.ToObject<double>();
}
if (token.Type == JTokenType.String)
{
// customize this to suit your needs
var wantedSeperator = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
var alternateSeparator = wantedSeperator == "," ? "." : ",";
double actualValue;
if (double.TryParse(token.ToString().Replace(alternateSeparator, wantedSeperator), NumberStyles.Any,
CultureInfo.CurrentCulture, out actualValue))
{
return actualValue;
}
else
{
throw new JsonSerializationException("Unexpected token value: " + token.ToString());
}
}
if (token.Type == JTokenType.Null && objectType == typeof(double?))
{
return null;
}
if (token.Type == JTokenType.Boolean)
{
return token.ToObject<bool>() ? 1 : 0;
}
throw new JsonSerializationException("Unexpected token type: " + token.Type.ToString());
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException("Unnecessary because CanWrite is false. The type will skip the converter.");
}
}