Merge pull request #385 from iceljc/features/refine-json-converter

refine json converter
This commit is contained in:
C. Oceania 2024-04-02 06:02:14 -05:00 committed by GitHub
commit 6f93347b08
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 93 additions and 94 deletions

View file

@ -10,15 +10,7 @@ public class RichContentJsonConverter : JsonConverter<IRichMessage>
using var jsonDoc = JsonDocument.ParseValue(ref reader);
var root = jsonDoc.RootElement;
var jsonText = root.GetRawText();
IRichMessage? res = null;
var parser = new MessageParser();
if (root.TryGetProperty("rich_type", out JsonElement element))
{
var richType = element.GetString();
res = parser.ParseRichMessage(richType, jsonText, root, options);
}
var res = MessageParser.ParseRichMessage(root, options);
return res;
}

View file

@ -10,15 +10,7 @@ public class TemplateMessageJsonConverter : JsonConverter<ITemplateMessage>
using var jsonDoc = JsonDocument.ParseValue(ref reader);
var root = jsonDoc.RootElement;
var jsonText = root.GetRawText();
ITemplateMessage? res = null;
var parser = new MessageParser();
if (root.TryGetProperty("template_type", out JsonElement element))
{
var templateType = element.GetString();
res = parser.ParseTemplateMessage(templateType, jsonText, root, options);
}
var res = MessageParser.ParseTemplateMessage(root, options);
return res;
}

View file

