I am have issue mapping a json coming from a different project api to my model Customer for the property public Dictionary<string, List<CustomerAddress>> CustomerAddress{ get; set; }
I have a ISerializationBinder defined for my json settings as follows
public class KnownTypesBinder : ISerializationBinder
{
public IList<Type> KnownTypes { get; set; }
public string TypeFormat { get; private set; }
public KnownTypesBinder(string typeFormat)
{
TypeFormat = typeFormat;
}
public void BindToName(Type serializedType, out string assemblyName, out string typeName)
{
assemblyName = null;
typeName = serializedType.Name;
}
public Type BindToType(string assemblyName, string typeName)
{
resolvedTypeName = string.Format(TypeFormat, typeName.Split('.').Last());
return Type.GetType(resolvedTypeName, true);
}
}
And my deserialization code using the above binder is as follows:
var settings = new JsonSerializerSettings
{
TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple,
TypeNameHandling = TypeNameHandling.All,
SerializationBinder = new KnownTypesBinder("CustomerWeb.API.Models.{0}, CustomerWeb.API, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")
{
KnownTypes = new List<Type>
{
typeof(CustomerProducts),
typeof(Dictionary<string,Customer>),
}
}
};
var jsonToObject = JsonConvert.DeserializeObject<Customer>(strStream, settings)
My other properties with Customer class are bing mapped. Only the dictionary type are failing . Can someplease guide meon this.
Errors Research: The below is the type of my project dictionary
System.Collections.Generic.Dictionary
2[[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Collections.Generic.List
1[[CustomerWeb.API.Models.CustomerAddress, CustomerWeb.API, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
The below one is the type coming in from Json object from another api
System.Collections.Generic.Dictionary
2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Collections.Generic.List
1[[Customer.CustomerWeb.Common.CustomerAddress, Customer.CustomerWeb.Common.V4, Version=9.0.0.0, Culture=neutral, PublicKeyToken=f291d57f641e84e4]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
I could identify the mismatch but how to handle it. I have no control on the settings of the Json serialization of the api coming from different project.
Sample Json :
{
"$id": "1",
"$type": "Customer.CustomerWeb.Common.CustomerRecommendation, Customer.CustomerWeb.Common.V4, Version=9.0.0.0, Culture=neutral, PublicKeyToken=f291d57f641e84e4",
"Addr1": "Cust1",
"CustomerAddress": {
"$id": "41",
"$type": "System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Collections.Generic.List`1[[Customer.CustomerWeb.Common.CustomerAddress, Customer.CustomerWeb.Common.V4, Version=9.0.0.0, Culture=neutral, PublicKeyToken=f291d57f641e84e4]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
"Items": {
"$id": "42",
"$type": "System.Collections.Generic.List`1[[Customer.CustomerWeb.Common.CustomerAddress, Customer.CustomerWeb.Common.V4, Version=9.0.0.0, Culture=neutral, PublicKeyToken=f291d57f641e84e4]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
"$values": [
{
"$id": "43",
"$type": "Customer.CustomerWeb.Common.CustomerAddress, Customer.CustomerWeb.Common.V4, Version=9.0.0.0, Culture=neutral, PublicKeyToken=f291d57f641e84e4",
"CustomerID": 268541064,
"ApplicationID": 110296837,
"ProductID": 1,
"ProductCode": 1,
"Amount": 1000.00000,
"AmountPercent": 2.98,
"AppliedValueType": 1,
"Id": 811760147,
"RealId": 811760147,
"DirtyProperties": {
"$id": "46",
"$type": "System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[Drive.Framework.DirtyProperty, OriginationFramework, Version=9.0.0.0, Culture=neutral, PublicKeyToken=f291d57f641e84e4]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
},
"IsDirty": false,
"IsNew": false,
"IsDeleted": false,
"ModifiedDate": "2020-05-28T09:01:27.033",
"CreatedDate": "2020-05-28T09:01:27.033",
"ModifiedByID": 0,
"CreatedByID": 0,
"DeactivationDate": null,
"ActivationDate": null,
"SerializationTracking": null
}
]
},
}
}