@@ -21,7 +21,7 @@ namespace GeoJSON.Text.Feature
2121 public class Feature < TGeometry , TProps > : GeoJSONObject , IEquatable < Feature < TGeometry , TProps > >
2222 where TGeometry : IGeometryObject
2323 {
24- private string _id ;
24+ private FeatureId _id ;
2525 private bool _idHasValue = false ;
2626 private TGeometry _geometry ;
2727 private bool _geometryHasValue = false ;
@@ -33,14 +33,14 @@ public Feature()
3333
3434 }
3535
36- public Feature ( TGeometry geometry , TProps properties , string id = null )
36+ public Feature ( TGeometry geometry , TProps properties , FeatureId id = null )
3737 {
3838 Geometry = geometry ;
3939 Properties = properties ;
4040 Id = id ;
4141 }
4242
43- public Feature ( IGeometryObject geometry , TProps properties , string id = null )
43+ public Feature ( IGeometryObject geometry , TProps properties , FeatureId id = null )
4444 {
4545 Geometry = ( TGeometry ) geometry ;
4646 Properties = properties ;
@@ -53,7 +53,8 @@ public Feature(IGeometryObject geometry, TProps properties, string id = null)
5353
5454 [ JsonPropertyName ( "id" ) ]
5555 [ JsonIgnore ( Condition = JsonIgnoreCondition . WhenWritingNull ) ]
56- public string Id {
56+ [ JsonConverter ( typeof ( FeatureIdConverter ) ) ]
57+ public FeatureId Id {
5758 get
5859 {
5960 return _id ;
@@ -195,12 +196,12 @@ public Feature()
195196
196197 }
197198
198- public Feature ( IGeometryObject geometry , IDictionary < string , object > properties = null , string id = null )
199+ public Feature ( IGeometryObject geometry , IDictionary < string , object > properties = null , FeatureId id = null )
199200 : base ( geometry , properties , id )
200201 {
201202 }
202203
203- public Feature ( IGeometryObject geometry , object properties , string id = null )
204+ public Feature ( IGeometryObject geometry , object properties , FeatureId id = null )
204205 : base ( geometry , properties , id )
205206 {
206207 }
@@ -225,7 +226,7 @@ public Feature()
225226 /// <param name="geometry">The Geometry Object.</param>
226227 /// <param name="properties">The properties.</param>
227228 /// <param name="id">The (optional) identifier.</param>
228- public Feature ( TGeometry geometry , IDictionary < string , object > properties = null , string id = null )
229+ public Feature ( TGeometry geometry , IDictionary < string , object > properties = null , FeatureId id = null )
229230 : base ( geometry , properties ?? new Dictionary < string , object > ( ) , id )
230231 {
231232 }
@@ -236,7 +237,7 @@ public Feature(TGeometry geometry, IDictionary<string, object> properties = null
236237 /// <param name="geometry">The Geometry Object.</param>
237238 /// <param name="properties">The properties.</param>
238239 /// <param name="id">The (optional) identifier.</param>
239- public Feature ( IGeometryObject geometry , IDictionary < string , object > properties = null , string id = null )
240+ public Feature ( IGeometryObject geometry , IDictionary < string , object > properties = null , FeatureId id = null )
240241 : base ( ( TGeometry ) geometry , properties ?? new Dictionary < string , object > ( ) , id )
241242 {
242243 }
@@ -250,7 +251,7 @@ public Feature(IGeometryObject geometry, IDictionary<string, object> properties
250251 /// properties
251252 /// </param>
252253 /// <param name="id">The (optional) identifier.</param>
253- public Feature ( TGeometry geometry , object properties , string id = null )
254+ public Feature ( TGeometry geometry , object properties , FeatureId id = null )
254255 : this ( geometry , GetDictionaryOfPublicProperties ( properties ) , id )
255256 {
256257 }
@@ -312,5 +313,32 @@ public override int GetHashCode()
312313 {
313314 return ! ( left ? . Equals ( right ) ?? right is null ) ;
314315 }
316+ }
317+
318+ public sealed class FeatureId : IEquatable < FeatureId >
319+ {
320+ private readonly string _strId ;
321+ private readonly ulong ? _numId ;
322+
323+ private FeatureId ( string str , ulong ? num ) => ( _strId , _numId ) = ( str , num ) ;
324+
325+ public static implicit operator FeatureId ( string str ) => new ( str , null ) ;
326+ public static implicit operator FeatureId ( ulong num ) => new ( null , num ) ;
327+
328+ public bool IsNumeric => _numId . HasValue ;
329+ public bool IsString => ! IsNumeric ;
330+
331+ public static implicit operator string ( FeatureId id ) => id . IsString ? id . _strId : throw new InvalidCastException ( ) ;
332+ public static implicit operator ulong ( FeatureId id ) => id . IsNumeric ? id . _numId . Value : throw new InvalidCastException ( ) ;
333+
334+ public override int GetHashCode ( ) => ( _strId . GetHashCode ( ) * 397 ) ^ _numId . GetHashCode ( ) ;
335+
336+ public bool Equals ( FeatureId other ) => _strId == other . _strId && _numId == other . _numId ;
337+
338+ public override bool Equals ( object obj ) => obj is FeatureId id && Equals ( id ) ;
339+
340+ public static bool operator == ( FeatureId left , FeatureId right ) => left . Equals ( right ) ;
341+
342+ public static bool operator != ( FeatureId left , FeatureId right ) => ! ( left == right ) ;
315343 }
316344}
0 commit comments