Patrick Patrick - 16 days ago 7
C# Question

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.

Answer

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());
    }