Skip to content

Commit

Permalink
json: fix call resolution ambiguity by moving JsonObject overloads as…
Browse files Browse the repository at this point in the history
… extensions methods

- ambiguous when passing collection expressions (ex: AddRange([ ]) )
  • Loading branch information
KrzysFR committed Jul 22, 2024
1 parent 88d6621 commit 3403a45
Showing 1 changed file with 30 additions and 108 deletions.
138 changes: 30 additions & 108 deletions Doxense.Core/Serialization/JSON/ObjectModel/JsonArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -874,31 +874,6 @@ public JsonArray AddRange(ReadOnlySpan<JsonValue?> values)
return this;
}

/// <summary>Appends all the elements of a read-only span to the end of this <see cref="JsonArray"/></summary>
/// <remarks>Any mutable element in in <paramref name="values"/> will be converted to read-only before being added. Elements that were already read-only will be added be reference.</remarks>
[CollectionAccess(CollectionAccessType.UpdatedContent)]
public JsonArray AddRange(ReadOnlySpan<JsonObject?> values)
{
if (m_readOnly) throw FailCannotMutateReadOnlyValue(this);
if (values.Length > 0)
{
// resize
int size = m_size;
EnsureCapacity(size + values.Length);
var items = m_items;
Contract.Debug.Assert(items != null && size + values.Length <= items.Length);

// append
var tail = items.AsSpan(size, values.Length);
for (int i = 0; i < tail.Length; i++)
{
tail[i] = (values[i] ?? JsonNull.Null);
}
m_size = size + values.Length;
}
return this;
}

/// <summary>Appends all the elements of a read-only span to the end of this <see cref="JsonArray"/></summary>
[CollectionAccess(CollectionAccessType.UpdatedContent)]
public JsonArray AddRange<TSource>(ReadOnlySpan<TSource> values, Func<TSource, JsonValue?> selector)
Expand Down Expand Up @@ -947,31 +922,6 @@ public JsonArray AddRangeReadOnly(ReadOnlySpan<JsonValue?> values)
return this;
}

/// <summary>Appends all the elements of a read-only span to the end of this <see cref="JsonArray"/></summary>
/// <remarks>Any mutable element in in <paramref name="values"/> will be converted to read-only before being added. Elements that were already read-only will be added be reference.</remarks>
[CollectionAccess(CollectionAccessType.UpdatedContent)]
public JsonArray AddRangeReadOnly(ReadOnlySpan<JsonObject?> values)
{
if (m_readOnly) throw FailCannotMutateReadOnlyValue(this);
if (values.Length > 0)
{
// resize
int size = m_size;
EnsureCapacity(size + values.Length);
var items = m_items;
Contract.Debug.Assert(items != null && size + values.Length <= items.Length);

// append
var tail = items.AsSpan(size, values.Length);
for (int i = 0; i < tail.Length; i++)
{
tail[i] = (values[i] ?? JsonNull.Null).ToReadOnly();
}
m_size = size + values.Length;
}
return this;
}

/// <summary>Appends all the elements of a read-only span to the end of this <see cref="JsonArray"/></summary>
/// <remarks>Any mutable element in in <paramref name="values"/> will be converted to read-only before being added. Elements that were already read-only will be added be reference.</remarks>
[CollectionAccess(CollectionAccessType.UpdatedContent)]
Expand Down Expand Up @@ -1006,15 +956,6 @@ public JsonArray AddRange(JsonValue[] values)
return AddRange(values.AsSpan()!);
}

/// <summary>Appends all the elements of an array to the end of this <see cref="JsonArray"/></summary>
/// <remarks>Any mutable element in in <paramref name="values"/> will be converted to read-only before being added. Elements that were already read-only will be added be reference.</remarks>
[CollectionAccess(CollectionAccessType.UpdatedContent)]
public JsonArray AddRange(JsonObject[] values)
{
Contract.NotNull(values);
return AddRange(values.AsSpan()!);
}

/// <summary>Appends all the elements of an array to the end of this <see cref="JsonArray"/></summary>
/// <remarks>Any mutable element in in <paramref name="values"/> will be converted to read-only before being added. Elements that were already read-only will be added be reference.</remarks>
[CollectionAccess(CollectionAccessType.UpdatedContent)]
Expand All @@ -1033,15 +974,6 @@ public JsonArray AddRangeReadOnly(JsonValue[] values)
return AddRangeReadOnly(values.AsSpan()!);
}

/// <summary>Appends all the elements of an array to the end of this <see cref="JsonArray"/></summary>
/// <remarks>Any mutable element in in <paramref name="values"/> will be converted to read-only before being added. Elements that were already read-only will be added be reference.</remarks>
[CollectionAccess(CollectionAccessType.UpdatedContent)]
public JsonArray AddRangeReadOnly(JsonObject[] values)
{
Contract.NotNull(values);
return AddRangeReadOnly(values.AsSpan()!);
}

