Deserialisation Help

Related to my service, I am doing some custom persistence in the building a in-memory database over Redis used for testing, and I am stuck trying to deserialize generic objects, and was wondering if I can still use ServiceStack serialization routines to avoid going to another library.

I have a serialized object coming back from storage, represented by the following JSON:

  "Properties": {
    "AKey": "\"AAECAwQFBgcICQA=\"",
    "ExpiresUtc": "\"2001-05-21T12:51:25.0000000+00:00\""
  "Id": "2f34f9f6-1584-4cd6-b535-c54722569f24",
  "RowKey": "2f34f9f6-1584-4cd6-b535-c54722569f24",
  "RowVersion": "0",
  "Timestamp": "2000-05-21T12:51:25.0000000+00:00"

My job is to deserialise it to any typed object, like this one (in this case):

public class MyCustomEntity : BaseEntity
     public byte[] AKey { get; set; }

     public DateTime ExpiresUtc { get; set; }

where BaseEntity has all the other properties on it, like: Id, RowVersion etc.

I have a generic method: TEntity Deserialize<TEntity>(string json) in which to do my deserializaiton.

This would normally be a single call to JsonSerializer.Deserialize(), except for the fact that I have one thing that is not quite normal in this picture.
If you look closely, at AKey and ExpiryDate you notice they have been serialized as individual properties, and therefore they are quoted in " because they are byte[] and DateTime types. All other types are serialized just fine.

I am confident ServiceStack has all the classes I need here, but I am not sure of the approach with those classes. Can someone suggest a straightforward approach, and which classes to use to achieve this?

The Serialization of the DTO (Data Transfer Object) should map 1:1 with the wire format, after you’ve serialized it into your DTO you can copy it into your preferred model, i.e. don’t try to project it into a different shape during serialization.

Sorry, I’m not sure I understand your point there.
I am constrained by the serialized persisted format.
My job is to get it back to the instance of the type I have.

Is there a convenient way to iterate through the properties of a generic type (e.g given TEntity)?

That way I can rebuild an new instance of the object from the persisted JSON that I have, since I have already de-serialized it into a general entity.

This DTO doesn’t look like the wire format, i.e. whatever you used to serialize the Wire Format you should use to deserialize it, that’s the purpose of DTOs - a typed model for the purpose of serialization/deserialization.

Use C#/.NET reflection as per normal, you can get the Type from typeof(TEntity).

Thanks I found a way.

If you don’t have the model you should be able to create a DTO with the shape that matches the WireFormat like:

public class MyWrapper
    public Dictionary<string,string> Properties { get; set; }
    public Guid Id { get; set; }
    public Guid RowKey { get; set; }
    public ulong RowVersion { get; set; }
    public DateTime Timestamp { get; set; }

Then deserialize it with:

var wrapper = json.FromJson<MyWrapper>();

If you don’t want to create a Typed DTO to deserialize it you would need to deserialize dynamic JSON.