-Fixed error when writing JSON for a JProperty with no value
-LINQ to JSON performance improvements
This commit is contained in:
@@ -52,6 +52,17 @@ namespace Newtonsoft.Json.Tests.Linq
|
|||||||
[TestFixture]
|
[TestFixture]
|
||||||
public class JObjectTests : TestFixtureBase
|
public class JObjectTests : TestFixtureBase
|
||||||
{
|
{
|
||||||
|
[Test]
|
||||||
|
public void WritePropertyWithNoValue()
|
||||||
|
{
|
||||||
|
var o = new JObject();
|
||||||
|
o.Add(new JProperty("novalue"));
|
||||||
|
|
||||||
|
Assert.AreEqual(@"{
|
||||||
|
""novalue"": null
|
||||||
|
}", o.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void Keys()
|
public void Keys()
|
||||||
{
|
{
|
||||||
@@ -1360,7 +1371,7 @@ Parameter name: arrayIndex",
|
|||||||
public void IBindingListAddNewWithEvent()
|
public void IBindingListAddNewWithEvent()
|
||||||
{
|
{
|
||||||
JObject o = new JObject();
|
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;
|
IBindingList l = o;
|
||||||
object newObject = l.AddNew();
|
object newObject = l.AddNew();
|
||||||
@@ -1441,7 +1452,7 @@ Parameter name: arrayIndex",
|
|||||||
NotifyCollectionChangedAction? changedType = null;
|
NotifyCollectionChangedAction? changedType = null;
|
||||||
int? index = null;
|
int? index = null;
|
||||||
|
|
||||||
o.CollectionChanged += (s, a) =>
|
o._collectionChanged += (s, a) =>
|
||||||
{
|
{
|
||||||
changedType = a.Action;
|
changedType = a.Action;
|
||||||
index = a.NewStartingIndex;
|
index = a.NewStartingIndex;
|
||||||
|
|||||||
@@ -188,7 +188,7 @@ namespace Newtonsoft.Json.Linq
|
|||||||
{
|
{
|
||||||
writer.WriteStartArray();
|
writer.WriteStartArray();
|
||||||
|
|
||||||
foreach (JToken token in ChildrenTokens)
|
foreach (JToken token in _values)
|
||||||
{
|
{
|
||||||
token.WriteTo(writer, converters);
|
token.WriteTo(writer, converters);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,21 +56,37 @@ namespace Newtonsoft.Json.Linq
|
|||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
||||||
|
internal ListChangedEventHandler _listChanged;
|
||||||
|
internal AddingNewEventHandler _addingNew;
|
||||||
|
internal NotifyCollectionChangedEventHandler _collectionChanged;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Occurs when the list changes or an item in the list changes.
|
/// Occurs when the list changes or an item in the list changes.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public event ListChangedEventHandler ListChanged;
|
public event ListChangedEventHandler ListChanged
|
||||||
|
{
|
||||||
|
add { _listChanged += value; }
|
||||||
|
remove { _listChanged -= value; }
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Occurs before an item is added to the collection.
|
/// Occurs before an item is added to the collection.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public event AddingNewEventHandler AddingNew;
|
public event AddingNewEventHandler AddingNew
|
||||||
|
{
|
||||||
|
add { _addingNew += value; }
|
||||||
|
remove { _addingNew -= value; }
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#if SILVERLIGHT || !(NET20 || NET35 || PORTABLE)
|
#if SILVERLIGHT || !(NET20 || NET35 || PORTABLE)
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Occurs when the items list of the collection has changed, or the collection is reset.
|
/// Occurs when the items list of the collection has changed, or the collection is reset.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public event NotifyCollectionChangedEventHandler CollectionChanged;
|
public event NotifyCollectionChangedEventHandler CollectionChanged
|
||||||
|
{
|
||||||
|
add { _collectionChanged += value; }
|
||||||
|
remove { _collectionChanged -= value; }
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -87,6 +103,7 @@ namespace Newtonsoft.Json.Linq
|
|||||||
}
|
}
|
||||||
|
|
||||||
internal JContainer(JContainer other)
|
internal JContainer(JContainer other)
|
||||||
|
: this()
|
||||||
{
|
{
|
||||||
ValidationUtils.ArgumentNotNull(other, "c");
|
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()));
|
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)
|
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Raises the <see cref="AddingNew"/> event.
|
/// Raises the <see cref="_addingNew"/> event.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="e">The <see cref="AddingNewEventArgs"/> instance containing the event data.</param>
|
/// <param name="e">The <see cref="AddingNewEventArgs"/> instance containing the event data.</param>
|
||||||
protected virtual void OnAddingNew(AddingNewEventArgs e)
|
protected virtual void OnAddingNew(AddingNewEventArgs e)
|
||||||
{
|
{
|
||||||
AddingNewEventHandler handler = AddingNew;
|
AddingNewEventHandler handler = _addingNew;
|
||||||
if (handler != null)
|
if (handler != null)
|
||||||
handler(this, e);
|
handler(this, e);
|
||||||
}
|
}
|
||||||
@@ -120,7 +142,7 @@ namespace Newtonsoft.Json.Linq
|
|||||||
/// <param name="e">The <see cref="ListChangedEventArgs"/> instance containing the event data.</param>
|
/// <param name="e">The <see cref="ListChangedEventArgs"/> instance containing the event data.</param>
|
||||||
protected virtual void OnListChanged(ListChangedEventArgs e)
|
protected virtual void OnListChanged(ListChangedEventArgs e)
|
||||||
{
|
{
|
||||||
ListChangedEventHandler handler = ListChanged;
|
ListChangedEventHandler handler = _listChanged;
|
||||||
|
|
||||||
if (handler != null)
|
if (handler != null)
|
||||||
{
|
{
|
||||||
@@ -138,12 +160,12 @@ namespace Newtonsoft.Json.Linq
|
|||||||
#endif
|
#endif
|
||||||
#if SILVERLIGHT || !(NET20 || NET35 || PORTABLE)
|
#if SILVERLIGHT || !(NET20 || NET35 || PORTABLE)
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Raises the <see cref="CollectionChanged"/> event.
|
/// Raises the <see cref="_collectionChanged"/> event.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="e">The <see cref="NotifyCollectionChangedEventArgs"/> instance containing the event data.</param>
|
/// <param name="e">The <see cref="NotifyCollectionChangedEventArgs"/> instance containing the event data.</param>
|
||||||
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
|
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
|
||||||
{
|
{
|
||||||
NotifyCollectionChangedEventHandler handler = CollectionChanged;
|
NotifyCollectionChangedEventHandler handler = _collectionChanged;
|
||||||
|
|
||||||
if (handler != null)
|
if (handler != null)
|
||||||
{
|
{
|
||||||
@@ -330,11 +352,11 @@ namespace Newtonsoft.Json.Linq
|
|||||||
ChildrenTokens.Insert(index, item);
|
ChildrenTokens.Insert(index, item);
|
||||||
|
|
||||||
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
||||||
if (ListChanged != null)
|
if (_listChanged != null)
|
||||||
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded, index));
|
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded, index));
|
||||||
#endif
|
#endif
|
||||||
#if SILVERLIGHT || !(NET20 || NET35 || PORTABLE)
|
#if SILVERLIGHT || !(NET20 || NET35 || PORTABLE)
|
||||||
if (CollectionChanged != null)
|
if (_collectionChanged != null)
|
||||||
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, index));
|
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, index));
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -364,10 +386,12 @@ namespace Newtonsoft.Json.Linq
|
|||||||
ChildrenTokens.RemoveAt(index);
|
ChildrenTokens.RemoveAt(index);
|
||||||
|
|
||||||
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
||||||
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemDeleted, index));
|
if (_listChanged != null)
|
||||||
|
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemDeleted, index));
|
||||||
#endif
|
#endif
|
||||||
#if SILVERLIGHT || !(NET20 || NET35 || PORTABLE)
|
#if SILVERLIGHT || !(NET20 || NET35 || PORTABLE)
|
||||||
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, index));
|
if (_collectionChanged != null)
|
||||||
|
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, index));
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -426,10 +450,12 @@ namespace Newtonsoft.Json.Linq
|
|||||||
existing.Next = null;
|
existing.Next = null;
|
||||||
|
|
||||||
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
||||||
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged, index));
|
if (_listChanged != null)
|
||||||
|
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged, index));
|
||||||
#endif
|
#endif
|
||||||
#if SILVERLIGHT || !(NET20 || NET35 || PORTABLE)
|
#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
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -447,10 +473,12 @@ namespace Newtonsoft.Json.Linq
|
|||||||
ChildrenTokens.Clear();
|
ChildrenTokens.Clear();
|
||||||
|
|
||||||
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
||||||
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
|
if (_listChanged != null)
|
||||||
|
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
|
||||||
#endif
|
#endif
|
||||||
#if SILVERLIGHT || !(NET20 || NET35 || PORTABLE)
|
#if SILVERLIGHT || !(NET20 || NET35 || PORTABLE)
|
||||||
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
|
if (_collectionChanged != null)
|
||||||
|
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -159,10 +159,12 @@ namespace Newtonsoft.Json.Linq
|
|||||||
{
|
{
|
||||||
OnPropertyChanged(childProperty.Name);
|
OnPropertyChanged(childProperty.Name);
|
||||||
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
||||||
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged, IndexOfItem(childProperty)));
|
if (_listChanged != null)
|
||||||
|
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged, IndexOfItem(childProperty)));
|
||||||
#endif
|
#endif
|
||||||
#if SILVERLIGHT || !(NET20 || NET35 || PORTABLE)
|
#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
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,7 +195,7 @@ namespace Newtonsoft.Json.Linq
|
|||||||
/// <returns>An <see cref="IEnumerable{JProperty}"/> of this object's properties.</returns>
|
/// <returns>An <see cref="IEnumerable{JProperty}"/> of this object's properties.</returns>
|
||||||
public IEnumerable<JProperty> Properties()
|
public IEnumerable<JProperty> Properties()
|
||||||
{
|
{
|
||||||
return ChildrenTokens.Cast<JProperty>();
|
return _properties.Cast<JProperty>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -368,7 +370,7 @@ namespace Newtonsoft.Json.Linq
|
|||||||
{
|
{
|
||||||
writer.WriteStartObject();
|
writer.WriteStartObject();
|
||||||
|
|
||||||
foreach (JProperty property in ChildrenTokens)
|
foreach (JProperty property in _properties)
|
||||||
{
|
{
|
||||||
property.WriteTo(writer, converters);
|
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.");
|
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;
|
int index = 0;
|
||||||
foreach (JProperty property in ChildrenTokens)
|
foreach (JProperty property in _properties)
|
||||||
{
|
{
|
||||||
array[arrayIndex + index] = new KeyValuePair<string, JToken>(property.Name, property.Value);
|
array[arrayIndex + index] = new KeyValuePair<string, JToken>(property.Name, property.Value);
|
||||||
index++;
|
index++;
|
||||||
@@ -512,7 +514,7 @@ namespace Newtonsoft.Json.Linq
|
|||||||
/// </returns>
|
/// </returns>
|
||||||
public IEnumerator<KeyValuePair<string, JToken>> GetEnumerator()
|
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);
|
yield return new KeyValuePair<string, JToken>(property.Name, property.Value);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,14 +65,14 @@ namespace Newtonsoft.Json.Linq
|
|||||||
public JToken Value
|
public JToken Value
|
||||||
{
|
{
|
||||||
[DebuggerStepThrough]
|
[DebuggerStepThrough]
|
||||||
get { return (ChildrenTokens.Count > 0) ? ChildrenTokens[0] : null; }
|
get { return (_content.Count > 0) ? _content[0] : null; }
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
CheckReentrancy();
|
CheckReentrancy();
|
||||||
|
|
||||||
JToken newValue = value ?? new JValue((object) null);
|
JToken newValue = value ?? new JValue((object) null);
|
||||||
|
|
||||||
if (ChildrenTokens.Count == 0)
|
if (_content.Count == 0)
|
||||||
{
|
{
|
||||||
InsertItem(0, newValue, false);
|
InsertItem(0, newValue, false);
|
||||||
}
|
}
|
||||||
@@ -209,7 +209,12 @@ namespace Newtonsoft.Json.Linq
|
|||||||
public override void WriteTo(JsonWriter writer, params JsonConverter[] converters)
|
public override void WriteTo(JsonWriter writer, params JsonConverter[] converters)
|
||||||
{
|
{
|
||||||
writer.WritePropertyName(_name);
|
writer.WritePropertyName(_name);
|
||||||
Value.WriteTo(writer, converters);
|
|
||||||
|
JToken value = Value;
|
||||||
|
if (value != null)
|
||||||
|
value.WriteTo(writer, converters);
|
||||||
|
else
|
||||||
|
writer.WriteNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal override int GetDeepHashCode()
|
internal override int GetDeepHashCode()
|
||||||
|
|||||||
@@ -528,7 +528,10 @@ namespace Newtonsoft.Json.Linq
|
|||||||
}
|
}
|
||||||
|
|
||||||
JsonConverter matchingConverter;
|
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());
|
matchingConverter.WriteJson(writer, _value, new JsonSerializer());
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user