Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trouble serializing Dates #2

Closed
omatrot opened this issue Sep 12, 2019 · 1 comment

Comments

@omatrot
Copy link

commented Sep 12, 2019

In the models included in my previous issue, I have a Date field configured as the following:

  @autoserializeAs(() => Date)
  dbInsertTime: Date;

This allows me to accept "dbInsertTime":"2015-11-27T07:53:38.6" as the incoming value for this field.

However the Serialization of the resulting object gives me:

"dbInsertTime":1549527854940

Which is considered invalid by NewtonSoft.Json on server side:

Newtonsoft.Json.JsonReaderException
  HResult=0x80131500
  Message=Unexpected character encountered while parsing value: 1. Path 'dbInsertTime', line 1, position 238.
  Source=Newtonsoft.Json
  StackTrace:
   at Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType)
   at Newtonsoft.Json.JsonTextReader.ReadAsDateTime()
   at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at RTE.Technologies.SafeProtect.WebAdmin.API.Binders.TrackableEntityModelBinder.BindModelAsync(ModelBindingContext bindingContext) in C:\Users\olivier.matrot\source\repos\SafeProtect\WebAdministration\SafeProtect.WebAdmin.Web\API\Binders\TrackableEntityModelBinder.cs:line 49
   at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.BinderTypeModelBinder.<BindModelAsync>d__2.MoveNext()

What Am I missing here ?

@omatrot

This comment has been minimized.

Copy link
Author

commented Sep 12, 2019

I solved the problem by overriding the serialization of dates in onSerialized:

static onSerialized(json: IJsonObject, instance: Vehicule) {
   json["dbInsertTime"] = instance.dbInsertTime.toISOString();
}
@omatrot omatrot closed this Sep 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.