-Fixed error when writing JSON for a JProperty with no value

-LINQ to JSON performance improvements
This commit is contained in:
James Newton-King
2012-08-11 20:12:49 +12:00
parent 7359433dc7
commit f5fec30c2a
6 changed files with 78 additions and 29 deletions
+13 -2
View File
@@ -52,6 +52,17 @@ namespace Newtonsoft.Json.Tests.Linq
[TestFixture]
public class JObjectTests : TestFixtureBase
{
[Test]
public void WritePropertyWithNoValue()
{
var o = new JObject();
o.Add(new JProperty("novalue"));
Assert.AreEqual(@"{
""novalue"": null
}", o.ToString());
}
[Test]
public void Keys()
{
@@ -1360,7 +1371,7 @@ Parameter name: arrayIndex",
public void IBindingListAddNewWithEvent()
{
JObject o = new JObject();
o.AddingNew += (s, e) => e.NewObject = new JProperty("Property!");
o._addingNew += (s, e) => e.NewObject = new JProperty("Property!");
IBindingList l = o;
object newObject = l.AddNew();
@@ -1441,7 +1452,7 @@ Parameter name: arrayIndex",
NotifyCollectionChangedAction? changedType = null;
int? index = null;
o.CollectionChanged += (s, a) =>
o._collectionChanged += (s, a) =>
{
changedType = a.Action;
index = a.NewStartingIndex;
+1 -1
View File
@@ -188,7 +188,7 @@ namespace Newtonsoft.Json.Linq
{
writer.WriteStartArray();
foreach (JToken token in ChildrenTokens)
foreach (JToken token in _values)
{
token.WriteTo(writer, converters);
}
+44 -16
View File
@@ -56,21 +56,37 @@ namespace Newtonsoft.Json.Linq
#endif
{
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
internal ListChangedEventHandler _listChanged;
internal AddingNewEventHandler _addingNew;
internal NotifyCollectionChangedEventHandler _collectionChanged;
/// <summary>
/// Occurs when the list changes or an item in the list changes.
/// </summary>
public event ListChangedEventHandler ListChanged;
public event ListChangedEventHandler ListChanged
{
add { _listChanged += value; }
remove { _listChanged -= value; }
}
/// <summary>
/// Occurs before an item is added to the collection.
/// </summary>
public event AddingNewEventHandler AddingNew;
public event AddingNewEventHandler AddingNew
{
add { _addingNew += value; }
remove { _addingNew -= value; }
}
#endif
#if SILVERLIGHT || !(NET20 || NET35 || PORTABLE)
/// <summary>
/// Occurs when the items list of the collection has changed, or the collection is reset.
/// </summary>
public event NotifyCollectionChangedEventHandler CollectionChanged;
public event NotifyCollectionChangedEventHandler CollectionChanged
{
add { _collectionChanged += value; }
remove { _collectionChanged -= value; }
}
#endif
/// <summary>
@@ -87,6 +103,7 @@ namespace Newtonsoft.Json.Linq
}
internal JContainer(JContainer other)
: this()
{
ValidationUtils.ArgumentNotNull(other, "c");
@@ -102,14 +119,19 @@ namespace Newtonsoft.Json.Linq
throw new InvalidOperationException("Cannot change {0} during a collection change event.".FormatWith(CultureInfo.InvariantCulture, GetType()));
}
internal virtual IList<JToken> CreateChildrenCollection()
{
return new List<JToken>();
}
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
/// <summary>
/// Raises the <see cref="AddingNew"/> event.
/// Raises the <see cref="_addingNew"/> event.
/// </summary>
/// <param name="e">The <see cref="AddingNewEventArgs"/> instance containing the event data.</param>
protected virtual void OnAddingNew(AddingNewEventArgs e)
{
AddingNewEventHandler handler = AddingNew;
AddingNewEventHandler handler = _addingNew;
if (handler != null)
handler(this, e);
}
@@ -120,7 +142,7 @@ namespace Newtonsoft.Json.Linq
/// <param name="e">The <see cref="ListChangedEventArgs"/> instance containing the event data.</param>
protected virtual void OnListChanged(ListChangedEventArgs e)
{
ListChangedEventHandler handler = ListChanged;
ListChangedEventHandler handler = _listChanged;
if (handler != null)
{
@@ -138,12 +160,12 @@ namespace Newtonsoft.Json.Linq
#endif
#if SILVERLIGHT || !(NET20 || NET35 || PORTABLE)
/// <summary>
/// Raises the <see cref="CollectionChanged"/> event.
/// Raises the <see cref="_collectionChanged"/> event.
/// </summary>
/// <param name="e">The <see cref="NotifyCollectionChangedEventArgs"/> instance containing the event data.</param>
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
NotifyCollectionChangedEventHandler handler = CollectionChanged;
NotifyCollectionChangedEventHandler handler = _collectionChanged;
if (handler != null)
{
@@ -330,11 +352,11 @@ namespace Newtonsoft.Json.Linq
ChildrenTokens.Insert(index, item);
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
if (ListChanged != null)
if (_listChanged != null)
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded, index));
#endif
#if SILVERLIGHT || !(NET20 || NET35 || PORTABLE)
if (CollectionChanged != null)
if (_collectionChanged != null)
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, index));
#endif
}
@@ -364,10 +386,12 @@ namespace Newtonsoft.Json.Linq
ChildrenTokens.RemoveAt(index);
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemDeleted, index));
if (_listChanged != null)
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemDeleted, index));
#endif
#if SILVERLIGHT || !(NET20 || NET35 || PORTABLE)
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, index));
if (_collectionChanged != null)
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, index));
#endif
}
@@ -426,10 +450,12 @@ namespace Newtonsoft.Json.Linq
existing.Next = null;
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged, index));
if (_listChanged != null)
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged, index));
#endif
#if SILVERLIGHT || !(NET20 || NET35 || PORTABLE)
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, item, existing, index));
if (_collectionChanged != null)
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, item, existing, index));
#endif
}
@@ -447,10 +473,12 @@ namespace Newtonsoft.Json.Linq
ChildrenTokens.Clear();
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
if (_listChanged != null)
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
#endif
#if SILVERLIGHT || !(NET20 || NET35 || PORTABLE)
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
if (_collectionChanged != null)
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
#endif
}
+8 -6
View File
@@ -159,10 +159,12 @@ namespace Newtonsoft.Json.Linq
{
OnPropertyChanged(childProperty.Name);
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged, IndexOfItem(childProperty)));
if (_listChanged != null)
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged, IndexOfItem(childProperty)));
#endif
#if SILVERLIGHT || !(NET20 || NET35 || PORTABLE)
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, childProperty, childProperty, IndexOfItem(childProperty)));
if (_collectionChanged != null)
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, childProperty, childProperty, IndexOfItem(childProperty)));
#endif
}
@@ -193,7 +195,7 @@ namespace Newtonsoft.Json.Linq
/// <returns>An <see cref="IEnumerable{JProperty}"/> of this object's properties.</returns>
public IEnumerable<JProperty> Properties()
{
return ChildrenTokens.Cast<JProperty>();
return _properties.Cast<JProperty>();
}
/// <summary>
@@ -368,7 +370,7 @@ namespace Newtonsoft.Json.Linq
{
writer.WriteStartObject();
foreach (JProperty property in ChildrenTokens)
foreach (JProperty property in _properties)
{
property.WriteTo(writer, converters);
}
@@ -476,7 +478,7 @@ namespace Newtonsoft.Json.Linq
throw new ArgumentException("The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array.");
int index = 0;
foreach (JProperty property in ChildrenTokens)
foreach (JProperty property in _properties)
{
array[arrayIndex + index] = new KeyValuePair<string, JToken>(property.Name, property.Value);
index++;
@@ -512,7 +514,7 @@ namespace Newtonsoft.Json.Linq
/// </returns>
public IEnumerator<KeyValuePair<string, JToken>> GetEnumerator()
{
foreach (JProperty property in ChildrenTokens)
foreach (JProperty property in _properties)
{
yield return new KeyValuePair<string, JToken>(property.Name, property.Value);
}
+8 -3
View File
@@ -65,14 +65,14 @@ namespace Newtonsoft.Json.Linq
public JToken Value
{
[DebuggerStepThrough]
get { return (ChildrenTokens.Count > 0) ? ChildrenTokens[0] : null; }
get { return (_content.Count > 0) ? _content[0] : null; }
set
{
CheckReentrancy();
JToken newValue = value ?? new JValue((object) null);
if (ChildrenTokens.Count == 0)
if (_content.Count == 0)
{
InsertItem(0, newValue, false);
}
@@ -209,7 +209,12 @@ namespace Newtonsoft.Json.Linq
public override void WriteTo(JsonWriter writer, params JsonConverter[] converters)
{
writer.WritePropertyName(_name);
Value.WriteTo(writer, converters);
JToken value = Value;
if (value != null)
value.WriteTo(writer, converters);
else
writer.WriteNull();
}
internal override int GetDeepHashCode()
+4 -1
View File
@@ -528,7 +528,10 @@ namespace Newtonsoft.Json.Linq
}
JsonConverter matchingConverter;
if (_value != null && ((matchingConverter = JsonSerializer.GetMatchingConverter(converters, _value.GetType())) != null))
if (_value != null
&& converters != null
&& converters.Length > 0
&& ((matchingConverter = JsonSerializer.GetMatchingConverter(converters, _value.GetType())) != null))
{
matchingConverter.WriteJson(writer, _value, new JsonSerializer());
return;