/// <summary>Appends all the elements of an array to the end of this <see cref="JsonArray"/></summary>
/// <remarks>Any mutable element in in <paramref name="items"/> will be converted to read-only before being added. Elements that were already read-only will be added be reference.</remarks>
[CollectionAccess(CollectionAccessType.UpdatedContent)]
Expand Down Expand Up @@ -1104,11 +1036,6 @@ public JsonArray AddRange(IEnumerable<JsonValue?> values)
return this;
}

/// <summary>Appends all the elements of an <see cref="IEnumerable{T}"/> to the end of this <see cref="JsonArray"/></summary>
/// <remarks>Any mutable element in in <paramref name="values"/> will be converted to read-only before being added. Elements that were already read-only will be added be reference.</remarks>
[CollectionAccess(CollectionAccessType.UpdatedContent)]
public JsonArray AddRange(IEnumerable<JsonObject?> values) => AddRange((IEnumerable<JsonValue?>) values);

/// <summary>Appends all the elements of an <see cref="IEnumerable{T}"/> to the end of this <see cref="JsonArray"/></summary>
/// <remarks>Any mutable element in in <paramref name="values"/> will be converted to read-only before being added. Elements that were already read-only will be added be reference.</remarks>
[CollectionAccess(CollectionAccessType.UpdatedContent)]
Expand Down Expand Up @@ -1235,11 +1162,6 @@ public JsonArray AddRangeReadOnly(IEnumerable<JsonValue?> values)
return this;
}

/// <summary>Appends all the elements of an <see cref="IEnumerable{T}"/> to the end of this <see cref="JsonArray"/></summary>
/// <remarks>Any mutable element in in <paramref name="values"/> will be converted to read-only before being added. Elements that were already read-only will be added be reference.</remarks>
[CollectionAccess(CollectionAccessType.UpdatedContent)]
public JsonArray AddRangeReadOnly(IEnumerable<JsonObject?> values) => AddRangeReadOnly((IEnumerable<JsonValue?>) values);

#endregion

#region AddValues [of T] ...
Expand Down Expand Up @@ -4785,6 +4707,32 @@ public static TValue[] ToArray<TValue>(this JsonArray self, [InstantHandle] Func
return buf;
}

/// <summary>Appends all the elements of an <see cref="IEnumerable{T}"/> to the end of this <see cref="JsonArray"/></summary>
/// <remarks>Any mutable element in in <paramref name="values"/> will be converted to read-only before being added. Elements that were already read-only will be added be reference.</remarks>
[CollectionAccess(CollectionAccessType.UpdatedContent)]
public static JsonArray AddRange(this JsonArray self, IEnumerable<JsonObject?> values)
=> self.AddRange((IEnumerable<JsonValue?>) values);

/// <summary>Appends all the elements of an <see cref="IEnumerable{T}"/> to the end of this <see cref="JsonArray"/></summary>
/// <remarks>Any mutable element in in <paramref name="values"/> will be converted to read-only before being added. Elements that were already read-only will be added be reference.</remarks>
[CollectionAccess(CollectionAccessType.UpdatedContent)]
public static JsonArray AddRangeReadOnly(this JsonArray self, IEnumerable<JsonObject?> values)
=> self.AddRangeReadOnly((IEnumerable<JsonValue?>) values);

/// <summary>Appends all the elements of an array to the end of this <see cref="JsonArray"/></summary>
/// <remarks>Any mutable element in in <paramref name="values"/> will be converted to read-only before being added. Elements that were already read-only will be added be reference.</remarks>
[CollectionAccess(CollectionAccessType.UpdatedContent)]
public static JsonArray AddRange(this JsonArray self, JsonObject[] values) =>
// ReSharper disable once CoVariantArrayConversion
self.AddRange((JsonValue[]) values);

/// <summary>Appends all the elements of an array to the end of this <see cref="JsonArray"/></summary>
/// <remarks>Any mutable element in in <paramref name="values"/> will be converted to read-only before being added. Elements that were already read-only will be added be reference.</remarks>
[CollectionAccess(CollectionAccessType.UpdatedContent)]
public static JsonArray AddRangeReadOnly(this JsonArray self, JsonObject[] values) =>
// ReSharper disable once CoVariantArrayConversion
self.AddRangeReadOnly((JsonValue[]) values);

#region ToJsonArray...

