i got a problem while i try to deserialize a DateTimeOffset from a json. I saw a lot of questions here, but no one seems to work. I got from Json this dateTime : 05/04/2019 02:39:33 PM GMT and i want to keep the offset to zero. After the deserialization, by the way, i got my object with same exact time(In this case 02:39:33 PM) but with my time zone ( +02:00). I tried these two workaround, without success:
First of all, i tried to setup setting to my deserializer:
JsonSerializerSettings serializerSettings = new JsonSerializerSettings
{
DateFormatHandling = DateFormatHandling.IsoDateFormat,
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
DateParseHandling = DateParseHandling.DateTimeOffset,
DateFormatString = "dd/MM/yyyy hh:mm:ss tt 'GMT'"
};
I tried this converter too:
class DateFormatConverter : IsoDateTimeConverter
{
public DateFormatConverter(string format)
{
DateTimeFormat = format;
DateTimeStyles = System.Globalization.DateTimeStyles.AssumeUniversal;
}
So, i expected this reseult:
05/04/2019 02:39:33 PM +00:00
thanks to all that will answer me!
Maybe change DateParseHandling.DateTimeOffset to DateParseHandling.None?
I managed to got the expected result by change DateParseHandling = DateParseHandling.None in the Setting of serializer.
Related
DateTimeStamp : 2022-04-29 15:19:41.350 after serialization 2022-04-29T22:19:41.35Z instead of 2022-04-29T22:19:41.350Z
I have fixed this using below code :
public static void ServiceJsonSettings(HttpConfiguration config, IServiceMetadata serviceMetadata)
{
config.Formatters.Insert(0, new JsonNewtonsoftJilFallbackFormatter());
IsoDateTimeConverter dateConverter = new IsoDateTimeConverter
{
DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fff'Z'",
DateTimeStyles = DateTimeStyles.AdjustToUniversal
};
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(dateConverter);
}
Now for large object serialization i am using Jil Serializer configured as below:
public static readonly Options JilOptions = new Options(excludeNulls: true, includeInherited: true,
serializationNameFormat: SerializationNameFormat.CamelCase, dateFormat: DateTimeFormat.ISO8601);
Using both Serializer Newtonsoft and JIL the output I am getting expected result : 2022-04-29T22:19:41.350Z.
But after testing it i found JIL serializer Serializer is trimming the zeros in millisecond part if it ends with 00 for ex: DateTimeStamp : 2022-04-29 15:19:41.300 serialized to 2022-04-29T22:19:41.3Z. On the other hand newtonsoft serializer produce expected result 2022-04-29T22:19:41.300Z.
Is there any way to provide dateFormat as "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fff'Z'" in JilOptions ?
I'm trying to get Newtonsoft to convert a string to a DateTime. This is the string:
13/02/2019 05:13
I try to tell Newtonsoft how to do this by using one of these:
var dateTimeConverter = new IsoDateTimeConverter { DateTimeFormat = "dd/MM/yyyy HH:mm" };
var converters = new List<JsonConverter>() { dateTimeConverter };
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
Converters = converters
};
or
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
DateFormatString = "dd'/'MM'/'yyyy' 'HH':'mm"
};
and of course it will just go right ahead and tell me that's fine and then blow up as it tries to process the second day of the thirteenth month.
I need this to work globally. How do I get Newtonsoft to do this?
This was in WebAPI and the problem was I wasn't setting the same Json.Net that WebAPI was using.
I fixed it by putting this in Application_Start of global.asax.cs:
var dateTimeConverter = new IsoDateTimeConverter { DateTimeFormat = "dd/MM/yyyy HH:mm" };
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add(dateTimeConverter);
you can convert a string to a datetime like this :
string iString = "2005-05-05 22:12 PM";
DateTime oDate = DateTime.ParseExact(iString, "yyyy-MM-dd HH:mm tt",null);
MessageBox.Show(oDate.ToString());
i hope to help you.. :/
I am using Web API2 to communicate with mobile.
Datetime in response was always containing T eg. [2018-09-26T01:30:37.967] which is UTC. To remove it I converted DateTime zone to iso like this
IsoDateTimeConverter converter = new IsoDateTimeConverter
{
DateTimeStyles = DateTimeStyles.AdjustToUniversal,
DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss"
};
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(converter);
Now another problem appeared that Input parameter does not take DateTime without T.
Previously it was working [2018-09-26 01:30:37.967] but not it stopped
I tried with this [2018-09-26T01:30:37.967] it worked. but I don't want it.
Web API should work with [2018-09-26T01:30:37.967] both for incoming and outgoing.
Waiting for help
With little change it worked. just changed yyyy-MM-dd HH:mm:ss to yyyy-MM-dd HH:mm:ss.fff
IsoDateTimeConverter converter = new IsoDateTimeConverter
{
DateTimeStyles = DateTimeStyles.AdjustToUniversal,
DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff"
};
I serialize objects having DateTime properties, like this: JsonConvert.SerializeObject(my_object), then on another machine I deserialize: result = JsonConvert.DeserializeObject<Result>(r);. And some dates are wrong. I've checked through fiddler that when date in json looks like this Date=2014-11-29T18:41:41.1672899 then it's deserialized correctly, but every 10th or so entry looks like Date=2014-11-29T18:55:39.1175417+00:00 (note the +00:00) and then it's deserialized as date shifted by two hours. Why is this and how to fix this?
You can explicitly set DateTimeZoneHandling to DateTimeZoneHandling.Utc on JsonSerializerSettings:
Result result = JsonConvert.DeserializeObject<Result>(r, new JsonSerializerSettings
{
DateTimeZoneHandling = DateTimeZoneHandling.Utc
});
I've tried deserializing a Json string containing DateTime.MinValue in every conceivable way, but when the set method gets called on my object. The date always gets changed from -01-01-01- to -01-01-02-.
The Json being parsed clearly contains
"inception_date": "0001-01-01T00:00:00+00:00"
I then call JsonConvert on it:
return JsonConvert.DeserializeObject<T>(json, deserializerSettings);
Where T is a basic struct that contains a property: DateTime inception_date { get; set; } property. The deserializer settings are as follows:
deserializerSettings = new JsonSerializerSettings()
{
DateFormatHandling = DateFormatHandling.IsoDateFormat,
DateParseHandling = Newtonsoft.Json.DateParseHandling.None,
DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc
};
Yet somewhere in the bowels of Newtonsoft.Json.dll, the above time gets converted to the following jObject.
"inception_date": "0001-01-02T00:00:00Z"
I have no way of stepping into their code, but by the damage is done before my code sees another call. ReadJson in the JsonConverter calls serializer.Populate(jObject.CreateReader(), target); where target is an instance of my class T, and jObject has somehow been rendered with the above incorrect date.
Can anyone figure out why this is happening or how I can prevent it? The jObject seems to be getting created in a way that is ignoring my serializer settings, which explicitly say not to screw with the date string (DateParseHandling.None).
I've taken screen shots to illustrate exactly where Newtonsoft's JsonConvert method seems to have lost a vital configuration value.
As you can see, this is the point in the code where I call JsonConvert:
The dateParseHandling value is set to None, and that's how it should be for this to work.
At this next step, I've jumped a few internal Newtonsoft calls and landed in a generic implementation of the JsonConverter which I borrowed from an accepted reference implementation to be able to see what was going on. The JsonReader passed in suddenly has lost that dateParseHandling value:
Because of this value being switched back to DateTime - the internal workings of the jObject try to represent this as an internal localized DateTime, which underflows because my timezone is negative, and we're already representing the minimum DateTime value, resulting in the conversion back to UTC adding a full day.
Try:
deserializerSettings = new JsonSerializerSettings()
{
DateFormatHandling = DateFormatHandling.IsoDateFormat,
DateParseHandling = Newtonsoft.Json.DateParseHandling.DateTimeOffset
}
This resulted in me getting 1/1/0001 12:00:00 AM instead of 1/2/0001 12:00:00 AM
Here is my test code (written in LINQPad)
void Main()
{
var deserializerSettings = new JsonSerializerSettings()
{
DateFormatHandling = DateFormatHandling.IsoDateFormat,
DateParseHandling = Newtonsoft.Json.DateParseHandling.None,
DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc
};
var json = "{\"inception_date\": \"0001-01-01T00:00:00+00:00\"}";
var parsedObj = JsonConvert.DeserializeObject<TestClass>(json, deserializerSettings);
Console.WriteLine(parsedObj);
deserializerSettings = new JsonSerializerSettings()
{
DateFormatHandling = DateFormatHandling.IsoDateFormat,
DateParseHandling = Newtonsoft.Json.DateParseHandling.DateTimeOffset
};
parsedObj = JsonConvert.DeserializeObject<TestClass>(json, deserializerSettings);
Console.WriteLine(parsedObj);
}
public class TestClass
{
public DateTime inception_date {get;set;}
}
Outputs: