Skip to content

Commit 88664a3

Browse files
Merge pull request #429 from mzahor/fix/serialization
Fix/serialization
2 parents 6615bf7 + 8ee7f0f commit 88664a3

File tree

7 files changed

+130
-1
lines changed

7 files changed

+130
-1
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Notion.Client
4+
{
5+
public class CustomEmoji
6+
{
7+
[JsonProperty("id")]
8+
public string Id { get; set; }
9+
10+
[JsonProperty("name")]
11+
public string Name { get; set; }
12+
13+
[JsonProperty("url")]
14+
public string Url { get; set; }
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Notion.Client
4+
{
5+
public class CustomEmojiObject : IPageIcon
6+
{
7+
[JsonProperty("custom_emoji")]
8+
public CustomEmoji CustomEmoji { get; set; }
9+
10+
[JsonProperty("type")]
11+
public string Type { get; set; }
12+
}
13+
}

Src/Notion.Client/Models/Page/IPageIcon.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace Notion.Client
55
{
66
[JsonConverter(typeof(JsonSubtypes), "type")]
77
[JsonSubtypes.KnownSubTypeAttribute(typeof(EmojiObject), "emoji")]
8+
[JsonSubtypes.KnownSubTypeAttribute(typeof(CustomEmojiObject), "custom_emoji")]
89
[JsonSubtypes.KnownSubTypeAttribute(typeof(FileObject), "file")]
910
[JsonSubtypes.KnownSubTypeAttribute(typeof(FileObject), "external")]
1011
public interface IPageIcon
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using Newtonsoft.Json;
3+
4+
namespace Notion.Client
5+
{
6+
public class DateCustomConverter : JsonConverter<Date>
7+
{
8+
public override Date ReadJson(JsonReader reader, Type objectType, Date existingValue, bool hasExistingValue,
9+
JsonSerializer serializer)
10+
{
11+
var jsonObject = serializer.Deserialize<DateJsonObject>(reader);
12+
13+
if (jsonObject == null)
14+
{
15+
return null;
16+
}
17+
18+
var date = new Date
19+
{
20+
Start = ParseDateTime(jsonObject.Start, out bool includeTime),
21+
End = ParseDateTime(jsonObject.End, out _),
22+
TimeZone = jsonObject.TimeZone,
23+
IncludeTime = includeTime,
24+
};
25+
26+
return date;
27+
}
28+
29+
public override void WriteJson(JsonWriter writer, Date value, JsonSerializer serializer)
30+
{
31+
if (value is null)
32+
{
33+
writer.WriteNull();
34+
35+
return;
36+
}
37+
38+
writer.WriteStartObject();
39+
40+
if (value.Start.HasValue)
41+
{
42+
string startFormat = value.IncludeTime ? "yyyy-MM-ddTHH:mm:ss" : "yyyy-MM-dd";
43+
writer.WritePropertyName("start");
44+
writer.WriteValue(value.Start.Value.ToString(startFormat));
45+
}
46+
47+
if (value.End.HasValue)
48+
{
49+
string endFormat = value.IncludeTime ? "yyyy-MM-ddTHH:mm:ss" : "yyyy-MM-dd";
50+
writer.WritePropertyName("end");
51+
writer.WriteValue(value.End.Value.ToString(endFormat));
52+
}
53+
54+
if (!string.IsNullOrEmpty(value.TimeZone))
55+
{
56+
writer.WritePropertyName("time_zone");
57+
writer.WriteValue(value.TimeZone);
58+
}
59+
60+
writer.WriteEndObject();
61+
}
62+
63+
private static DateTime? ParseDateTime(string dateTimeString, out bool includeTime)
64+
{
65+
includeTime = false;
66+
67+
if (string.IsNullOrEmpty(dateTimeString))
68+
{
69+
return null;
70+
}
71+
72+
includeTime = dateTimeString.Contains("T") || dateTimeString.Contains(" ");
73+
74+
return DateTimeOffset.Parse(dateTimeString).UtcDateTime;
75+
}
76+
}
77+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Notion.Client
4+
{
5+
internal class DateJsonObject
6+
{
7+
[JsonProperty("start")]
8+
public string Start { get; set; }
9+
10+
[JsonProperty("end")]
11+
public string End { get; set; }
12+
13+
[JsonProperty("time_zone")]
14+
public string TimeZone { get; set; }
15+
}
16+
}

Src/Notion.Client/Models/PropertyValue/DatePropertyValue.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class DatePropertyValue : PropertyValue
2121
/// <summary>
2222
/// Date value object.
2323
/// </summary>
24+
[JsonConverter(typeof(DateCustomConverter))]
2425
public class Date
2526
{
2627
/// <summary>
@@ -43,5 +44,10 @@ public class Date
4344
/// </summary>
4445
[JsonProperty("time_zone")]
4546
public string TimeZone { get; set; }
47+
48+
/// <summary>
49+
/// Whether to include time
50+
/// </summary>
51+
public bool IncludeTime { get; set; } = true;
4652
}
4753
}

Src/Notion.Client/Models/PropertyValue/RollupPropertyValue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class RollupValue
3535
/// Date rollup property values contain a date property value.
3636
/// </summary>
3737
[JsonProperty("date")]
38-
public DatePropertyValue Date { get; set; }
38+
public Date Date { get; set; }
3939

4040
/// <summary>
4141
/// Array rollup property values contain an array of element objects.

0 commit comments

Comments
 (0)