title: Newtonsoft JSon Deserialize into Primitive type
body: <p>In my C# program, I am querying a webservice and getting a reply stream back in JSON that looks something like this:</p>
  
  <pre><code>{"val":12345.12},{"val":23456.23},{"val":34567.01},...
  </code></pre>
  
  <p>or, with possibly more than 1 value per reply object:</p>
  
  <pre><code>{"val1":12345.12,"val2":1},{"val1":23456.23,"val2":3},....
  </code></pre>
  
  <p>And I have the following code utilizing the <a href="http://www.newtonsoft.com/json" rel="nofollow">Newtonsoft.Json</a> library that parses the stream and performs some action on each parsed object, one at a time:</p>
  
  <pre><code>public void ParseReply&lt;T&gt;(StreamReader sr, Action&lt;T&gt; action)
  {
      using (var reader = new JsonTextReader(sr))
      {
          var ser = new JsonSerializer();
  
          while (reader.Read())
          {
              if (reader.TokenType == JsonToken.EndArray)
                  break;
              action(ser.Deserialize&lt;T&gt;(reader));
          }
      }
  }
  </code></pre>
  
  <p>So, in the case of the second result, I have the following code:</p>
  
  <pre><code>public class MyObject
  {
      public double val1;
      public double val2;
  }
  </code></pre>
  
  <p>and then:</p>
  
  <pre><code>myJson.ParseReply&lt;MyObject&gt;(sr, obj =&gt; ...);
  </code></pre>
  
  <p>works perfectly.</p>
  
  <hr>
  
  <p>But, in the case of the first reply (1 value per object), if I try and use my method in the following way:</p>
  
  <pre><code>myJson.ParseReply&lt;double&gt;(sr, dbl =&gt; ...);
  </code></pre>
  
  <p>I get an error saying:</p>
  
  <pre><code>Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Double' because the type requires a JSON primitive value (e.g. string, number, boolean, null) to deserialize correctly.
  To fix this error either change the JSON to a JSON primitive value (e.g. string, number, boolean, null) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List&lt;T&gt;) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
  </code></pre>
  
  <p>I'm really lost / curious how I could update my code to be able to parse both of these correctly and I'm a bit lost on this error message. Any help would REALLY be appreciated!!</p>