【问题标题】:Values are being null while getting it from Redis for complex objects从 Redis 获取复杂对象的值时为空
【发布时间】:2020-05-17 00:31:56
【问题描述】:

我正在尝试将一个复杂对象添加到 Redis 中,但在从 Redis 检索值时,我将某些值设为空。下面是我正在尝试的粗略示例。我有一个复杂对象,我使用 JsonConvert 序列化该复杂对象并将其添加到 Redis 中。属性 CollectionID 有两个具有各自值的计数,但在从 Redis 获取它并反序列化后,该值将变为 null。请查看下图

属性 CollectionID 有两个 ID 值:

从 Redis 缓存中获取该属性时变为 null

下面是示例:

class Program
{
    private static IDatabase _cache;
    private static ConnectionMultiplexer _connection;

    static void Main(string[] args)
    {
        _connection = ConnectionMultiplexer.Connect("localhost");
        _cache = _connection.GetDatabase();

        List<Id> id = new List<Id>() { new Id() { ID = 10 }, new Id() { ID = 20 } };
        Collection<Customers> collection = new Collection<Customers>() { new Customers(id) };
        Product product = new Product(new Guid(), collection, 1);
        _cache.StringSet("Redis_Key", GetSerializedString(product));
        var value = JsonConvert.DeserializeObject<Product>(_cache.StringGet("Redis_Key"));
    }

    private static String GetSerializedString<Test1>(Test1 value)
    {
        return JsonConvert.SerializeObject(
                    value,
                    Formatting.Indented,
                    new JsonSerializerSettings
                    {
                        ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
                        PreserveReferencesHandling = PreserveReferencesHandling.All
                    });
    }
}

public class Product
{
    public Product(
        Guid parentGuid,
        Collection<Customers> collection,
        int number)
    {
        _parentGuid = parentGuid;
        _collection = collection;
        _number = number;
    }

    private Guid _parentGuid;
    public Guid ParentGuid
    {
        get { return _parentGuid; }
    }

    private Collection<Customers> _collection;
    public Collection<Customers> Collection
    {
        get { return _collection; }
    }

    private int _number;
    public int number
    {
        get { return _number; }
    }
}

public class Customers
{
    public Customers(IEnumerable<Id> id)
    {
        _id = id;
    }

    private IEnumerable<Id> _id;

    public IEnumerable<Id> CollectionID
    {
        get { return _id; }
    }
}

public class Id
{
    public int ID { get; set; }
}

任何建议都会有很大帮助。

谢谢, 阿尼什

【问题讨论】:

  • 尝试重构,将字符串与序列化和反序列化分开获取和设置。比较原始字符串应该可以告诉您是 redis 问题还是序列化问题

标签: c# redis json.net stackexchange.redis


【解决方案1】:

问题是您在CollectionID 上没有设置器。

public IEnumerable<Id> CollectionID
{
    get { return _id; }
    set { _id = value; } //need a setter
}

如果您需要设置器为private,您可以这样做,但您需要ContractResolver。可以添加包JsonNet.PrivateSettersContractResolvers,然后添加

using JsonNet.PrivateSettersContractResolvers;
...
var value = JsonConvert.DeserializeObject<Product>(_cache.StringGet("Redis_Key"),
        new JsonSerializerSettings
        {
            ContractResolver = new PrivateSetterContractResolver()
        });

Private setters in Json.Net

【讨论】:

  • 嗨,Leo,您的建议有效,但在另一种情况下,“Id”类继承了一个 ISerializable 类,并将值返回为空。有什么方法可以让 JsonConvert 序列化一个已经自定义序列化的类?
  • 听起来像是一个有效的新问题,请将更多详细信息作为另一个问题发布
猜你喜欢
  • 2016-04-30
  • 1970-01-01
  • 2016-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-27
  • 2020-04-05
  • 2018-05-25
相关资源
最近更新 更多