简体   繁体   中英

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. These require Newtonsoft's Json.NET to be present for the ITextBlobSerializer. Json.NET in turn requires System.Numerics as a reference.

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. The challenge at hand is that the application compiler does not support System.Numerics and System.Numerics is also not an embeddable interop type. My request for System.Numerics to be added has been ignored.

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.

Is anyone able to provide an ITextBlobSerializer implementation that has no other dependencies? 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. I then created my own ITextBlobSerializer implementation that utilizes the Javascript serializer like this:

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:

    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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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