forked from dotnet/efcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use SQLite native representations in JSON
Closes dotnet#30727
- Loading branch information
Showing
21 changed files
with
763 additions
and
137 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
53 changes: 53 additions & 0 deletions
53
src/EFCore.Sqlite.Core/Storage/Internal/Json/SqliteJsonByteArrayReaderWriter.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,53 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Globalization; | ||
using System.Text; | ||
using System.Text.Encodings.Web; | ||
using System.Text.Json; | ||
using Microsoft.EntityFrameworkCore.Storage.Json; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal.Json; | ||
|
||
/// <summary> | ||
/// The Sqlite-specific JsonValueReaderWrite for byte[]. Generates the SQLite representation (e.g. X'0102') rather than base64, in order | ||
/// to match our SQLite non-JSON representation. | ||
/// </summary> | ||
/// <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 SqliteJsonByteArrayReaderWriter : JsonValueReaderWriter<byte[]> | ||
{ | ||
/// <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 SqliteJsonByteArrayReaderWriter Instance { get; } = new(); | ||
|
||
private SqliteJsonByteArrayReaderWriter() | ||
{ | ||
} | ||
|
||
/// <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 byte[] FromJsonTyped(ref Utf8JsonReaderManager manager, object? existingObject = null) | ||
=> Convert.FromHexString(manager.CurrentReader.GetString()!); | ||
|
||
/// <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, byte[] value) | ||
=> writer.WriteStringValue(Convert.ToHexString(value)); | ||
} |
59 changes: 59 additions & 0 deletions
59
src/EFCore.Sqlite.Core/Storage/Internal/Json/SqliteJsonDateTimeOffsetReaderWriter.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,59 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Globalization; | ||
using System.Text.Encodings.Web; | ||
using System.Text.Json; | ||
using Microsoft.EntityFrameworkCore.Storage.Json; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal.Json; | ||
|
||
/// <summary> | ||
/// The Sqlite-specific JsonValueReaderWrite for DateTime. Generates a ISO8601 string representation with a space instead of a T | ||
/// separating the date and time components, in order to match our SQLite non-JSON representation. | ||
/// </summary> | ||
/// <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 SqliteJsonDateTimeOffsetReaderWriter : JsonValueReaderWriter<DateTimeOffset> | ||
{ | ||
private const string DateTimeOffsetFormatConst = @"{0:yyyy\-MM\-dd HH\:mm\:ss.FFFFFFFzzz}"; | ||
|
||
/// <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 SqliteJsonDateTimeOffsetReaderWriter Instance { get; } = new(); | ||
|
||
private SqliteJsonDateTimeOffsetReaderWriter() | ||
{ | ||
} | ||
|
||
/// <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 DateTimeOffset FromJsonTyped(ref Utf8JsonReaderManager manager, object? existingObject = null) | ||
// => manager.CurrentReader.GetDateTimeOffset(); | ||
=> DateTimeOffset.Parse(manager.CurrentReader.GetString()!, CultureInfo.InvariantCulture); | ||
|
||
/// <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, DateTimeOffset value) | ||
// We use UnsafeRelaxedJsonEscaping to prevent the DateTimeOffset plus (+) sign from getting escaped | ||
=> writer.WriteStringValue( | ||
JsonEncodedText.Encode( | ||
string.Format(CultureInfo.InvariantCulture, DateTimeOffsetFormatConst, value), | ||
JavaScriptEncoder.UnsafeRelaxedJsonEscaping)); | ||
} |
53 changes: 53 additions & 0 deletions
53
src/EFCore.Sqlite.Core/Storage/Internal/Json/SqliteJsonDateTimeReaderWriter.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,53 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Globalization; | ||
using System.Text.Json; | ||
using Microsoft.EntityFrameworkCore.Storage.Json; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal.Json; | ||
|
||
/// <summary> | ||
/// The Sqlite-specific JsonValueReaderWrite for DateTime. Generates a ISO8601 string representation with a space instead of a T | ||
/// separating the date and time components, in order to match our SQLite non-JSON representation. | ||
/// </summary> | ||
/// <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 SqliteJsonDateTimeReaderWriter : JsonValueReaderWriter<DateTime> | ||
{ | ||
private const string DateTimeFormatConst = @"{0:yyyy\-MM\-dd HH\:mm\:ss.FFFFFFF}"; | ||
|
||
/// <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 SqliteJsonDateTimeReaderWriter Instance { get; } = new(); | ||
|
||
private SqliteJsonDateTimeReaderWriter() | ||
{ | ||
} | ||
|
||
/// <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 DateTime FromJsonTyped(ref Utf8JsonReaderManager manager, object? existingObject = null) | ||
=> DateTime.Parse(manager.CurrentReader.GetString()!, CultureInfo.InvariantCulture); | ||
|
||
/// <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, DateTime value) | ||
=> writer.WriteStringValue(string.Format(CultureInfo.InvariantCulture, DateTimeFormatConst, value)); | ||
} |
53 changes: 53 additions & 0 deletions
53
src/EFCore.Sqlite.Core/Storage/Internal/Json/SqliteJsonDecimalReaderWriter.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,53 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Globalization; | ||
using System.Text.Json; | ||
using Microsoft.EntityFrameworkCore.Storage.Json; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal.Json; | ||
|
||
/// <summary> | ||
/// The Sqlite-specific JsonValueReaderWrite for decimal. Generates a string representation instead of a JSON number, in order to match | ||
/// our SQLite non-JSON representation. | ||
/// </summary> | ||
/// <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 SqliteJsonDecimalReaderWriter : JsonValueReaderWriter<decimal> | ||
{ | ||
private const string DecimalFormatConst = "{0:0.0###########################}"; | ||
|
||
/// <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 SqliteJsonDecimalReaderWriter Instance { get; } = new(); | ||
|
||
private SqliteJsonDecimalReaderWriter() | ||
{ | ||
} | ||
|
||
/// <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 decimal FromJsonTyped(ref Utf8JsonReaderManager manager, object? existingObject = null) | ||
=> decimal.Parse(manager.CurrentReader.GetString()!, CultureInfo.InvariantCulture); | ||
|
||
/// <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, decimal value) | ||
=> writer.WriteStringValue(string.Format(CultureInfo.InvariantCulture, DecimalFormatConst, value)); | ||
} |
Oops, something went wrong.