Skip to content

Commit e8d1ee4

Browse files
author
David Edgar
committed
ANnotations for JSON serialisation to ensure the json is serialised in the manner the API expects
NULL values should be ignored. Enums must be passed as their string name, not that int value. DateTimes should be nullable so that DateTime.MinValue is not sent, which would be bad for the expiry time.
1 parent c833802 commit e8d1ee4

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed
Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,78 @@
11
using System;
22
using System.Collections.Generic;
33
using Newtonsoft.Json;
4+
using Newtonsoft.Json.Converters;
45

56
namespace MessageMedia.Messages.Models
67
{
78
public struct Message
89
{
9-
[JsonProperty("message_id")] public string MessageId { get; set; }
10+
[JsonProperty("message_id", NullValueHandling = NullValueHandling.Ignore)] public string MessageId { get; set; }
1011

1112
/// <summary>
1213
/// Urls of the media files to send in the Message
1314
///
1415
/// <remarks>Only valid if the Format is MMS</remarks>
1516
/// </summary>
16-
[JsonProperty("media")] public string[] Media { get; set; }
17+
[JsonProperty("media", NullValueHandling = NullValueHandling.Ignore)] public string[] Media { get; set; }
1718

1819
/// <summary>
1920
/// Subject of the Message
2021
///
2122
/// <remarks>Only valid if the Format is MMS</remarks>
2223
/// </summary>
23-
[JsonProperty("subject")] public string Subject { get; set; }
24+
[JsonProperty("subject", NullValueHandling = NullValueHandling.Ignore)] public string Subject { get; set; }
2425

25-
[JsonProperty("status")] public MessageStatus Status { get; set; }
26+
[JsonProperty("status", NullValueHandling = NullValueHandling.Ignore)]
27+
[JsonConverter(typeof(StringEnumConverter))]
28+
public MessageStatus? Status { get; set; }
2629

2730
/// <summary>
2831
/// Replies and delivery reports for this message will be pushed to the URL"
2932
/// </summary>
30-
[JsonProperty("callback_url")] public string CallbackUrl { get; set; }
33+
[JsonProperty("callback_url", NullValueHandling = NullValueHandling.Ignore)] public string CallbackUrl { get; set; }
3134

3235
/// <summary>
3336
/// Content of the message
3437
/// <example>Hello world!</example>
3538
/// </summary>
36-
[JsonProperty("content")] public string Content { get; set; }
39+
[JsonProperty("content", NullValueHandling = NullValueHandling.Ignore)] public string Content { get; set; }
3740

3841
/// <summary>
3942
/// Destination number of the message
4043
/// <example>+61491570156</example>
4144
/// </summary>
42-
[JsonProperty("destination_number")] public string DestinationNumber { get; set; }
45+
[JsonProperty("destination_number", NullValueHandling = NullValueHandling.Ignore)] public string DestinationNumber { get; set; }
4346

4447
/// <summary>
4548
/// Request a delivery report for this message
4649
/// </summary>
47-
[JsonProperty("delivery_report")] public bool DeliveryReport { get; set; }
50+
[JsonProperty("delivery_report", NullValueHandling = NullValueHandling.Ignore)] public bool DeliveryReport { get; set; }
4851

4952
/// <summary>
5053
/// Format of message, SMS or TTS (Text To Speech).
5154
/// </summary>
52-
[JsonProperty("format")] public MessageFormat Format { get; set; }
55+
[JsonProperty("format", NullValueHandling = NullValueHandling.Ignore)]
56+
[JsonConverter(typeof(StringEnumConverter))]
57+
public MessageFormat Format { get; set; }
5358

5459
/// <summary>
5560
/// Date time after which the message expires and will not be sent
5661
/// </summary>
57-
[JsonProperty("message_expiry_timestamp")]
58-
public DateTime MessageExpiryTimestamp { get; set; }
62+
[JsonProperty("message_expiry_timestamp", NullValueHandling = NullValueHandling.Ignore)]
63+
public DateTime? MessageExpiryTimestamp { get; set; }
5964

6065
/// <summary>
6166
/// Metadata for the message specified as a set of key value pairs.
6267
///
6368
/// <remarks>Each key can be up to 100 characters long and each value can be up to 256 characters long.</remarks>
6469
/// </summary>
65-
[JsonProperty("metadata")] public Dictionary<string, string> Metadata { get; set; }
70+
[JsonProperty("metadata", NullValueHandling = NullValueHandling.Ignore)] public Dictionary<string, string> Metadata { get; set; }
6671

6772
/// <summary>
6873
/// Scheduled delivery date time of the message
6974
/// </summary>
70-
[JsonProperty("scheduled")] public DateTime Scheduled { get; set; }
75+
[JsonProperty("scheduled", NullValueHandling = NullValueHandling.Ignore)] public DateTime? Scheduled { get; set; }
7176

7277
/// <summary>
7378
/// Source of the message
@@ -77,11 +82,13 @@ public struct Message
7782
///
7883
/// <remarks>By default this feature is not available and will be ignored in the request. Please contact [email protected] for more information. Specifying a source number is optional and a by default a source number will be assigned to the message.</remarks>
7984
/// </summary>
80-
[JsonProperty("source_number")] public string SourceNumber { get; set; }
85+
[JsonProperty("source_number", NullValueHandling = NullValueHandling.Ignore)] public string SourceNumber { get; set; }
8186

8287
/// <summary>
8388
/// Type of source address specified
8489
/// </summary>
85-
[JsonProperty("source_number_type")] public NumberType SourceNumberType { get; set; }
90+
[JsonProperty("source_number_type", NullValueHandling = NullValueHandling.Ignore)]
91+
[JsonConverter(typeof(StringEnumConverter))]
92+
public NumberType SourceNumberType { get; set; }
8693
}
8794
}

0 commit comments

Comments
 (0)