I am trying to understand the included behavior of code snippets, it does not work as I expected. That's what I'm doing:
- Serialize a LINQ Object for JSON
- Disable JSON in (1) back in source type
- Confirm that each property is still in the object.
- I am serializing an object from (2) back to JSON
- Print json from (4) and visually inspect it
My problem is that in (5), any LINQ object that is a property of the main LINQ object is gone, even if it was still there when it was checked in (3). All LINQ classes are LINQ to SQL. I am using the Newtons JSON.Net library. I tried the same type of logic with a non-link object, and the defect observed in (5) does not occur.
var infoId = Guid.NewGuid(); var alertId = Guid.NewGuid(); var info = new Info(); info.InfoId = infoId; var alert = new Alert(); alert.AlertId = alertId; alert.Infos.Add(info); var json = JsonConvert.SerializeObject(alert); Debug.WriteLine(json);
Update:
I did some debbugging while in serializing a deserialized object
var json2 = JsonConvert.SerializeObject(deserializedObject);
I see the following when reaching the Info Serialization step (the following code cuts off):
- this.serializing is true
- this._Infos.HasLoadedOrAssignedValues ββis false
- Get null null is called. (get returns a null value)
If I put a breakpoint and put my cursor in return this._Infos, I really see the object that it should return ...
[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Alert_Info", Storage="_Infos", ThisKey="AlertId", OtherKey="AlertId")] [global::System.Runtime.Serialization.DataMemberAttribute(Order=15, EmitDefaultValue=false)] public EntitySet<Info> Infos { get { if ((this.serializing && (this._Infos.HasLoadedOrAssignedValues == false))) { return null; } return this._Infos; } set { this._Infos.Assign(value); } }
Update: It makes me think that I might need to find a way to change the default value of HasLoadedOrAssignedValues ββwhen it is deserialized from Newtons Json.Net.
I also found a dirty fix that I don't really like, seems to work:
var deserializedObject = JsonConvert.DeserializeObject<EasAlert>(json); var list = new List<EasInfo>(); deserializedObject.EasInfos.SetSource(list);
source share