@ -6,48 +6,51 @@ using System.Text.Json;
namespace BotSharp.Core.Messaging;
public class MessageParser
public static class MessageParser
{
public MessageParser()
{
}
public IRichMessage? ParseRichMessage(string richType, string jsonText, JsonElement root, JsonSerializerOptions options)
public static IRichMessage? ParseRichMessage(JsonElement root, JsonSerializerOptions options)
{
IRichMessage? res = null;
JsonElement element;
var jsonText = root.GetRawText();
if (richType == RichTypeEnum.ButtonTemplate)
if (root.TryGetProperty("rich_type", out element))
{
res = JsonSerializer.Deserialize<ButtonTemplateMessage>(jsonText, options);
}
else if (richType == RichTypeEnum.MultiSelectTemplate)
{
res = JsonSerializer.Deserialize<MultiSelectTemplateMessage>(jsonText, options);
}
else if (richType == RichTypeEnum.QuickReply)
{
res = JsonSerializer.Deserialize<QuickReplyMessage>(jsonText, options);
}
else if (richType == RichTypeEnum.CouponTemplate)
{
res = JsonSerializer.Deserialize<CouponTemplateMessage>(jsonText, options);
}
else if (richType == RichTypeEnum.Text)
{
res = JsonSerializer.Deserialize<TextMessage>(jsonText, options);
}
else if (richType == RichTypeEnum.GenericTemplate)
{
if (root.TryGetProperty("element_type", out var element))
var richType = element.GetString();
if (richType == RichTypeEnum.ButtonTemplate)
{
var elementType = element.GetString();
if (elementType == typeof(GenericElement).Name)
res = JsonSerializer.Deserialize<ButtonTemplateMessage>(jsonText, options);
}
else if (richType == RichTypeEnum.MultiSelectTemplate)
{
res = JsonSerializer.Deserialize<MultiSelectTemplateMessage>(jsonText, options);
}
else if (richType == RichTypeEnum.QuickReply)
{
res = JsonSerializer.Deserialize<QuickReplyMessage>(jsonText, options);
}
else if (richType == RichTypeEnum.CouponTemplate)
{
res = JsonSerializer.Deserialize<CouponTemplateMessage>(jsonText, options);
}
else if (richType == RichTypeEnum.Text)
{
res = JsonSerializer.Deserialize<TextMessage>(jsonText, options);
}
else if (richType == RichTypeEnum.GenericTemplate)
{
if (root.TryGetProperty("element_type", out element))
{
res = JsonSerializer.Deserialize<GenericTemplateMessage<GenericElement>>(jsonText, options);
}
else if (elementType == typeof(ButtonElement).Name)
{
res = JsonSerializer.Deserialize<GenericTemplateMessage<ButtonElement>>(jsonText, options);
var elementType = element.GetString();
if (elementType == typeof(GenericElement).Name)
{
res = JsonSerializer.Deserialize<GenericTemplateMessage<GenericElement>>(jsonText, options);
}
else if (elementType == typeof(ButtonElement).Name)
{
res = JsonSerializer.Deserialize<GenericTemplateMessage<ButtonElement>>(jsonText, options);
}
}
}
}
@ -55,38 +58,44 @@ public class MessageParser
return res;
}
public ITemplateMessage? ParseTemplateMessage(string templateType, string jsonText, JsonElement root, JsonSerializerOptions options)
public static ITemplateMessage? ParseTemplateMessage(JsonElement root, JsonSerializerOptions options)
{
ITemplateMessage? res = null;
JsonElement element;
var jsonText = root.GetRawText();
if (templateType == TemplateTypeEnum.Button)
if (root.TryGetProperty("template_type", out element))
{
res = JsonSerializer.Deserialize<ButtonTemplateMessage>(jsonText, options);
}
else if (templateType == TemplateTypeEnum.MultiSelect)
{
res = JsonSerializer.Deserialize<MultiSelectTemplateMessage>(jsonText, options);
}
else if (templateType == TemplateTypeEnum.Coupon)
{
res = JsonSerializer.Deserialize<CouponTemplateMessage>(jsonText, options);
}
else if (templateType == TemplateTypeEnum.Product)
{
res = JsonSerializer.Deserialize<ProductTemplateMessage>(jsonText, options);
}
else if (templateType == TemplateTypeEnum.Generic)
{
if (root.TryGetProperty("element_type", out var element))
var templateType = element.GetString();
if (templateType == TemplateTypeEnum.Button)
{
var elementType = element.GetString();
if (elementType == typeof(GenericElement).Name)
res = JsonSerializer.Deserialize<ButtonTemplateMessage>(jsonText, options);
}
else if (templateType == TemplateTypeEnum.MultiSelect)
{
res = JsonSerializer.Deserialize<MultiSelectTemplateMessage>(jsonText, options);
}
else if (templateType == TemplateTypeEnum.Coupon)
{
res = JsonSerializer.Deserialize<CouponTemplateMessage>(jsonText, options);
}
else if (templateType == TemplateTypeEnum.Product)
{
res = JsonSerializer.Deserialize<ProductTemplateMessage>(jsonText, options);
}
else if (templateType == TemplateTypeEnum.Generic)
{
if (root.TryGetProperty("element_type", out element))
{
res = JsonSerializer.Deserialize<GenericTemplateMessage<GenericElement>>(jsonText, options);
}
else if (elementType == typeof(ButtonElement).Name)
{
res = JsonSerializer.Deserialize<GenericTemplateMessage<ButtonElement>>(jsonText, options);
var elementType = element.GetString();
if (elementType == typeof(GenericElement).Name)
{
res = JsonSerializer.Deserialize<GenericTemplateMessage<GenericElement>>(jsonText, options);
}
else if (elementType == typeof(ButtonElement).Name)
{
res = JsonSerializer.Deserialize<GenericTemplateMessage<ButtonElement>>(jsonText, options);
}
}
}
}

View file

@ -1,4 +1,3 @@
using BotSharp.Abstraction.Messaging.JsonConverters;
using System.Text.Json;
namespace BotSharp.Abstraction.Options;

View file

@ -1,5 +1,4 @@
using BotSharp.Abstraction.Messaging;
using BotSharp.Abstraction.Messaging.JsonConverters;
using BotSharp.Abstraction.Messaging.Models.RichContent;
using BotSharp.Abstraction.Options;
using System.IO;
@ -10,7 +9,7 @@ public class ConversationStorage : IConversationStorage
{
private readonly BotSharpDatabaseSettings _dbSettings;
private readonly IServiceProvider _services;
private readonly JsonSerializerOptions _options;
private readonly JsonSerializerOptions _jsonOptions;
public ConversationStorage(
BotSharpDatabaseSettings dbSettings,
@ -19,17 +18,7 @@ public class ConversationStorage : IConversationStorage
{
_dbSettings = dbSettings;
_services = services;
_options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
AllowTrailingCommas = true,
Converters =
{
new RichContentJsonConverter(),
new TemplateMessageJsonConverter(),
}
};
_jsonOptions = InitJsonSerilizerOptions(options);
}
public void Append(string conversationId, RoleDialogModel dialog)
@ -80,7 +69,8 @@ public class ConversationStorage : IConversationStorage
{
return;
}
var richContent = dialog.RichContent != null ? JsonSerializer.Serialize(dialog.RichContent, _options) : null;
var richContent = dialog.RichContent != null ? JsonSerializer.Serialize(dialog.RichContent, _jsonOptions) : null;
dialogElements.Add(new DialogElement(meta, content, richContent));
}
@ -105,7 +95,7 @@ public class ConversationStorage : IConversationStorage
var senderId = role == AgentRole.Function ? currentAgentId : meta.SenderId;
var createdAt = meta.CreateTime;
var richContent = !string.IsNullOrEmpty(dialog.RichContent) ?
JsonSerializer.Deserialize<RichContent<IRichMessage>>(dialog.RichContent, _options) : null;
JsonSerializer.Deserialize<RichContent<IRichMessage>>(dialog.RichContent, _jsonOptions) : null;
var record = new RoleDialogModel(role, content)
{
@ -150,4 +140,21 @@ public class ConversationStorage : IConversationStorage
}
return Path.Combine(dir, "dialogs.txt");
}
private JsonSerializerOptions InitJsonSerilizerOptions(BotSharpOptions botSharOptions)
{
var options = botSharOptions.JsonSerializerOptions;
var jsonOptions = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = options.PropertyNameCaseInsensitive,
PropertyNamingPolicy = options.PropertyNamingPolicy ?? JsonNamingPolicy.CamelCase,
AllowTrailingCommas = options.AllowTrailingCommas,
};
foreach (var converter in options.Converters)
{
jsonOptions.Converters.Add(converter);
}
return jsonOptions;
}
}