-Fix bug when JValue of type string has a null string value
-Fix JToken casting bugs to float and decimal
This commit is contained in:
@@ -410,5 +410,23 @@ Parameter name: arrayIndex")]
|
|||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void WriteObjectNullStringValue()
|
||||||
|
{
|
||||||
|
string s = null;
|
||||||
|
JValue v = new JValue(s);
|
||||||
|
Assert.AreEqual(null, v.Value);
|
||||||
|
Assert.AreEqual(JTokenType.String, v.Type);
|
||||||
|
|
||||||
|
JObject o = new JObject();
|
||||||
|
o["title"] = v;
|
||||||
|
|
||||||
|
string output = o.ToString();
|
||||||
|
|
||||||
|
Assert.AreEqual(@"{
|
||||||
|
""title"": null
|
||||||
|
}", output);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -235,6 +235,11 @@ namespace Newtonsoft.Json.Tests.Linq
|
|||||||
Assert.AreEqual(ulong.MaxValue, (ulong)new JValue(ulong.MaxValue));
|
Assert.AreEqual(ulong.MaxValue, (ulong)new JValue(ulong.MaxValue));
|
||||||
Assert.AreEqual(ulong.MaxValue, (ulong)new JProperty("Test", new JValue(ulong.MaxValue)));
|
Assert.AreEqual(ulong.MaxValue, (ulong)new JProperty("Test", new JValue(ulong.MaxValue)));
|
||||||
Assert.AreEqual(null, (string)new JValue((string)null));
|
Assert.AreEqual(null, (string)new JValue((string)null));
|
||||||
|
Assert.AreEqual(5m, (decimal)(new JValue(5L)));
|
||||||
|
Assert.AreEqual(5m, (decimal?)(new JValue(5L)));
|
||||||
|
Assert.AreEqual(5f, (float)(new JValue(5L)));
|
||||||
|
Assert.AreEqual(5f, (float)(new JValue(5m)));
|
||||||
|
Assert.AreEqual(5f, (float?)(new JValue(5m)));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
|
|||||||
@@ -517,7 +517,7 @@ namespace Newtonsoft.Json.Linq
|
|||||||
if (v == null || !ValidateFloat(v, true))
|
if (v == null || !ValidateFloat(v, true))
|
||||||
throw new ArgumentException("Can not convert {0} to Decimal.".FormatWith(CultureInfo.InvariantCulture, GetType(v)));
|
throw new ArgumentException("Can not convert {0} to Decimal.".FormatWith(CultureInfo.InvariantCulture, GetType(v)));
|
||||||
|
|
||||||
return (decimal?)v.Value;
|
return (v.Value != null) ? (decimal?)Convert.ToDecimal(v.Value) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -613,7 +613,7 @@ namespace Newtonsoft.Json.Linq
|
|||||||
if (v == null || !ValidateFloat(v, true))
|
if (v == null || !ValidateFloat(v, true))
|
||||||
throw new ArgumentException("Can not convert {0} to Single.".FormatWith(CultureInfo.InvariantCulture, GetType(v)));
|
throw new ArgumentException("Can not convert {0} to Single.".FormatWith(CultureInfo.InvariantCulture, GetType(v)));
|
||||||
|
|
||||||
return (float?)v.Value;
|
return (v.Value != null) ? (float?)Convert.ToSingle(v.Value) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -627,7 +627,7 @@ namespace Newtonsoft.Json.Linq
|
|||||||
if (v == null || !ValidateFloat(v, false))
|
if (v == null || !ValidateFloat(v, false))
|
||||||
throw new ArgumentException("Can not convert {0} to Decimal.".FormatWith(CultureInfo.InvariantCulture, GetType(v)));
|
throw new ArgumentException("Can not convert {0} to Decimal.".FormatWith(CultureInfo.InvariantCulture, GetType(v)));
|
||||||
|
|
||||||
return (decimal)v.Value;
|
return Convert.ToDecimal(v.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -264,15 +264,14 @@ namespace Newtonsoft.Json.Linq
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteConvertableValue(JsonWriter writer, IList<JsonConverter> converters, Action<object> _defaultWrite, object value)
|
private static void WriteConvertableValue(JsonWriter writer, IList<JsonConverter> converters, Action<object> defaultWrite, object value)
|
||||||
{
|
{
|
||||||
JsonConverter matchingConverter;
|
JsonConverter matchingConverter;
|
||||||
|
|
||||||
JsonSerializer.HasMatchingConverter(converters, value.GetType(), out matchingConverter);
|
if (value != null && JsonSerializer.HasMatchingConverter(converters, value.GetType(), out matchingConverter))
|
||||||
if (matchingConverter != null)
|
|
||||||
matchingConverter.WriteJson(writer, value);
|
matchingConverter.WriteJson(writer, value);
|
||||||
else
|
else
|
||||||
_defaultWrite(value);
|
defaultWrite(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -294,7 +293,7 @@ namespace Newtonsoft.Json.Linq
|
|||||||
WriteConvertableValue(writer, converters, v => writer.WriteValue(Convert.ToDouble(v, CultureInfo.InvariantCulture)), _value);
|
WriteConvertableValue(writer, converters, v => writer.WriteValue(Convert.ToDouble(v, CultureInfo.InvariantCulture)), _value);
|
||||||
break;
|
break;
|
||||||
case JTokenType.String:
|
case JTokenType.String:
|
||||||
WriteConvertableValue(writer, converters, v => writer.WriteValue(v.ToString()), _value);
|
WriteConvertableValue(writer, converters, v => writer.WriteValue((v != null) ? v.ToString() : null), _value);
|
||||||
break;
|
break;
|
||||||
case JTokenType.Boolean:
|
case JTokenType.Boolean:
|
||||||
WriteConvertableValue(writer, converters, v => writer.WriteValue(Convert.ToBoolean(v, CultureInfo.InvariantCulture)), _value);
|
WriteConvertableValue(writer, converters, v => writer.WriteValue(Convert.ToBoolean(v, CultureInfo.InvariantCulture)), _value);
|
||||||
|
|||||||
Reference in New Issue
Block a user