Skip to content
This repository has been archived by the owner on Feb 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #49 from imranmomin/develop
Browse files Browse the repository at this point in the history
3.0.4
  • Loading branch information
imranmomin committed Nov 22, 2019
2 parents 7d6b01c + 51c7f15 commit ce27098
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 8 deletions.
8 changes: 3 additions & 5 deletions Hangfire.AzureDocumentDB/DocumentDbStorage.cs
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions Hangfire.AzureDocumentDB/Hangfire.AzureDocumentDB.csproj
Expand Up @@ -62,15 +62,15 @@
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Hangfire.Core" Version="1.7.3" />
<PackageReference Include="Hangfire.Core" Version="1.7.7" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="2.4.0" />
<PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="2.9.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net461'">
<PackageReference Include="Microsoft.Azure.DocumentDB" Version="2.4.0" />
<PackageReference Include="Microsoft.Azure.DocumentDB" Version="2.9.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
Expand Down
76 changes: 76 additions & 0 deletions 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<int>())
{
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;
}
}
}

0 comments on commit ce27098

Please sign in to comment.