title: Serializing C# Objects having delegate fields using JSON.NET?
body: <p>Although my problem is more of C# / JSON.NET related I will give you the context. </p>
  
  <p>I am building a game in which I need to implement a Challenge System. For those who might be unfamiliar, Challenges are basically clusters of achievements/tasks that you accomplish to earn rewards. </p>
  
  <p>So I need to have Challenges that have list of tasks that are tracked in game.</p>
  
  <p>e.g. Challenge XYZ has 3 task<br>
  1. Kill 3 Enemies<br>
  2. Collect 2 Stars<br>
  3. Reach Score of 100  </p>
  
  <p>So first of all I have a <strong>GameState</strong> Class. I am using GameState object to store all the in game data.</p>
  
  <pre><code>public class GameState
  {
      public static string SaveKey = "GameState";
  
      public int Blips { get; set; }
  
      public int ChainClearedCount { get; set;}
      public int DotClearedCount { get; set; }
  
      public int RetryUsedCount { get; set; }
      public int ContinueUsedCount { get; set; }
  
      public int LevelAttemptedCount { get; set; }
      public int LevelClearedCount { get; set; }
      public int LevelPlayedCount { get; set; }
      public int LevelFailCount { get; set; }
  
      public Dictionary&lt;ChainCombos, int&gt; ComboCountBook = new Dictionary&lt;ChainCombos, int&gt;();
      public Dictionary&lt;ChainType, int&gt; ChainTypeCountBook = new Dictionary&lt;ChainType, int&gt;();
  
      public Dictionary&lt;LevelType, int&gt; LevelTypeCountBook = new Dictionary&lt;LevelType, int&gt;();
      public Dictionary&lt;GameModes, int&gt; LevelModeCountBook = new Dictionary&lt;GameModes, int&gt;();
  
      public Dictionary&lt;GameModes, int&gt; HighScoreBook = new Dictionary&lt;GameModes, int&gt;();
      public Dictionary&lt;GameModes, int&gt; HighLevelBook = new Dictionary&lt;GameModes, int&gt;();
  
      public Dictionary&lt;LevelType, int&gt; PlaygroundLevelCountBook = new Dictionary&lt;LevelType, int&gt;();
      public Dictionary&lt;GameDifficulty, int&gt; PlaygroundDifficultyCountBook = new Dictionary&lt;GameDifficulty, int&gt;();
  
  //Followed By Constructors , Accessor Methods and Event Subscribers for GameCore Module
  }
  </code></pre>
  
  <p>So the Game State object's respective fields are updated by the GameCore class (not shown) whenever any event occurs. </p>
  
  <p>I am using JSON.NET to serialize this GameState object so that it can be used to load the game data on game launch. </p>
  
  <p>Now I have my Task Class:<br>
  The key part of Task Class is Func Value;<br>
  This stores a delegate that is used to query the GameState object. </p>
  
  <pre><code>public class Task
  {
      protected bool Incremental = true;
  
      Func&lt;int&gt; Value;
      public event Action&lt;float&gt; OnProgress;
  
      protected int targetValue;
      protected int currentValue;
  
      public bool Completed = false;
  
      public float Progress;
  
      [JsonConstructor]
      public Task(int targetValue, Func&lt;int&gt; Value, bool Incremental, bool ConnectedByDefault) //Constructor
      {
          this.targetValue = targetValue;
          this.Value = Value;
          this.Incremental = Incremental;
  
          if (ConnectedByDefault)
          {
              ConnectToGame();
          }
      }
  
      public void ConnectToGame()
      {
          GameController.GameStateEvent += UpdateProgress;
          CalculateProgress();
      }
  
      public void DisconnectFromGame()
      {
          GameController.GameStateEvent -= UpdateProgress;
      }
  
      protected virtual void OnComplete() { Completed = true; }
  
      protected void UpdateProgress(GameState s)
      {
          if (Value() &gt; currentValue)
          {
              CalculateProgress();
  
              if (OnProgress != null)
              {
                  OnProgress(Progress);
              }
          }
      }
  
      protected void CalculateProgress()
      {
          currentValue = Value();
  
          if (currentValue &lt; targetValue)
          {
              if (Incremental)
              {
                  Progress = ((float)currentValue / (float)targetValue);
              }
              else
              {
                  Progress = 0f;
              }
          }
          else
          {
              Progress = 100f;
              OnComplete();
          }
      }
  }
  </code></pre>
  
  <p>Now if i have to declare a Task i do something like this </p>
  
  <pre><code>Task a = new Task(200, () =&gt; GameController.CurrentGameState.DotClearedCount, true, false);
  </code></pre>
  
  <p>As you can see what I am doing is sending a delegate that will be allocated to Value field of the task and will be used internally to query the Game-state object's indicated field (in this case DotClearedCount).</p>
  
  <p>My issue is that I have to serialize the task objects since I need to keep track of player's task progress over multiple game sessions. </p>
  
  <p>JSON.NET is incapable of serializing delegate fields of objects as far as I know. I mean I checked storing a delegate and the JSON output was something like "delegatEntry = null".</p>
  
  <p>So I am unable to wrap my head around the issue of how I am going to save my Task objects such that when they are deserialized and constructed by JSON.NET they get linked back to my GameState object. </p>
  
  <p>I looked in LINQ queries I am still unable to find anything concrete that will help me. I mean as far as I understood I can build LINQ queries on objects like Lists and Dictionaries but then again my GameState objects have a lot of Dictionaries that I want to target separately for different tasks. </p>