Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Globalization;
using Microsoft.EntityFrameworkCore.Sqlite.Storage.Json.Internal;

namespace Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal;

Expand Down Expand Up @@ -30,7 +31,9 @@ public class SqliteHalfTypeMapping : RelationalTypeMapping
public SqliteHalfTypeMapping(string storeType)
: base(
new RelationalTypeMappingParameters(
new CoreTypeMappingParameters(typeof(Half)),
new CoreTypeMappingParameters(
Comment thread
AndriySvyryd marked this conversation as resolved.
typeof(Half),
jsonValueReaderWriter: SqliteJsonHalfReaderWriter.Instance),
storeType))
Comment thread
AndriySvyryd marked this conversation as resolved.
{
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json;
using Microsoft.EntityFrameworkCore.Storage.Json;

namespace Microsoft.EntityFrameworkCore.Sqlite.Storage.Json.Internal;

/// <summary>
/// The Sqlite-specific JsonValueReaderWriter for <see cref="Half" />. Reads and writes JSON number values by converting through
/// <see langword="float" />, matching the SQLite REAL storage representation.
/// </summary>
Comment thread
AndriySvyryd marked this conversation as resolved.
/// <remarks>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </remarks>
public sealed class SqliteJsonHalfReaderWriter : JsonValueReaderWriter<Half>
{
private static readonly PropertyInfo InstanceProperty = typeof(SqliteJsonHalfReaderWriter).GetProperty(nameof(Instance))!;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static SqliteJsonHalfReaderWriter Instance { get; } = new();

private SqliteJsonHalfReaderWriter()
{
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override Half FromJsonTyped(ref Utf8JsonReaderManager manager, object? existingObject = null)
=> (Half)manager.CurrentReader.GetSingle();

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override void ToJsonTyped(Utf8JsonWriter writer, Half value)
=> writer.WriteNumberValue((float)value);

/// <inheritdoc />
public override Expression ConstructorExpression
=> Expression.Property(null, InstanceProperty);
}
13 changes: 13 additions & 0 deletions test/EFCore.Sqlite.FunctionalTests/JsonTypesSqliteTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,19 @@ public override Task Can_read_write_collection_of_nullable_GUID_JSON_values(stri
=> base.Can_read_write_collection_of_nullable_GUID_JSON_values(
"""{"Prop":["00000000-0000-0000-0000-000000000000",null,"8C44242F-8E3F-4A20-8BE8-98C7C1AADEBD","FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF"]}""");

[Theory]
[InlineData(-65504f, """{"Prop":-65504}""")]
[InlineData(65504f, """{"Prop":65504}""")]
[InlineData(0f, """{"Prop":0}""")]
[InlineData(1.5f, """{"Prop":1.5}""")]
public virtual Task Can_read_write_Half_JSON_values(float value, string json)
=> Can_read_and_write_JSON_value<HalfType, Half>(nameof(HalfType.Half), (Half)value, json);

protected class HalfType
{
public Half Half { get; set; }
}

public override Task Can_read_write_ulong_enum_JSON_values(EnumU64 value, string json)
=> Can_read_and_write_JSON_value<EnumU64Type, EnumU64>(nameof(EnumU64Type.EnumU64), value, json);

Expand Down
Loading