简体   繁体   English

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

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

I am writing a plugin (.Net Framework 4.61) that uses the SQLite-Net Extensions.我正在编写一个使用 SQLite-Net 扩展的插件 (.Net Framework 4.61)。 These require Newtonsoft's Json.NET to be present for the ITextBlobSerializer.这些要求 ITextBlobSerializer 存在 Newtonsoft 的 Json.NET。 Json.NET in turn requires System.Numerics as a reference. Json.NET 反过来需要 System.Numerics 作为参考。

The plugin can not use any Nuget packages and has to be submitted as zipped source which is then compiled on the servers of the application provider.该插件不能使用任何 Nuget 包,必须作为压缩源提交,然后在应用程序提供商的服务器上编译。 The challenge at hand is that the application compiler does not support System.Numerics and System.Numerics is also not an embeddable interop type.面临的挑战是应用程序编译器不支持 System.Numerics 并且 System.Numerics 也不是可嵌入的互操作类型。 My request for System.Numerics to be added has been ignored.我对添加 System.Numerics 的请求已被忽略。

Since I have no way of using System.Numerics my best approach would probably be to get rid of Json.Net and replace the ITextBlobSerializer with my own implementation.由于我无法使用 System.Numerics,因此我最好的方法可能是摆脱 Json.Net 并用我自己的实现替换 ITextBlobSerializer。

Is anyone able to provide an ITextBlobSerializer implementation that has no other dependencies?有没有人能够提供没有其他依赖项的 ITextBlobSerializer 实现? I am not sure how to proceed on that front.我不知道如何在这方面进行。

Turns out it was not that difficult.事实证明,这并不难。 I removed the JsonBlobSerializer.cs which was the only file depending on Json.Net.我删除了JsonBlobSerializer.cs ,它是唯一依赖于 Json.Net 的文件。 I then created my own ITextBlobSerializer implementation that utilizes the Javascript serializer like this:然后我创建了我自己的 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;
    }
}

Finally I edited the GetTextSerializer method in TextBlobOperations.cs to look like this so my own ITextBlobSerializer became the default:最后,我编辑的GetTextSerializer的方法TextBlobOperations.cs到这个样子的,所以我自己ITextBlobSerializer成为默认:

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

You can use TextBlobOperations.SetTextSerializer method to set new serializer.您可以使用TextBlobOperations.SetTextSerializer方法来设置新的序列化程序。

The serializer used to store and load the elements can be customized by implementing the simple ITextBlobSerializer interface.可以通过实现简单的 ITextBlobSerializer 接口来自定义用于存储和加载元素的序列化程序。

A JSON-based serializer is used if no other serializer has been specified using TextBlobOperations.SetTextSerializer method.如果未使用 TextBlobOperations.SetTextSerializer 方法指定其他序列化程序,则使用基于 JSON 的序列化程序。 To use the JSON serializer, a reference to Newtonsoft Json.Net library must be included in the project, also available as a NuGet package.要使用 JSON 序列化程序,项目中必须包含对 Newtonsoft Json.Net 库的引用,也可以作为 NuGet 包使用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM