Skip to content

Using DataTabe or DataSet types as cache value type #415

@amirhosseinmp02

Description

@amirhosseinmp02

Description

In the way of using DataTabe or DataSet types as cache values in EasyCaching , we have Newtonsoft.Json and SystemTextJson options (we can't use other serializer such MessagePack or ProtoBuf because they do not support these types at all !) but there is a big problem with serializing and deserializing DataTable in json , imagine that your DataTable object has a column with type of GUID and you want to cache it with one of json serializers that it pointed in the top , The problem will occur because json did not save the structure of your table and the only thing that was saved was the data inside it, so when deserializing it, it Guid type is converted to a string !!
This problem will occur in many type that are related together like byte to int or int to long.

Example of DataTable json serializing and deserializing so you can see the problem to using json as serializer

var dt = new System.Data.DataTable();
		dt.Columns.Add("Id",typeof(System.Guid));
		dt.Rows.Add(System.Guid.NewGuid());
		
		var json = JsonConvert.SerializeObject(dt);
		Console.WriteLine(json);
		
		var newDt = JsonConvert.DeserializeObject<System.Data.DataTable>(json);
		
		var id = newDt.Rows[0].Field<Guid>("Id");
		Console.WriteLine(id);

Expected behavior: Store and restore DataSet and DataTable with EasyCaching without any problem (I saw an old project that used BinaryFormatter and no problem but unfortunately BinaryFormatter removed from EasyCaching).

Actual behavior: Disruption of the structure !

Specifications

  • Provider : Redis
  • Interceptor : .NET 6 - ASP.NET Core WebApi
  • Serializer : Newtonsoft.Json and SystemTextJson
  • System : Windows 10

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions