Skip to content

Commit

Permalink
Merge pull request #8 from JSkimming/json-serialize
Browse files Browse the repository at this point in the history
Ensure the objects correctly serialize as JSON
  • Loading branch information
JSkimming committed Aug 15, 2015
2 parents 54f2118 + 917b105 commit 87525a6
Show file tree
Hide file tree
Showing 16 changed files with 382 additions and 65 deletions.
4 changes: 4 additions & 0 deletions src/CloudFlare.NET/CloudFlare.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@
<Compile Include="IdentifierTag.cs" />
<Compile Include="IdentifierTag.IEquatable.cs" />
<Compile Include="IDnsRecordClient.cs" />
<Compile Include="IIdentifier.cs" />
<Compile Include="IModified.cs" />
<Compile Include="IZoneClient.cs" />
<Compile Include="Serialization\IsoDateTimeOffsetConverter.cs" />
<Compile Include="Serialization\ToStringJsonConverter.cs" />
<Compile Include="Zone.cs" />
<Compile Include="ZoneClientExtensions.cs" />
<None Include="app.config" />
Expand Down
34 changes: 16 additions & 18 deletions src/CloudFlare.NET/DnsRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,29 @@
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;

/// <summary>
/// Represents a DNS record for a <see cref="Zone"/>.
/// </summary>
/// <seealso href="https://api.cloudflare.com/#dns-records-for-a-zone-properties"/>
public class DnsRecord
public class DnsRecord : IIdentifier, IModified
{
/// <summary>
/// Initializes a new instance of the <see cref="DnsRecord"/> class.
/// </summary>
public DnsRecord(
IdentifierTag id,
DateTimeOffset? createdOn,
DateTimeOffset modifiedOn,
DnsRecordType type,
string name,
bool proxiable,
bool proxied,
int ttl,
bool locked,
IdentifierTag zoneId,
DateTimeOffset? createdOn,
DateTimeOffset modifiedOn,
int priority,
string content = null,
string zoneName = null,
Expand All @@ -40,6 +41,8 @@ public DnsRecord(
throw new ArgumentNullException(nameof(zoneId));

Id = id;
CreatedOn = createdOn;
ModifiedOn = modifiedOn;
Type = type;
Name = name;
Content = content ?? string.Empty;
Expand All @@ -49,8 +52,6 @@ public DnsRecord(
Locked = locked;
ZoneId = zoneId;
ZoneName = zoneName ?? string.Empty;
CreatedOn = createdOn;
ModifiedOn = modifiedOn;
Data = data ?? new JObject();
Meta = meta ?? new JObject();
Priority = priority;
Expand All @@ -59,9 +60,18 @@ public DnsRecord(
/// <summary>
/// API item identifier tag.
/// </summary>
[JsonProperty("id")]
public IdentifierTag Id { get; }

/// <summary>
/// When the record was created.
/// </summary>
public DateTimeOffset? CreatedOn { get; }

/// <summary>
/// When the record was last modified.
/// </summary>
public DateTimeOffset ModifiedOn { get; }

/// <summary>
/// Record type.
/// </summary>
Expand Down Expand Up @@ -116,18 +126,6 @@ public DnsRecord(
[JsonProperty("zone_name")]
public string ZoneName { get; }

/// <summary>
/// When the record was created.
/// </summary>
[JsonProperty("created_on")]
public DateTimeOffset? CreatedOn { get; }

/// <summary>
/// When the record was last modified.
/// </summary>
[JsonProperty("modified_on")]
public DateTimeOffset ModifiedOn { get; }

/// <summary>
/// Metadata about the record.
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions src/CloudFlare.NET/DnsRecordType.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace CloudFlare.NET
{
using System.Diagnostics.CodeAnalysis;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

// ReSharper disable InconsistentNaming
#pragma warning disable 1591
Expand All @@ -10,6 +12,7 @@
/// </summary>
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1602:EnumerationItemsMustBeDocumented",
Justification = "Names a self-explanatory.")]
[JsonConverter(typeof(StringEnumConverter))]
public enum DnsRecordType
{
A,
Expand Down
16 changes: 16 additions & 0 deletions src/CloudFlare.NET/IIdentifier.cs
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; }
}
}
26 changes: 26 additions & 0 deletions src/CloudFlare.NET/IModified.cs
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; }
}
}
3 changes: 3 additions & 0 deletions src/CloudFlare.NET/IdentifierTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using CloudFlare.NET.Serialization;
using Newtonsoft.Json;

/// <summary>
/// A CloudFlare Identifier.
/// </summary>
[DebuggerDisplay("{_id}")]
[JsonConverter(typeof(ToStringJsonConverter))]
public partial class IdentifierTag
{
/// <summary>
Expand Down
31 changes: 31 additions & 0 deletions src/CloudFlare.NET/Serialization/IsoDateTimeOffsetConverter.cs
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);
}
}
}
}
53 changes: 53 additions & 0 deletions src/CloudFlare.NET/Serialization/ToStringJsonConverter.cs
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();
}
}
}
34 changes: 16 additions & 18 deletions src/CloudFlare.NET/Zone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using CloudFlare.NET.Serialization;
using Newtonsoft.Json;

/// <summary>
/// A Zone is a domain name along with its subdomains and other identities.
/// </summary>
/// <seealso href="https://api.cloudflare.com/#zone-properties"/>
public class Zone
public class Zone : IIdentifier, IModified
{
private static readonly IReadOnlyList<string> EmptyStrings = Enumerable.Empty<string>().ToArray();

Expand All @@ -18,10 +19,10 @@ public class Zone
/// </summary>
public Zone(
IdentifierTag id,
string name,
int developmentMode,
DateTimeOffset? createdOn,
DateTimeOffset modifiedOn,
string name,
int developmentMode,
IReadOnlyList<string> originalNameServers = null,
string originalRegistrar = null,
string originalDnshost = null,
Expand All @@ -33,22 +34,31 @@ public Zone(
throw new ArgumentNullException(nameof(name));

Id = id;
CreatedOn = createdOn;
ModifiedOn = modifiedOn;
Name = name;
DevelopmentMode = developmentMode;
OriginalNameServers = originalNameServers ?? EmptyStrings;
OriginalRegistrar = originalRegistrar ?? string.Empty;
OriginalDnshost = originalDnshost ?? string.Empty;
CreatedOn = createdOn;
ModifiedOn = modifiedOn;
NameServers = nameServers ?? EmptyStrings;
}

/// <summary>
/// API item identifier tag.
/// </summary>
[JsonProperty("id")]
public IdentifierTag Id { get; }

/// <summary>
/// When the zone was created.
/// </summary>
public DateTimeOffset? CreatedOn { get; }

/// <summary>
/// When the zone was last modified.
/// </summary>
public DateTimeOffset ModifiedOn { get; }

/// <summary>
/// The domain name.
/// </summary>
Expand Down Expand Up @@ -80,18 +90,6 @@ public Zone(
[JsonProperty("original_dnshost")]
public string OriginalDnshost { get; }

/// <summary>
/// When the zone was created.
/// </summary>
[JsonProperty("created_on")]
public DateTimeOffset? CreatedOn { get; }

/// <summary>
/// When the zone was last modified.
/// </summary>
[JsonProperty("modified_on")]
public DateTimeOffset ModifiedOn { get; }

/// <summary>
/// CloudFlare-assigned name servers. This is only populated for zones that use CloudFlare DNS.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/Tests/CloudFlare.NET.Tests/CloudFlare.NET.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
<Compile Include="LikenessExtensions.cs" />
<Compile Include="RequiresArgNullEx.cs" />
<Compile Include="RequiresArgNullExAutoMoqAttribute.cs" />
<Compile Include="Serialization\BehaviorTemplates.cs" />
<Compile Include="ZoneClientExtensionsSpec.cs" />
<Compile Include="Serialization\DnsRecordSerializationSpec.cs" />
<Compile Include="Serialization\ErrorResponseSerializationSpec.cs" />
Expand Down
Binary file not shown.
4 changes: 4 additions & 0 deletions src/Tests/CloudFlare.NET.Tests/CloudFlareCustomization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;
using Ploeh.AutoFixture;
using Ploeh.AutoFixture.AutoMoq;

Expand All @@ -19,6 +20,9 @@ internal class FixtureCustomization : ICustomization
void ICustomization.Customize(IFixture fixture)
{
fixture.Register(() => new IdentifierTag(Guid.NewGuid().ToString("N")));
fixture.Register<IReadOnlyList<string>>(fixture.Create<string[]>);
fixture.Register<IReadOnlyList<CloudFlareError>>(fixture.Create<CloudFlareError[]>);
fixture.Register(() => JObject.FromObject(fixture.Create<CloudFlareResponseBase>()));
}
}
}
Loading

0 comments on commit 87525a6

Please sign in to comment.