Merge pull request #443 from iceljc/features/unite-json-serializer

unite json serializer
This commit is contained in:
C. Oceania 2024-05-08 11:55:28 -05:00 committed by GitHub
commit 60d82f91e9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 20 additions and 95 deletions

View file

@ -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;
@ -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;
@ -101,7 +99,6 @@ public static class BotSharpMessageParser
if (wrapperType != null && genericType != null)
{
targetType = wrapperType.MakeGenericType(genericType);
res = JsonConvert.DeserializeObject(jsonText, targetType) as ITemplateMessage;
}
}
}
@ -109,7 +106,7 @@ public static class BotSharpMessageParser
if (targetType != null)
{
res = JsonConvert.DeserializeObject(jsonText, targetType) as ITemplateMessage;
res = JsonSerializer.Deserialize(jsonText, targetType, options) as ITemplateMessage;
}
return res;

View file

@ -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;
}

View file

@ -1,10 +1,7 @@
using Newtonsoft.Json;
namespace BotSharp.Abstraction.Messaging;
public interface ITemplateMessage
{
[JsonPropertyName("template_type")]
[JsonProperty("template_type")]
string TemplateType => string.Empty;
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -1,6 +1,3 @@
using Newtonsoft.Json;
using JsonIgnoreAttribute = System.Text.Json.Serialization.JsonIgnoreAttribute;
namespace BotSharp.Abstraction.Messaging.Models.RichContent;
public class ElementAction
@ -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)]

View file

@ -1,6 +1,3 @@
using Newtonsoft.Json;
using JsonIgnoreAttribute = System.Text.Json.Serialization.JsonIgnoreAttribute;
namespace BotSharp.Abstraction.Messaging.Models.RichContent;
/// <summary>
@ -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; }

View file

@ -1,6 +1,3 @@
using Newtonsoft.Json;
using JsonIgnoreAttribute = System.Text.Json.Serialization.JsonIgnoreAttribute;
namespace BotSharp.Abstraction.Messaging.Models.RichContent;
public class QuickReplyElement
@ -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; }
}

View file

@ -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>();
}

View file

@ -1,5 +1,3 @@
using BotSharp.Abstraction.Messaging.Enums;
namespace BotSharp.Abstraction.Messaging.Models.RichContent;
public class RichContent<T> where T : IRichMessage

View file

@ -1,5 +1,3 @@
using Newtonsoft.Json;
namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;
/// <summary>
@ -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; }
}

View file

@ -1,6 +1,3 @@
using BotSharp.Abstraction.Messaging.Enums;
using Newtonsoft.Json;
namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;
/// <summary>
@ -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; }
}

View file

@ -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;
}
@ -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; }
}

View file

@ -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; }
}

View file

@ -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;
}

View file

@ -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]

View file

@ -1,10 +1,7 @@
using BotSharp.Abstraction.MLTasks;
using BotSharp.Abstraction.Options;
using BotSharp.Abstraction.Templating;
using BotSharp.Abstraction.Translation.Attributes;
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
namespace BotSharp.Core.Translation;
@ -39,10 +36,14 @@ public class TranslationService : ITranslationService
return data;
}
var cloned = data;
var clonedData = data;
if (clone)
{
cloned = Clone(data);
clonedData = Clone(data);
if (clonedData == null)
{
return data;
}
}
// chat completion
@ -52,7 +53,7 @@ public class TranslationService : ITranslationService
var template = _router.Templates.First(x => x.Name == "translation_prompt").Content;
var texts = unique.ToArray();
var translatedStringList = await InnerTranslate(JsonConvert.SerializeObject(texts), language, template);
var translatedStringList = await InnerTranslate(JsonSerializer.Serialize(texts, _options.JsonSerializerOptions), language, template);
try
{
@ -64,22 +65,22 @@ public class TranslationService : ITranslationService
map.Add(texts[i], translatedTexts[i]);
}
cloned = Assign(cloned, map);
clonedData = Assign(clonedData, map);
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
}
return cloned;
return clonedData;
}
private T Clone<T>(T data) where T : class
{
if (data == null) return data;
var str = System.Text.Json.JsonSerializer.Serialize(data, _options.JsonSerializerOptions);
var cloned = System.Text.Json.JsonSerializer.Deserialize<T>(str, _options.JsonSerializerOptions);
var str = JsonSerializer.Serialize(data, _options.JsonSerializerOptions);
var cloned = JsonSerializer.Deserialize<T>(str, _options.JsonSerializerOptions);
return cloned;
}
@ -256,7 +257,8 @@ public class TranslationService : ITranslationService
{
if (translate != null)
{
var targetValue = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(Assign(value, map)), propType);
var json = JsonSerializer.Serialize(Assign(value, map), _options.JsonSerializerOptions);
var targetValue = JsonSerializer.Deserialize(json, propType, _options.JsonSerializerOptions);
prop.SetValue(data, targetValue);
}
}

View file

@ -54,6 +54,7 @@
<ProjectReference Include="..\Plugins\BotSharp.Plugin.LLamaSharp\BotSharp.Plugin.LLamaSharp.csproj" />
<ProjectReference Include="..\Plugins\BotSharp.Plugin.SqlDriver\BotSharp.Plugin.SqlDriver.csproj" />
<ProjectReference Include="..\Plugins\BotSharp.Plugin.WebDriver\BotSharp.Plugin.WebDriver.csproj" />
<ProjectReference Include="..\Plugins\BotSharp.Plugin.AnthropicAI\BotSharp.Plugin.AnthropicAI.csproj" />
</ItemGroup>
<ItemGroup>