title: How to serialize object to json based on the client culture with newtonsoft?
body: <p>I'm using Newtonsoft.Json.JsonConverter and .NET Framework 4.7.2.</p>
  
  <p>I have a controller that returns an object with a property of type LocalizedString which is just a simple object with 2 properties: english and french representation of a string.</p>
  
  <p>The culture is set to the current thread by the controller like this:</p>
  
  <pre><code>Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(this.Name);
  Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(this.Name);
  </code></pre>
  
  <p>The culture comes from the request cookies and when queried within the controller GET method it works. </p>
  
  <p>The LocalizedString has a ToString method that looks like this:</p>
  
  <pre><code>public string ToString(string language)
  {
     return language.Equals(Language.English.CultureName, StringComparison.OrdinalIgnoreCase) ? this.English : this.French;
  }
  </code></pre>
  
  <p>Now I have a JsonConverter to manage the serialization of the LocalizedString objects that looks like this:</p>
  
  <pre><code>public class LocalizedStringJsonConverter : JsonConverter
  {
      #region Methods
  
      /// &lt;summary&gt;
      /// Writes the JSON representation of the object.
      /// &lt;/summary&gt;
      /// &lt;param name="writer"&gt;The &lt;see cref="T:Newtonsoft.Json.JsonWriter"/&gt; to write to.&lt;/param&gt;&lt;param name="value"&gt;The value.&lt;/param&gt;&lt;param name="serializer"&gt;The calling serializer.&lt;/param&gt;
      public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
      {
          writer.WriteValue(((LocalizedString)value).ToString(Thread.CurrentThread.CurrentCulture.Name));
      }
  
      /// &lt;summary&gt;
      /// Reads the JSON representation of the object.
      /// &lt;/summary&gt;
      /// &lt;param name="reader"&gt;The &lt;see cref="T:Newtonsoft.Json.JsonReader"/&gt; to read from.&lt;/param&gt;&lt;param name="objectType"&gt;Type of the object.&lt;/param&gt;&lt;param name="existingValue"&gt;The existing value of object being read.&lt;/param&gt;&lt;param name="serializer"&gt;The calling serializer.&lt;/param&gt;
      /// &lt;returns&gt;
      /// The object value.
      /// &lt;/returns&gt;
      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
      {
          throw new NotImplementedException();
      }
  
      /// &lt;summary&gt;
      /// Determines whether this instance can convert the specified object type.
      /// &lt;/summary&gt;
      /// &lt;param name="objectType"&gt;Type of the object.&lt;/param&gt;
      /// &lt;returns&gt;
      /// &lt;c&gt;true&lt;/c&gt; if this instance can convert the specified object type; otherwise, &lt;c&gt;false&lt;/c&gt;.
      /// &lt;/returns&gt;
      public override bool CanConvert(Type objectType)
      {
          return objectType == typeof(LocalizedString);
      }
  
      #endregion
  }
  </code></pre>
  
  <p>When I query the culture within the JsonConverter like this:
      Thread.CurrentThread.CurrentCulture.Name</p>
  
  <p>The value is always the culture of the server (which is french when i debug on my PC).</p>
  
  <p>I'm trying to understand what I'm doing wrong...
  I know of plenty of other ways to do what I want to do, like I could let client manage the culture with javascript or my controller could actually return a string pre-localized but that would mean I would need to refactor the whole web app.. which I'm trying to avoid.</p>
  
  <p>Thanks for your time</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I've tried to add my own filter to make sure the culture is still set after my controller returns but the JsonConverter is executed after the OnActionExecuted method of my filter and the culture is still wrong. I don't know what calls the JsonConverter but that is probably where the culture should be set...</p>