Skip to content

devsko/GeoJson.aot

Repository files navigation

GeoJson.aot

A fast and easy-to-use serializer for the GeoJSON format. NativeAOT-compatible and trim safe.

NuGet package

Features

  • RFC 7946 compliant. Including custom feature property types and alternative coordinate reference systems as described in the 2008 specification.
  • NativeAOT-compatibel and trim safe.
  • Leveraging the System.Text.Json source generator under the hood.
  • Fast and memory efficient.
  • Easy to use but highly configurable™.
  • Feature and FeatureCollection are using properties of type IDictionary<string, object?> but can be customized with specialized properties type.
  • Choose coordinate precision: double, float, Half or decimal
  • Choose positions:
    • 2D (without altitude) or
    • 3D (with optional altitude)

Usage

Deserialize a feature from stream (2D positions, double precision)

using GeoJson;

namespace Samples;

using static Geo<Position2D<double>, double>;

public static class Simple
{
    public static async Task Deserialization(Stream stream)
    {
        Feature? feature = await GeoDouble2D.Default.DeserializeAsync<Feature>(stream);
    }
}

Serialize a polygon into stream with customized JSON options (3D positions, single precision)

using System.Text.Json;
using GeoJson;

namespace Samples;

using static Geo<Position3D<float>, float>;

public class WithOptions
{
    private readonly GeoSingle3D _geo = new(new JsonSerializerOptions { WriteIndented = true });

    public async Task Serialization(Stream stream, Polygon geometry)
    {
        await _geo.SerializeAsync(stream, geometry);
    }
}

Deserialize a feature with specialized property type and JSON options (2D positions, single precision)

using System.Text.Json.Serialization;
using GeoJson;

namespace Samples;

using static Geo<Position2D<float>, float>;

public class WithFeatureProperties
{
    private readonly GeoSingle2D _geo = new(MyContext.Default, typeof(MyProperties));

    public void Deserialization(string json)
    {
        Feature<MyProperties>? feature = _geo.Deserialize<Feature<MyProperties>>(json);
        if (feature is not null)
        {
            Console.WriteLine(feature.Properties.Speed);
        }
    }
}

public record struct MyProperties(string Name, int Distance, float Speed);

[JsonSerializable(typeof(Feature<MyProperties>))]
[JsonSerializable(typeof(FeatureCollection<MyProperties>))]
[JsonSourceGenerationOptions(WriteIndented = true)]
public sealed partial class MyContext : JsonSerializerContext
{ }

About

A fast and easy-to-use serializer for the GeoJSON format. NativeAOT-compatible and trim safe.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages