This repository has been archived by the owner on Feb 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from imranmomin/develop
3.0.4
- Loading branch information
Showing
3 changed files
with
82 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |