如何在没有 Json.Net 依赖性的情况下使用 SQLite-Net 扩展(使用替代 ITextBlobSerializer)?

How to use SQLite-Net Extensions without Json.Net dependency (with alternative ITextBlobSerializer)?

我正在编写一个使用 SQLite-Net 扩展的插件 (.Net Framework 4.61)。这些要求 Newtonsoft 的 Json.NET 存在于 ITextBlobSerializer 中。 Json.NET 又需要 System.Numerics 作为参考。

该插件不能使用任何 Nuget 包,必须作为压缩源提交,然后在应用程序提供商的服务器上编译。目前的挑战是应用程序编译器不支持 System.Numerics 并且 System.Numerics 也不是可嵌入的互操作类型。我要求添加 System.Numerics 的请求已被忽略。

因为我无法使用 System.Numerics,所以我最好的方法可能是摆脱 Json.Net 并用我自己的实现替换 ITextBlobSerializer。

是否有人能够提供没有其他依赖项的 ITextBlobSerializer 实现?我不确定如何在这方面进行。

原来并没有那么难。我删除了 JsonBlobSerializer.cs,它是唯一依赖于 Json.Net 的文件。然后,我创建了自己的 ITextBlobSerializer 实现,它使用 Javascript 序列化程序,如下所示:

using System;
using System.Web.Script.Serialization;
using SQLite.Extensions.TextBlob;

public class BlobSerializer : ITextBlobSerializer
{
    private readonly JavaScriptSerializer serializer = new JavaScriptSerializer();

    public string Serialize(object element)
    {
        var str = serializer.Serialize(element);
        return str;

    }

    public object Deserialize(string text, Type type)
    {
        var result = serializer.Deserialize(text, type);
        return result;
    }
}

最后,我将 TextBlobOperations.cs 中的 GetTextSerializer 方法编辑为如下所示,因此我自己的 ITextBlobSerializer 成为默认方法:

    public static ITextBlobSerializer GetTextSerializer()
    {
        // If not specified, use the Javascript serializer
        return _serializer ?? (_serializer = new BlobSerializer());
    }

您可以使用TextBlobOperations.SetTextSerializer方法来设置新的序列化器。

The serializer used to store and load the elements can be customized by implementing the simple ITextBlobSerializer interface.

A JSON-based serializer is used if no other serializer has been specified using TextBlobOperations.SetTextSerializer method. To use the JSON serializer, a reference to Newtonsoft Json.Net library must be included in the project, also available as a NuGet package.

https://bitbucket.org/twincoders/sqlite-net-extensions