#region Mutable...
Expand All @@ -4796,27 +4744,13 @@ public static TValue[] ToArray<TValue>(this JsonArray self, [InstantHandle] Func
public static JsonArray ToJsonArray([InstantHandle] this ReadOnlySpan<JsonValue> source)
=> new JsonArray().AddRange(source!);

/// <summary>Creates a <see cref="JsonArray"/> from a read-only span.</summary>
/// <param name="source">The <see cref="T:System.ReadOnlySpan`1" /> to create a <see cref="JsonArray" /> from.</param>
/// <returns>A <see cref="JsonArray" /> that contains elements from the input span.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static JsonArray ToJsonArray([InstantHandle] this ReadOnlySpan<JsonObject> source)
=> new JsonArray().AddRange(source!);

/// <summary>Creates a <see cref="JsonArray"/> from a span.</summary>
/// <param name="source">The <see cref="T:System.ReadOnlySpan`1" /> to create a <see cref="JsonArray" /> from.</param>
/// <returns>A <see cref="JsonArray" /> that contains elements from the input span.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static JsonArray ToJsonArray([InstantHandle] this Span<JsonValue> source)
=> new JsonArray().AddRange(source!);

/// <summary>Creates a <see cref="JsonArray"/> from a span.</summary>
/// <param name="source">The <see cref="T:System.ReadOnlySpan`1" /> to create a <see cref="JsonArray" /> from.</param>
/// <returns>A <see cref="JsonArray" /> that contains elements from the input span.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static JsonArray ToJsonArray([InstantHandle] this Span<JsonObject> source)
=> new JsonArray().AddRange(source!);

/// <summary>Creates a <see cref="JsonArray"/> from an array.</summary>
/// <param name="source">The array to create a <see cref="JsonArray" /> from.</param>
/// <exception cref="T:System.ArgumentNullException"> <paramref name="source" /> is <see langword="null" />.</exception>
Expand All @@ -4831,7 +4765,8 @@ public static JsonArray ToJsonArray([InstantHandle] this JsonValue[] source)
/// <returns>A <see cref="JsonArray" /> that contains elements from the input sequence.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static JsonArray ToJsonArray([InstantHandle] this JsonObject[] source)
=> new JsonArray().AddRange(source);
// ReSharper disable once CoVariantArrayConversion
=> new JsonArray().AddRange((JsonValue[]) source);

/// <summary>Creates a <see cref="JsonArray"/> from an <see cref="IEnumerable{JsonValue}"/>.</summary>
/// <param name="source">The <see cref="T:System.Collections.Generic.IEnumerable`1" /> to create a <see cref="JsonArray" /> from.</param>
Expand Down Expand Up @@ -4914,27 +4849,13 @@ public static JsonArray ToJsonArray<TSource, TValue>(this IEnumerable<TSource> s
public static JsonArray ToJsonArrayReadOnly(this ReadOnlySpan<JsonValue> source)
=> new JsonArray().AddRangeReadOnly(source!).FreezeUnsafe();

/// <summary>Creates a read-only <see cref="JsonArray"/> from a read-only span.</summary>
/// <param name="source">The <see cref="T:System.ReadOnlySpan`1" /> to create a <see cref="JsonArray" /> from.</param>
/// <returns>A read-only <see cref="JsonArray" /> that contains elements from the input span.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static JsonArray ToJsonArrayReadOnly(this ReadOnlySpan<JsonObject> source)
=> new JsonArray().AddRangeReadOnly(source!).FreezeUnsafe();

/// <summary>Creates a read-only <see cref="JsonArray"/> from a span.</summary>
/// <param name="source">The <see cref="T:System.ReadOnlySpan`1" /> to create a <see cref="JsonArray" /> from.</param>
/// <returns>A read-only <see cref="JsonArray" /> that contains elements from the input span.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static JsonArray ToJsonArrayReadOnly(this Span<JsonValue> source)
=> new JsonArray().AddRangeReadOnly(source!).FreezeUnsafe();

/// <summary>Creates a read-only <see cref="JsonArray"/> from a span.</summary>
/// <param name="source">The <see cref="T:System.ReadOnlySpan`1" /> to create a <see cref="JsonArray" /> from.</param>
/// <returns>A read-only <see cref="JsonArray" /> that contains elements from the input span.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static JsonArray ToJsonArrayReadOnly(this Span<JsonObject> source)
=> new JsonArray().AddRangeReadOnly(source!).FreezeUnsafe();

/// <summary>Creates a read-only <see cref="JsonArray"/> from an array.</summary>
/// <param name="source">The array to create a <see cref="JsonArray" /> from.</param>
/// <exception cref="T:System.ArgumentNullException"> <paramref name="source" /> is <see langword="null" />.</exception>
Expand All @@ -4949,7 +4870,8 @@ public static JsonArray ToJsonArrayReadOnly(this JsonValue[] source)
/// <returns>A read-only <see cref="JsonArray" /> that contains elements from the input sequence.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static JsonArray ToJsonArrayReadOnly(this JsonObject[] source)
=> new JsonArray().AddRangeReadOnly(source).FreezeUnsafe();
// ReSharper disable once CoVariantArrayConversion
=> new JsonArray().AddRangeReadOnly((JsonValue[]) source).FreezeUnsafe();

/// <summary>Creates a read-only <see cref="JsonArray"/> from an <see cref="IEnumerable{JsonValue}"/>.</summary>
/// <param name="source">The <see cref="T:System.Collections.Generic.IEnumerable`1" /> to create a <see cref="JsonArray" /> from.</param>
Expand Down

0 comments on commit 3403a45

Please sign in to comment.