Skip to content

unite json serializer #443

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 3 commits into from
May 8, 2024
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
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
using BotSharp.Abstraction.Messaging.Enums;
using BotSharp.Abstraction.Messaging.Models.RichContent.Template;
using BotSharp.Abstraction.Messaging.Models.RichContent;
using System.Text.Json;
using System.Reflection;
using Newtonsoft.Json;

namespace BotSharp.Abstraction.Messaging;

public static class BotSharpMessageParser
{

public static IRichMessage? ParseRichMessage(JsonElement root)
public static IRichMessage? ParseRichMessage(JsonElement root, JsonSerializerOptions options)
{
IRichMessage? res = null;
Type? targetType = null;
Expand Down Expand Up @@ -58,13 +56,13 @@ public static class BotSharpMessageParser

if (targetType != null)
{
res = JsonConvert.DeserializeObject(jsonText, targetType) as IRichMessage;
res = JsonSerializer.Deserialize(jsonText, targetType, options) as IRichMessage;
}

return res;
}

public static ITemplateMessage? ParseTemplateMessage(JsonElement root)
public static ITemplateMessage? ParseTemplateMessage(JsonElement root, JsonSerializerOptions options)
{
ITemplateMessage? res = null;
Type? targetType = null;
Expand Down Expand Up @@ -101,15 +99,14 @@ public static class BotSharpMessageParser
if (wrapperType != null && genericType != null)
{
targetType = wrapperType.MakeGenericType(genericType);
res = JsonConvert.DeserializeObject(jsonText, targetType) as ITemplateMessage;
}
}
}
}

if (targetType != null)
{
res = JsonConvert.DeserializeObject(jsonText, targetType) as ITemplateMessage;
res = JsonSerializer.Deserialize(jsonText, targetType, options) as ITemplateMessage;
}

return res;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
using BotSharp.Abstraction.Messaging.Enums;
using Newtonsoft.Json;

namespace BotSharp.Abstraction.Messaging;

