Skip to content

Fix/serialization #429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Src/Notion.Client/Models/CustomEmoji.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Newtonsoft.Json;

namespace Notion.Client
{
public class CustomEmoji
{
[JsonProperty("id")]
public string Id { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("url")]
public string Url { get; set; }
}
}
13 changes: 13 additions & 0 deletions Src/Notion.Client/Models/CustomEmojiObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Newtonsoft.Json;

namespace Notion.Client
{
public class CustomEmojiObject : IPageIcon
{
[JsonProperty("custom_emoji")]
public CustomEmoji CustomEmoji { get; set; }

[JsonProperty("type")]
public string Type { get; set; }
}
}
1 change: 1 addition & 0 deletions Src/Notion.Client/Models/Page/IPageIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Notion.Client
{
[JsonConverter(typeof(JsonSubtypes), "type")]
[JsonSubtypes.KnownSubTypeAttribute(typeof(EmojiObject), "emoji")]
[JsonSubtypes.KnownSubTypeAttribute(typeof(CustomEmojiObject), "custom_emoji")]
[JsonSubtypes.KnownSubTypeAttribute(typeof(FileObject), "file")]
[JsonSubtypes.KnownSubTypeAttribute(typeof(FileObject), "external")]
public interface IPageIcon
Expand Down
77 changes: 77 additions & 0 deletions Src/Notion.Client/Models/PropertyValue/DateCustomConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using Newtonsoft.Json;

namespace Notion.Client
{
public class DateCustomConverter : JsonConverter<Date>
{
public override Date ReadJson(JsonReader reader, Type objectType, Date existingValue, bool hasExistingValue,
JsonSerializer serializer)
{
var jsonObject = serializer.Deserialize<DateJsonObject>(reader);

if (jsonObject == null)
{
return null;
}

var date = new Date
{
Start = ParseDateTime(jsonObject.Start, out bool includeTime),
End = ParseDateTime(jsonObject.End, out _),
TimeZone = jsonObject.TimeZone,
IncludeTime = includeTime,
};

return date;
}

public override void WriteJson(JsonWriter writer, Date value, JsonSerializer serializer)
{
if (value is null)
{
writer.WriteNull();

return;
}

writer.WriteStartObject();

if (value.Start.HasValue)
{
string startFormat = value.IncludeTime ? "yyyy-MM-ddTHH:mm:ss" : "yyyy-MM-dd";
writer.WritePropertyName("start");
writer.WriteValue(value.Start.Value.ToString(startFormat));
}

if (value.End.HasValue)
{
string endFormat = value.IncludeTime ? "yyyy-MM-ddTHH:mm:ss" : "yyyy-MM-dd";
writer.WritePropertyName("end");
writer.WriteValue(value.End.Value.ToString(endFormat));
}

if (!string.IsNullOrEmpty(value.TimeZone))
{
writer.WritePropertyName("time_zone");
writer.WriteValue(value.TimeZone);
}

writer.WriteEndObject();
}

private static DateTime? ParseDateTime(string dateTimeString, out bool includeTime)
{
includeTime = false;

if (string.IsNullOrEmpty(dateTimeString))
{
return null;
}

includeTime = dateTimeString.Contains("T") || dateTimeString.Contains(" ");

return DateTimeOffset.Parse(dateTimeString).UtcDateTime;
}
}
}
16 changes: 16 additions & 0 deletions Src/Notion.Client/Models/PropertyValue/DateJsonObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Newtonsoft.Json;

namespace Notion.Client
{
internal class DateJsonObject
{
[JsonProperty("start")]
public string Start { get; set; }

[JsonProperty("end")]
public string End { get; set; }

[JsonProperty("time_zone")]
public string TimeZone { get; set; }
}
}
6 changes: 6 additions & 0 deletions Src/Notion.Client/Models/PropertyValue/DatePropertyValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class DatePropertyValue : PropertyValue
/// <summary>
/// Date value object.
/// </summary>
[JsonConverter(typeof(DateCustomConverter))]
public class Date
{
/// <summary>
Expand All @@ -43,5 +44,10 @@ public class Date
/// </summary>
[JsonProperty("time_zone")]
public string TimeZone { get; set; }

/// <summary>
/// Whether to include time
/// </summary>
public bool IncludeTime { get; set; } = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class RollupValue
/// Date rollup property values contain a date property value.
/// </summary>
[JsonProperty("date")]
public DatePropertyValue Date { get; set; }
public Date Date { get; set; }

/// <summary>
/// Array rollup property values contain an array of element objects.
Expand Down
Loading