tl24 / jsonexserializer

Automatically exported from code.google.com/p/jsonexserializer
1 stars 1 forks source link

Serializing DateTimes with custom converter #59

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create custom class for serializing DateTimes
2. Register the TypeConverter:
serializer.Config.RegisterTypeConverter(typeof(DateTime), new 
MyDateConverter());
3. Serialize a class containing a datetime.

What is the expected output? What do you see instead?
MyDateConverter is never called without using "JsonConvert" attribute. 

I think the error is in TypeDataRepository.cs:
private TypeData CreateTypeHandler(Type forType)....
"ProcessAttributes(handler, handler.ForType);" should be called even if already 
in _cache, otherwise the default encoder is always called.

Original issue reported on code.google.com by mwe.icom...@gmail.com on 12 Jul 2011 at 1:46

GoogleCodeExporter commented 9 years ago
This is actually not supported in the way that you are trying to do it.  Due to 
the way it's designed, scalar types (int's, bool's, string's, Date's, etc) 
don't allow converters to be registered on the type itself, only on the 
property.  Step #2 in your scenario is supposed to throw an exception, but 
there's a bug there and it's not.  If you want to change the formatting or 
style on the dates you can do it like this:

    DateTimeExpressionHandler handler = new DateTimeExpressionHandler("F");
    Serializer s = new Serializer(typeof(SomeClass));
    s.Config.ExpressionHandlers.InsertBefore(typeof(DateTimeExpressionHandler), handler);

You can also set DateTimeStyles and CultureInfo property on the 
DateTimeExpressionHandler as well if necessary.

Original comment by elliott....@gmail.com on 21 Dec 2011 at 10:16