public interface IRichMessage
{
[JsonPropertyName("text")]
[JsonProperty("text")]
string Text { get; set; }

[JsonPropertyName("rich_type")]
[JsonProperty("rich_type")]
string RichType => RichTypeEnum.Text;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using Newtonsoft.Json;

namespace BotSharp.Abstraction.Messaging;

public interface ITemplateMessage
{
[JsonPropertyName("template_type")]
[JsonProperty("template_type")]
string TemplateType => string.Empty;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class RichContentJsonConverter : JsonConverter<IRichMessage>
{
using var jsonDoc = JsonDocument.ParseValue(ref reader);
var root = jsonDoc.RootElement;
var res = BotSharpMessageParser.ParseRichMessage(root);
var res = BotSharpMessageParser.ParseRichMessage(root, options);
return res;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class TemplateMessageJsonConverter : JsonConverter<ITemplateMessage>
{
using var jsonDoc = JsonDocument.ParseValue(ref reader);
var root = jsonDoc.RootElement;
var res = BotSharpMessageParser.ParseTemplateMessage(root);
var res = BotSharpMessageParser.ParseTemplateMessage(root, options);
return res;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using Newtonsoft.Json;
using JsonIgnoreAttribute = System.Text.Json.Serialization.JsonIgnoreAttribute;

namespace BotSharp.Abstraction.Messaging.Models.RichContent;

public class ElementAction
Expand All @@ -11,7 +8,6 @@ public class ElementAction
public string Url { get; set; }

[JsonPropertyName("webview_height_ratio")]
[JsonProperty("webview_height_ratio")]
public string WebViewHeightRatio { get; set; }

[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using Newtonsoft.Json;
using JsonIgnoreAttribute = System.Text.Json.Serialization.JsonIgnoreAttribute;

namespace BotSharp.Abstraction.Messaging.Models.RichContent;

/// <summary>
Expand All @@ -20,15 +17,12 @@ public class ElementButton
public string Payload { get; set; }

[JsonPropertyName("is_primary")]
[JsonProperty("is_primary")]
public bool IsPrimary { get; set; }

[JsonPropertyName("is_secondary")]
[JsonProperty("is_secondary")]
public bool IsSecondary { get; set; }

[JsonPropertyName("post_action_disclaimer")]
[JsonProperty("post_action_disclaimer")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[Translate]
public string? PostActionDisclaimer { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using Newtonsoft.Json;
using JsonIgnoreAttribute = System.Text.Json.Serialization.JsonIgnoreAttribute;

namespace BotSharp.Abstraction.Messaging.Models.RichContent;

public class QuickReplyElement
Expand All @@ -12,7 +9,6 @@ public class QuickReplyElement
public string? Payload { get; set; }

[JsonPropertyName("image_url")]
[JsonProperty("image_url")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? ImageUrl { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
using BotSharp.Abstraction.Messaging.Enums;
using Newtonsoft.Json;

namespace BotSharp.Abstraction.Messaging.Models.RichContent;

public class QuickReplyMessage : IRichMessage
{
[JsonPropertyName("rich_type")]
[JsonProperty("rich_type")]
public string RichType => RichTypeEnum.QuickReply;
public string Text { get; set; } = string.Empty;

[JsonPropertyName("quick_replies")]
[JsonProperty("quick_replies")]
public List<QuickReplyElement> QuickReplies { get; set; } = new List<QuickReplyElement>();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using BotSharp.Abstraction.Messaging.Enums;

namespace BotSharp.Abstraction.Messaging.Models.RichContent;

public class RichContent<T> where T : IRichMessage
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Newtonsoft.Json;

namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;

/// <summary>
Expand All @@ -8,23 +6,18 @@ namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;
public class ButtonTemplateMessage : IRichMessage, ITemplateMessage
{
[JsonPropertyName("rich_type")]
[JsonProperty("rich_type")]
public string RichType => RichTypeEnum.ButtonTemplate;

[JsonPropertyName("text")]
[JsonProperty("text")]
[Translate]
public string Text { get; set; } = string.Empty;

[JsonPropertyName("template_type")]
[JsonProperty("template_type")]
public string TemplateType => TemplateTypeEnum.Button;

[JsonPropertyName("buttons")]
[JsonProperty("buttons")]
public ElementButton[] Buttons { get; set; } = new ElementButton[0];

[JsonPropertyName("is_horizontal")]
[JsonProperty("is_horizontal")]
public bool IsHorizontal { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using BotSharp.Abstraction.Messaging.Enums;
using Newtonsoft.Json;

namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;

/// <summary>
Expand All @@ -10,40 +7,31 @@ namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;
public class CouponTemplateMessage : IRichMessage, ITemplateMessage
{
[JsonPropertyName("rich_type")]
[JsonProperty("rich_type")]
public string RichType => RichTypeEnum.CouponTemplate;

[JsonPropertyName("text")]
[JsonProperty("text")]
public string Text { get; set; }
public string Title { get; set; }
public string Subtitle { get; set; }

[JsonPropertyName("template_type")]
[JsonProperty("template_type")]
public string TemplateType => TemplateTypeEnum.Coupon;

[JsonPropertyName("coupon_code")]
[JsonProperty("coupon_code")]
public string CouponCode { get; set; }

[JsonPropertyName("coupon_url")]
[JsonProperty("coupon_url")]
public string CouponUrl { get; set; }

[JsonPropertyName("coupon_url_button_title")]
[JsonProperty("coupon_url_button_title")]
public string CouponUrlButtonTitle { get; set; } = "Shop now";

[JsonPropertyName("coupon_pre_message")]
[JsonProperty("coupon_pre_message")]
public string CouponPreMessage { get; set; } = "Here's a deal just for you!";

[JsonPropertyName("image_url")]
[JsonProperty("image_url")]
public string ImageUrl { get; set; }

[JsonPropertyName("payload")]
[JsonProperty("payload")]
public string Payload { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,27 @@
using BotSharp.Abstraction.Messaging.Enums;
using Newtonsoft.Json;

namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;

public class GenericTemplateMessage<T> : IRichMessage, ITemplateMessage
{
[JsonPropertyName("rich_type")]
[JsonProperty("rich_type")]
public string RichType => RichTypeEnum.GenericTemplate;

[JsonPropertyName("text")]
[JsonProperty("text")]
[Translate]
public string Text { get; set; } = string.Empty;

[JsonPropertyName("template_type")]
[JsonProperty("template_type")]
public virtual string TemplateType { get; set; } = TemplateTypeEnum.Generic;

[JsonPropertyName("elements")]
[JsonProperty("elements")]
public List<T> Elements { get; set; } = new List<T>();

[JsonPropertyName("is_horizontal")]
[JsonProperty("is_horizontal")]
public bool IsHorizontal { get; set; }

[JsonPropertyName("is_popup")]
[JsonProperty("is_popup")]
public bool IsPopup { get; set; }

[JsonPropertyName("element_type")]
[JsonProperty("element_type")]
public string ElementType => typeof(T).Name;
}

Expand All @@ -44,11 +34,9 @@ public class GenericElement
public string Subtitle { get; set; }

[JsonPropertyName("image_url")]
[JsonProperty("image_url")]
public string ImageUrl { get; set; }

[JsonPropertyName("default_action")]
[JsonProperty("default_action")]
public ElementAction DefaultAction { get; set; }
public ElementButton[] Buttons { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
using Newtonsoft.Json;

namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;

public class MultiSelectTemplateMessage : IRichMessage, ITemplateMessage
{
[JsonPropertyName("rich_type")]
[JsonProperty("rich_type")]
public string RichType => RichTypeEnum.MultiSelectTemplate;

[JsonPropertyName("text")]
[JsonProperty("text")]
[Translate]
public string Text { get; set; } = string.Empty;

[JsonPropertyName("template_type")]
[JsonProperty("template_type")]
public string TemplateType => TemplateTypeEnum.MultiSelect;

[JsonPropertyName("options")]
[JsonProperty("options")]
public List<OptionElement> Options { get; set; } = new List<OptionElement>();

[JsonPropertyName("is_horizontal")]
[JsonProperty("is_horizontal")]
public bool IsHorizontal { get; set; }
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
using Newtonsoft.Json;

namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;

public class ProductTemplateMessage : IRichMessage, ITemplateMessage
{
[JsonPropertyName("rich_type")]
[JsonProperty("rich_type")]
public string RichType => RichTypeEnum.GenericTemplate;

[JsonPropertyName("text")]
[JsonProperty("text")]
[Translate]
public string Text { get; set; } = string.Empty;

[JsonPropertyName("template_type")]
[JsonProperty("template_type")]
public string TemplateType => TemplateTypeEnum.Product;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using Newtonsoft.Json;

namespace BotSharp.Abstraction.Messaging.Models.RichContent;

public class TextMessage : IRichMessage
{
[JsonPropertyName("rich_type")]
[JsonProperty("rich_type")]
public string RichType => RichTypeEnum.Text;

[Translate]
Expand Down
Loading