diff --git a/Hangfire.AzureDocumentDB/DocumentDbStorage.cs b/Hangfire.AzureDocumentDB/DocumentDbStorage.cs index 0cac1d0..54a352d 100644 --- a/Hangfire.AzureDocumentDB/DocumentDbStorage.cs +++ b/Hangfire.AzureDocumentDB/DocumentDbStorage.cs @@ -10,11 +10,12 @@ using Hangfire.Logging; using Newtonsoft.Json; using Microsoft.Azure.Documents; -using Newtonsoft.Json.Serialization; using Microsoft.Azure.Documents.Client; using Hangfire.Azure.Queue; using Hangfire.Azure.Helper; +using Hangfire.Azure.Documents.Json; + namespace Hangfire.Azure { @@ -50,10 +51,7 @@ public DocumentDbStorage(string url, string authSecret, string database, string { NullValueHandling = NullValueHandling.Ignore, DateTimeZoneHandling = DateTimeZoneHandling.Utc, - ContractResolver = new CamelCasePropertyNamesContractResolver - { - NamingStrategy = new CamelCaseNamingStrategy(false, false) - } + ContractResolver = new DocumentContractResolver() }; ConnectionPolicy connectionPolicy = ConnectionPolicy.Default; diff --git a/Hangfire.AzureDocumentDB/Hangfire.AzureDocumentDB.csproj b/Hangfire.AzureDocumentDB/Hangfire.AzureDocumentDB.csproj index 6d8e3db..0325608 100644 --- a/Hangfire.AzureDocumentDB/Hangfire.AzureDocumentDB.csproj +++ b/Hangfire.AzureDocumentDB/Hangfire.AzureDocumentDB.csproj @@ -62,15 +62,15 @@ - + - + - + diff --git a/Hangfire.AzureDocumentDB/Json/DocumentContractResolver.cs b/Hangfire.AzureDocumentDB/Json/DocumentContractResolver.cs new file mode 100644 index 0000000..9af4ac7 --- /dev/null +++ b/Hangfire.AzureDocumentDB/Json/DocumentContractResolver.cs @@ -0,0 +1,76 @@ +using System; + +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Newtonsoft.Json.Serialization; + +using Hangfire.Azure.Documents; + +namespace Hangfire.Azure.Documents.Json +{ + internal class DocumentContractResolver : CamelCasePropertyNamesContractResolver + { + public DocumentContractResolver() + { + NamingStrategy = new CamelCaseNamingStrategy(false, false); + } + + protected override JsonContract CreateContract(Type objectType) + { + JsonContract contract = base.CreateContract(objectType); + if (objectType != typeof(DocumentBase)) return contract; + contract.Converter = new DocumentConverter(); + return contract; + } + } + + internal class DocumentConverter : JsonConverter + { + public override bool CanWrite => false; + public override bool CanRead => true; + public override bool CanConvert(Type objectType) => objectType == typeof(DocumentBase); + + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) => throw new InvalidOperationException("Use default serialization."); + + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + JObject jsonObject = JObject.Load(reader); + DocumentBase document; + + switch (jsonObject["type"].Value()) + { + case (int)DocumentTypes.Counter: + document = new Counter(); + break; + case (int)DocumentTypes.Hash: + document = new Hash(); + break; + case (int)DocumentTypes.Job: + document = new Job(); + break; + case (int)DocumentTypes.List: + document = new List(); + break; + case (int)DocumentTypes.Lock: + document = new Lock(); + break; + case (int)DocumentTypes.Queue: + document = new Queue(); + break; + case (int)DocumentTypes.Server: + document = new Server(); + break; + case (int)DocumentTypes.Set: + document = new Set(); + break; + case (int)DocumentTypes.State: + document = new State(); + break; + default: throw new ArgumentOutOfRangeException(); + } + + serializer.Populate(jsonObject.CreateReader(), document); + return document; + } + } +} \ No newline at end of file