-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'release/0.9.0' into main
- Loading branch information
Showing
3 changed files
with
144 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace ZoomNet.Utilities | ||
{ | ||
/// <summary> | ||
/// Converts a JSON string into and array of tracking fields. | ||
/// </summary> | ||
/// <seealso cref="Newtonsoft.Json.JsonConverter" /> | ||
internal class TrackingFieldsConverter : JsonConverter | ||
{ | ||
/// <summary> | ||
/// Determines whether this instance can convert the specified object type. | ||
/// </summary> | ||
/// <param name="objectType">Type of the object.</param> | ||
/// <returns> | ||
/// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>. | ||
/// </returns> | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return objectType == typeof(IEnumerable<KeyValuePair<string, string>>); | ||
} | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether this <see cref="T:Newtonsoft.Json.JsonConverter" /> can read JSON. | ||
/// </summary> | ||
/// <value> | ||
/// <c>true</c> if this <see cref="T:Newtonsoft.Json.JsonConverter" /> can read JSON; otherwise, <c>false</c>. | ||
/// </value> | ||
public override bool CanRead | ||
{ | ||
get { return true; } | ||
} | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether this <see cref="T:Newtonsoft.Json.JsonConverter" /> can write JSON. | ||
/// </summary> | ||
/// <value> | ||
/// <c>true</c> if this <see cref="T:Newtonsoft.Json.JsonConverter" /> can write JSON; otherwise, <c>false</c>. | ||
/// </value> | ||
public override bool CanWrite | ||
{ | ||
get { return false; } | ||
} | ||
|
||
/// <summary> | ||
/// Writes the JSON representation of the object. | ||
/// </summary> | ||
/// <param name="writer">The <see cref="T:Newtonsoft.Json.JsonWriter" /> to write to.</param> | ||
/// <param name="value">The value.</param> | ||
/// <param name="serializer">The calling serializer.</param> | ||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
/// <summary> | ||
/// Reads the JSON representation of the object. | ||
/// </summary> | ||
/// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader" /> to read from.</param> | ||
/// <param name="objectType">Type of the object.</param> | ||
/// <param name="existingValue">The existing value of object being read.</param> | ||
/// <param name="serializer">The calling serializer.</param> | ||
/// <returns> | ||
/// The object value. | ||
/// </returns> | ||
/// <exception cref="System.Exception">Unable to determine the field type.</exception> | ||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
if (reader.TokenType == JsonToken.StartArray) | ||
{ | ||
var jArray = JArray.Load(reader); | ||
var items = jArray | ||
.OfType<JObject>() | ||
.Select(item => Convert(item, serializer)) | ||
.Where(item => !string.IsNullOrEmpty(item.Key)) | ||
.ToArray(); | ||
|
||
return items; | ||
} | ||
|
||
throw new Exception("Unable to convert to tracking fields"); | ||
} | ||
|
||
private KeyValuePair<string, string> Convert(JObject jsonObject, JsonSerializer serializer) | ||
{ | ||
var fieldName = jsonObject.Value<string>("field"); | ||
var fieldValue = jsonObject.Value<string>("value"); | ||
|
||
return new KeyValuePair<string, string>(fieldName, fieldValue); | ||
} | ||
} | ||
} |
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