Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding JsonSerializer settings #266

Open
dstoyanoff opened this issue Dec 1, 2022 · 5 comments
Open

Allow overriding JsonSerializer settings #266

dstoyanoff opened this issue Dec 1, 2022 · 5 comments
Labels
enhancement New feature or request

Comments

@dstoyanoff
Copy link

Would it be possible to allow putting custom JsonSerializerOptions when initializing the connection? That would allow everyone to control the JSON serialization in the way they want. Currently the only way to do it is via attributes on the Entity, but this isn't optimal for numerous reasons.

Is this something you would consider?

@slorello89
Copy link
Member

Hi @dstoyanoff - I'll leave this open to see if anyone else runs into this problem, FWIW there are some seralizers that Redis OM needs to use to make sure things like GeoLocs and dates work correctly, so doing something like this could have a great deal of undefined behavior.

@slorello89 slorello89 added the enhancement New feature or request label Dec 2, 2022
@dstoyanoff
Copy link
Author

You could also expose some subset of settings that people can override, while maintaining the core configuration compatible with redis.

@Twinki14
Copy link

Twinki14 commented Feb 22, 2023

Bumping this as I've been trying to use NodaTime in my entities / documents

Currently I'm unable to use NodaTime in combination with this framework, I can't add the needed JsonConverters to the SerializerSettings that this framework uses as there isn't any way to override or even add to the SerializerSettings this framework uses

Even trying to use the JsonConverter attribute leads me no-where, and even then I'd be limited to using attributes everywhere. Major downside to this framework if we can't access the SerializerSettings this framework is using, especially considering it's function

@mohammed-ehsan
Copy link

I've faced a similar issue. I was trying to use the slicing feature of json arrays supported by ReJson. For example, I can do something like: json.get <key> .property_array[1:4:1]. With RedisOM's default json serializer options, it was failing. RedisOM internal json serializer options omits empty arrays; thus, deserialized objects had null arrays. I've had to create something like this that I use for inserts:

public static async Task JsonSetAsync<TRedisModel>(this IRedisConnection connection, string key, TRedisModel model)
        where TRedisModel : IRedisModel
    {
        await connection.ExecuteAsync("JSON.SET", 
            key, 
            ".", 
            JsonSerializer.Serialize(model, CustomJsonSerializationOptions.RedisOptions));
    }

And this is how I fetch data:

public async Task<IEnumerable<T>> Seek<T>(TId sourceId, int skip, int take,
        Expression<Func<TRedisLinkedModel, IEnumerable<T>>> collection)
    {
        var key = _idStrategy.Construct(sourceId);
        int endIndexExclusive = skip + take;
        string jsonPath;
        try
        {
            jsonPath = collection.GetRedisJsonPath();
        }
        catch (InvalidOperationException)
        {
            throw new InvalidCollectionPathException($"Invalid collection path for type {typeof(TRedisLinkedModel).Name}");
        }

        var skipString = skip.ToString();
        var endIndexExclusiveString = endIndexExclusive.ToString();
        var paginatedJsonPath = $"{jsonPath}[{skipString}:{endIndexExclusiveString}:1]";

        var result = await Connection.ExecuteAsync("JSON.GET", key, paginatedJsonPath);

        var deserializedResult = JsonSerializer.Deserialize<IEnumerable<T>>(result, CustomJsonSerializationOptions.RedisOptions);
        return deserializedResult;
    }

Worth mentioning, I'm working with a special case where I've created an internal package on top of RedisOM for the organization.

@goodstas
Copy link

Hi, @slorello89 .
I'm also looking for a way to define my custom JsonSerializerOptions. I'm working with NetTopologySuite library and the properties of my business objects include types like Point and Polygon of NetTopologySuite.
Currently i got an error while saving them in Redis.
So what can i do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants