-
Notifications
You must be signed in to change notification settings - Fork 13
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 #8 from JSkimming/json-serialize
Ensure the objects correctly serialize as JSON
- Loading branch information
Showing
16 changed files
with
382 additions
and
65 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
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,16 @@ | ||
namespace CloudFlare.NET | ||
{ | ||
using Newtonsoft.Json; | ||
|
||
/// <summary> | ||
/// Defines an entity that has a unique <see cref="Id"/>. | ||
/// </summary> | ||
public interface IIdentifier | ||
{ | ||
/// <summary> | ||
/// API item identifier tag. | ||
/// </summary> | ||
[JsonProperty("id")] | ||
IdentifierTag Id { get; } | ||
} | ||
} |
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,26 @@ | ||
namespace CloudFlare.NET | ||
{ | ||
using System; | ||
using CloudFlare.NET.Serialization; | ||
using Newtonsoft.Json; | ||
|
||
/// <summary> | ||
/// Defines the <see cref="CreatedOn"/> and <see cref="ModifiedOn"/> properies of an entity. | ||
/// </summary> | ||
public interface IModified | ||
{ | ||
/// <summary> | ||
/// When the entity was created. | ||
/// </summary> | ||
[JsonProperty("created_on")] | ||
[JsonConverter(typeof(IsoDateTimeOffsetConverter))] | ||
DateTimeOffset? CreatedOn { get; } | ||
|
||
/// <summary> | ||
/// When the entity was last modified. | ||
/// </summary> | ||
[JsonProperty("modified_on")] | ||
[JsonConverter(typeof(IsoDateTimeOffsetConverter))] | ||
DateTimeOffset ModifiedOn { get; } | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
src/CloudFlare.NET/Serialization/IsoDateTimeOffsetConverter.cs
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,31 @@ | ||
namespace CloudFlare.NET.Serialization | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Converters; | ||
|
||
internal class IsoDateTimeOffsetConverter : IsoDateTimeConverter | ||
{ | ||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
if (writer == null) | ||
throw new ArgumentNullException(nameof(writer)); | ||
if (value == null) | ||
throw new ArgumentNullException(nameof(value)); | ||
if (serializer == null) | ||
throw new ArgumentNullException(nameof(serializer)); | ||
|
||
if (value is DateTimeOffset) | ||
{ | ||
DateTimeOffset dateTimeOffset = (DateTimeOffset)value; | ||
base.WriteJson(writer, dateTimeOffset.UtcDateTime, serializer); | ||
} | ||
else | ||
{ | ||
base.WriteJson(writer, value, serializer); | ||
} | ||
} | ||
} | ||
} |
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,53 @@ | ||
namespace CloudFlare.NET.Serialization | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Newtonsoft.Json; | ||
|
||
/// <summary> | ||
/// <seealso href="http://stackoverflow.com/a/22355712"/> | ||
/// </summary> | ||
internal class ToStringJsonConverter : JsonConverter | ||
{ | ||
public override bool CanRead => false; | ||
|
||
public override bool CanConvert(Type objectType) | ||
{ | ||
if (objectType == null) | ||
throw new ArgumentNullException(nameof(objectType)); | ||
|
||
return true; | ||
} | ||
|
||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
if (writer == null) | ||
throw new ArgumentNullException(nameof(writer)); | ||
if (value == null) | ||
throw new ArgumentNullException(nameof(value)); | ||
if (serializer == null) | ||
throw new ArgumentNullException(nameof(serializer)); | ||
|
||
writer.WriteValue(value.ToString()); | ||
} | ||
|
||
public override object ReadJson( | ||
JsonReader reader, | ||
Type objectType, | ||
object existingValue, | ||
JsonSerializer serializer) | ||
{ | ||
if (reader == null) | ||
throw new ArgumentNullException(nameof(reader)); | ||
if (objectType == null) | ||
throw new ArgumentNullException(nameof(objectType)); | ||
if (existingValue == null) | ||
throw new ArgumentNullException(nameof(existingValue)); | ||
if (serializer == null) | ||
throw new ArgumentNullException(nameof(serializer)); | ||
|
||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
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
Binary file modified
BIN
-2 Bytes
(100%)
src/Tests/CloudFlare.NET.Tests/CloudFlare.NET.Tests.v2.ncrunchproject
Binary file not shown.
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
Oops, something went wrong.