benjaminvr

Gedrag JObject

Nov 27th, 2021
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.35 KB | None | 0 0
  1. using System;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4.  
  5. public class Circulair1 {
  6.     public string Naam => "Ik ben circulair 1";
  7.     public Circulair2 instantieVan2;
  8.    
  9.     public void ZetInstantieVan2 (Circulair2 inst2) {
  10.         instantieVan2 = inst2;
  11.     }
  12. }
  13.  
  14. public class Circulair2 {
  15.     public string Naam => "Ik ben circulair 2";
  16.     public Circulair1 instantieVan1;
  17.    
  18.     public Circulair2(Circulair1 inst1){
  19.         instantieVan1 = inst1;
  20.     }
  21. }
  22.                    
  23. public class Program
  24. {
  25.     public static void Main()
  26.     {
  27.         /* Preparatie */
  28.         Circulair1 Circ1 = new();
  29.         Circulair2 Circ2 = new(Circ1);
  30.         Circ1.ZetInstantieVan2(Circ2);
  31.        
  32.         /* Circulair zonder instellingen */
  33.         //JObject pogingMetCirculair = JObject.FromObject(Circ1);  //JsonSerializerException: Self referencing loop detected
  34.         //JObject pogingMetCirculair2 = JObject.FromObject(Circ2);
  35.        
  36.         /* Instellingen */
  37.         var JSS = new JsonSerializer {
  38.             NullValueHandling = NullValueHandling.Ignore,
  39.             ReferenceLoopHandling = ReferenceLoopHandling.Ignore //,
  40.             //PreserveReferencesHandling = PreserveReferencesHandling.Objects
  41.         };
  42.        
  43.         /* Circulair met deze instellingen */
  44.         JObject pogingMetSettingsCirculair = JObject.FromObject(Circ1, JSS);
  45.         JObject pogingMetSettingsCirculair2 = JObject.FromObject(Circ2, JSS);
  46.        
  47.  
  48.         Console.WriteLine(pogingMetSettingsCirculair.ToString());
  49.         Console.WriteLine(pogingMetSettingsCirculair2.ToString());
  50.         /*  -- Output
  51.             {
  52.                 "instantieVan2": {
  53.                     "Naam": "Ik ben circulair 2"
  54.                 },
  55.                 "Naam": "Ik ben circulair 1"
  56.                 }
  57.                
  58.             {
  59.                "instantieVan1": {
  60.                     "Naam": "Ik ben circulair 1"
  61.                 },
  62.                 "Naam": "Ik ben circulair 2"
  63.             }
  64.            
  65.             -- Gedrag
  66.                     Json.NET will ignore objects in reference loops and not serialize them.
  67.                     The first time an object is encountered >>it will be serialized<< as usual but if the object is encountered
  68.                     as a child object of itself the serializer will skip serializing it.
  69.         */
  70.        
  71.         /* Nieuwe instelling met serialize */
  72.         var JSS2 = new JsonSerializer {
  73.             NullValueHandling = NullValueHandling.Ignore,
  74.             ReferenceLoopHandling = ReferenceLoopHandling.Serialize
  75.         };
  76.        
  77.         //JObject pogingMetSettingsSerializeCirculair = JObject.FromObject(Circ1, JSS2); // stack overflow
  78.         //JObject pogingMetSettingsSerializeCirculair2 = JObject.FromObject(Circ2, JSS2);
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment