PainlessHttp icon indicating copy to clipboard operation
PainlessHttp copied to clipboard

Cannot deserialize DateTime with milliseconds

Open DanielSundberg opened this issue 9 years ago • 4 comments

When trying to fetch an object containing a DateTime with a milliseconds part I get an exception:

System.Runtime.Serialization.SerializationException : There was an error deserializing the object of type MyNamespace.MyClass. String was not recognized as a valid DateTime.

DanielSundberg avatar Mar 17 '17 09:03 DanielSundberg

What serializer are you using? The default serializers for XML (XmlSerializer) and JSON (DataContractJsonSerializer) uses serializers from the .NET Framework. If you are sending data as json, I would recommend using PainlessHttp.Serializer.JsonNet that is industry standard.

Hope this helps!

pardahlman avatar Mar 17 '17 11:03 pardahlman

I mostly use this to GET/POST/PUT my .Net objects to a Asp.net Wep API, like:

HttpClient.Post<MyObj>("api/obj", myObjInst);

I just assumed that everything should work out of the box (it mostly does except from the this issue and https://github.com/pardahlman/PainlessHttp/issues/4)

DanielSundberg avatar Mar 17 '17 11:03 DanielSundberg

Understood! The idea with the main library is to provide an easy-to-use client without any external dependencies. Unfortunately, that limits the options when it comes to serialization 😄 That is the reason why I made serializaers pluggable so that you could use JsonNet, ServiceStack, Protobuf or any other serialization technology.

There are some information in the README on how to register serializers!

pardahlman avatar Mar 17 '17 16:03 pardahlman

Stumbled upon this once again. I don't manage to get PainlessHttpClient to request Json from my web API. Could you provide sample code on how to do this? I've tried:

var config = new Configuration {
    BaseUrl = TestEnvironment.Instance.Url,
    Advanced = {
        Serializers = new List<IContentSerializer> { PainlessHttp.Serializers.Defaults.DefaultJson.GetSerializer() },
        WebrequestModifier = request => request.Headers.Add("Accept", "application/json")
    }
};
HttpClient = new HttpClient(config);    

However, this gives me errors:

System.AggregateException : One or more errors occurred.
  ----> System.ArgumentException : The 'Accept' header must be modified using the appropriate property or method.

DanielSundberg avatar May 09 '17 08:05 DanielSundberg