-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow (opt-in) modifying IAttributesTable (#118)
This has to be opt-in, because it would otherwise break anyone who expects a JsonElementAttributesTable whose JsonElement they can do stuff with. Resolves #117
- Loading branch information
1 parent
368733d
commit 28502d5
Showing
12 changed files
with
553 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/NetTopologySuite.IO.GeoJSON4STJ/Converters/IPartiallyDeserializedAttributesTable.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System.Text.Json; | ||
|
||
namespace NetTopologySuite.Features | ||
{ | ||
/// <summary> | ||
/// An <see cref="IAttributesTable"/> that has been <b>partially</b> deserialized to a strongly- | ||
/// typed CLR object model, but which still contains some remnants of the JSON source that | ||
/// produced it which may require the consumer to tell us more about what types they expected. | ||
/// <para/> | ||
/// Due to an intentional limitation in <c>System.Text.Json</c>, there is no way to produce a | ||
/// standalone GeoJSON object that includes enough information to produce an object graph that's | ||
/// complete with nested members of arbitrary types. | ||
/// <para/> | ||
/// In that spirit, this interface allows you to pick up where this library left off and use the | ||
/// Feature's attributes in a more strongly-typed fashion using your own knowledge of whatever | ||
/// internal structure the GeoJSON object is expected to have. | ||
/// </summary> | ||
public interface IPartiallyDeserializedAttributesTable : IAttributesTable | ||
{ | ||
/// <summary> | ||
/// Attempts to convert this entire table to a strongly-typed CLR object. | ||
/// <para> | ||
/// Modifications to the result <b>WILL NOT</b> propagate back to this table, or vice-versa. | ||
/// </para> | ||
/// </summary> | ||
/// <typeparam name="T"> | ||
/// The type of object to convert to. | ||
/// </typeparam> | ||
/// <param name="options"> | ||
/// The <see cref="JsonSerializerOptions"/> to use for the deserialization. | ||
/// </param> | ||
/// <param name="deserialized"> | ||
/// Receives the converted value on success, or the default value on failure. | ||
/// </param> | ||
/// <returns> | ||
/// A value indicating whether or not the conversion succeeded. | ||
/// </returns> | ||
bool TryDeserializeJsonObject<T>(JsonSerializerOptions options, out T deserialized); | ||
|
||
/// <summary> | ||
/// Attempts to get a strongly-typed CLR object that corresponds to a single property that's | ||
/// present in this table. | ||
/// <para> | ||
/// Modifications to the result <b>WILL NOT</b> propagate back to this table, or vice-versa. | ||
/// </para> | ||
/// </summary> | ||
/// <typeparam name="T"> | ||
/// The type of object to retrieve. | ||
/// </typeparam> | ||
/// <param name="propertyName"> | ||
/// The name of the property in this table to get as the specified type. | ||
/// </param> | ||
/// <param name="options"> | ||
/// The <see cref="JsonSerializerOptions"/> to use for the deserialization. | ||
/// </param> | ||
/// <param name="deserialized"> | ||
/// Receives the converted value on success, or the default value on failure. | ||
/// </param> | ||
/// <returns> | ||
/// A value indicating whether or not the conversion succeeded. | ||
/// </returns> | ||
bool TryGetJsonObjectPropertyValue<T>(string propertyName, JsonSerializerOptions options, out T deserialized); | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
src/NetTopologySuite.IO.GeoJSON4STJ/Converters/JsonArrayInAttributesTableWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
|
||
using NetTopologySuite.IO.Converters; | ||
|
||
namespace NetTopologySuite.Features | ||
{ | ||
internal sealed class JsonArrayInAttributesTableWrapper : IList<object>, IReadOnlyList<object> | ||
{ | ||
private readonly JsonArray _array; | ||
|
||
private readonly JsonSerializerOptions _serializerOptions; | ||
|
||
public JsonArrayInAttributesTableWrapper(JsonArray array, JsonSerializerOptions serializerOptions) | ||
{ | ||
_array = array; | ||
_serializerOptions = serializerOptions; | ||
} | ||
|
||
public object this[int index] | ||
{ | ||
get => Utility.ObjectFromJsonNode(_array[index], _serializerOptions); | ||
set => _array[index] = Utility.ObjectToJsonNode(value, _serializerOptions); | ||
} | ||
|
||
public int Count => _array.Count; | ||
|
||
bool ICollection<object>.IsReadOnly => false; | ||
|
||
public void Add(object item) | ||
{ | ||
_array.Add(Utility.ObjectToJsonNode(item, _serializerOptions)); | ||
} | ||
|
||
public void Clear() | ||
{ | ||
_array.Clear(); | ||
} | ||
|
||
public bool Contains(object item) | ||
{ | ||
foreach (JsonNode node in _array) | ||
{ | ||
object obj = Utility.ObjectFromJsonNode(node, _serializerOptions); | ||
if (Equals(item, obj)) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public void CopyTo(object[] array, int arrayIndex) | ||
{ | ||
foreach (JsonNode node in _array) | ||
{ | ||
array[arrayIndex++] = Utility.ObjectFromJsonNode(node, _serializerOptions); | ||
} | ||
} | ||
|
||
public IEnumerator<object> GetEnumerator() | ||
{ | ||
return _array.Select(node => Utility.ObjectFromJsonNode(node, _serializerOptions)).GetEnumerator(); | ||
} | ||
|
||
public int IndexOf(object item) | ||
{ | ||
for (int i = 0; i < _array.Count; i++) | ||
{ | ||
object obj = Utility.ObjectFromJsonNode(_array[i], _serializerOptions); | ||
if (Equals(item, obj)) | ||
{ | ||
return i; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
public void Insert(int index, object item) | ||
{ | ||
_array.Insert(index, Utility.ObjectToJsonNode(item, _serializerOptions)); | ||
} | ||
|
||
public bool Remove(object item) | ||
{ | ||
for (int i = 0; i < _array.Count; i++) | ||
{ | ||
object obj = Utility.ObjectFromJsonNode(_array[i], _serializerOptions); | ||
if (Equals(item, obj)) | ||
{ | ||
_array.RemoveAt(i); | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public void RemoveAt(int index) | ||
{ | ||
_array.RemoveAt(index); | ||
} | ||
|
||
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.