Linq object properties disappear after serialization

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); //All looking good, nothing missing var deserializedObject = JsonConvert.DeserializeObject<Alert>(json); Assert.AreEqual(alertId, deserializedObject.AlertId); //Assert is valid Assert.AreEqual(infoId, deserializedObject.Infos[0].InfoId); //Assert is valid var json2 = JsonConvert.SerializeObject(deserializedObject); Debug.WriteLine(json2); //Infos is gone 

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); //<--- Fix, the Info will show up in json2 Assert.AreEqual(alertId, deserializedObject.AlertId); Assert.AreEqual(infoId, deserializedObject.EasInfos[0].InfoId); 
+6
source share
1 answer

I tried Microsoft JavaScriptSerializer and it worked fine. Code and output can be found below.

Could this be Newton's mistake?

 void Main() { 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 jss = new JavaScriptSerializer(); var json = jss.Serialize(alert); //JsonConvert.SerializeObject(alert); Debug.WriteLine(json); //All looking good, nothing missing var deserializedObject = jss.Deserialize<Alert>(json); //JsonConvert.DeserializeObject<Alert>(json); (alertId == deserializedObject.AlertId).Dump(); //Assert is valid (infoId == deserializedObject.Infos[0].InfoId).Dump(); //Assert is valid var json2 = jss.Serialize(deserializedObject); //JsonConvert.SerializeObject(deserializedObject); Debug.WriteLine(json2); //Infos is gone - NOT GONE! } public class Alert { public Guid AlertId { get; set; } public List<Info> Infos { get; set; } public Alert() { Infos = new List<Info>(); } } public class Info { public Guid InfoId { get; set; } } 

Output

{"AlertId": "0340e855-065c-4ac7-868e-5999fa4b7862", "Information": [{"InfoId": "31e269a1-4354-423d-84bc-62f6dc06b10f"}]}

True

True

{"AlertId": "0340e855-065c-4ac7-868e-5999fa4b7862", "Information": [{"InfoId": "31e269a1-4354-423d-84bc-62f6dc06b10f"}]}

0
source

All Articles