Newtonsoft.Json.JsonReader.ReadAsDouble C# (CSharp) Method

ReadAsDouble() public method

Reads the next JSON token from the stream as a Nullable{T} of Double.
public ReadAsDouble ( ) : double?
return double?
        public virtual double? ReadAsDouble()
        {
            JsonToken t = GetContentToken();

            switch (t)
            {
                case JsonToken.None:
                case JsonToken.Null:
                case JsonToken.EndArray:
                    return null;
                case JsonToken.Integer:
                case JsonToken.Float:
                    if (!(Value is double))
                    {
                        double d;
#if !(NET20 || NET35 || PORTABLE40 || PORTABLE) || NETSTANDARD1_1
                        if (Value is BigInteger)
                        {
                            d = (double)(BigInteger)Value;
                        }
                        else
#endif
                        {
                            d = Convert.ToDouble(Value, CultureInfo.InvariantCulture);
                        }

                        SetToken(JsonToken.Float, d, false);
                    }

                    return (double)Value;
                case JsonToken.String:
                    return ReadDoubleString((string)Value);
            }

            throw JsonReaderException.Create(this, "Error reading double. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, t));
        }

Usage Example

 public override Vector2 ReadJson(Newtonsoft.Json.JsonReader reader, Type objectType, Vector2 existingValue, bool hasExistingValue, JsonSerializer serializer)
 {
     if (reader.TokenType == JsonToken.StartObject)
     {
         JObject item = JObject.Load(reader);
         existingValue.x = item["X"].Value <float>();
         existingValue.y = item["Y"].Value <float>();
     }
     else
     {
         existingValue.x = (float)reader.ReadAsDouble().Value;
         existingValue.y = (float)reader.ReadAsDouble().Value;
     }
     return(existingValue);
 }
All Usage Examples Of Newtonsoft.Json.JsonReader::ReadAsDouble