TypeScript code generation is unique in that it doesn’t do any serialization/deserialization into the languages native types as JSON is parsed into JS as-is, i.e the TypeScript Definitions are just describing the serialized JSON that is emitted which needs to serialize into JSON primitive Types which as it doesn’t support Dates, DateTime’s need to be sent as strings.
You’re not going to be able to get native handing across all DTOs into their native types, each language is going to need explicit configuration on both the server about what destination Type it should emit each custom type as (e.g. in TypeScriptGenerator.TypeAliases
) then the serializer for each language is going to need to be configured with how to handle that Type. But as TypeScript doesn’t use a serializer nor has any dependencies it needs to be sent as a JSON type, e.g. a string. All other languages allow their JSON serializers to be customized albeit all need to be done differently given they all have different implementations.
The easiest way would be to register a custom serializer for the Type e.g. in TypeScriptGenerator.TypeAliases
) you want to serialize and serialize it into an ISO8601 or WCF JSON Date format:
\/Date({msSinceEpoch}+{TZ Offset})\/
But even then most native Date types doesn’t support configurable TimeZone’s which is why Date’s are sent as UTC which basically defeats the purpose of using Noda time. If you go this approach you’ll still need to register TypeAliases
in each of the languages mapping it to the String
type in each language.
If you want to preserve the timezone you could convert it into a custom typed DTO that preserves the Date and TImeZone in different properties where I’d add an extension method that converts the NodaTime date into your DTO, e.g:
new MyDto {
MyDate = nodaTime.ToDto()
}
The benefit of this Custom DTO approach is that you wont need to configure the custom serializers in each language as it’s just being returned as a custom POCO DTO. If you go this option I’d recommend also register Automapping converters so the AutoMapping APIs will also transparently work.
It’s also worth mentioning this dedicated package which is built around serializing Nodatime in ServiceStack.Text:
But as I don’t see any references to the native type generators, you’ll still need to configure them on both client/server for each language.