Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
71 views
in Technique[技术] by (71.8m points)

c# - How can I create a dynamic JSON key from a variable?

Our eLearning platform uses a third-party email service called SendGrid to send out our emails. We pass in a JSON object to the platform and our templates dynamically fill in values from the JSON object using Handlebars.

A typical JSON object that we pass looks like this:

{
   "To": "[email protected]",
   "From": "[email protected]",
   "Subject": "Online Course",
   "TemplateData": {
      "CourseName": "Course 1",
      "LoginPage": "https://www.login.com"
   }   
}

We're starting to implement multi-language support. We can add Handlebars to our template to do a condition if-then statement to dynamically populate the proper translatedd text based on the language that is passed in. So I envisioned adding a new Language key to the TemplateData with the language we passed in, as such as follows:

"TemplateData": {
      "CourseName": "Course 1",
      "Language": "en-US",
      "LoginPage": "https://www.login.com"
   }   

Unfortunately, I realized that their if-else conditions in the Handlebars only support boolean comparisons, so it would have to look like this instead:

"TemplateData": {
      "CourseName": "Course 1",
      "en-US": true
      "LoginPage": "https://www.login.com"
   }   

Since the language part (e.g. en-US, en-CA, fr-FR, etc.) is a variable being passed in from a stored procedure, I can't just set the language key statically in the object definition.

The deserialized object is as follows:

public abstract class CpiEmailMessage : IEmailMessage
{
   public string To { get; protected set; }
   protected static string FromEmail => "[email protected]";
   protected static string FromName => "CPI Learning";

   protected Dictionary<string, string> CustomArgs(int emailLogId) => new 
                      Dictionary<string, string>
   {
       { "SystemSource", "HoneyBee.Integrations" },
       { "Env", ConfigurationManager.AppSettings.Get("Env") },
       { "EmailLogId", $"{emailLogId}" }
   };

   public abstract EmailSendRequest ToEmailSendRequest(int emailLogId);
   public abstract EmailLog ToLogEmailCommandRequest(int currentUser, bool sent);
 }

public class PurchaseEmailMessage : CpiEmailMessage
{
    private string Subject { get; }
    public string CourseName { get; }
    public string CourseLanguage { get; set; }
    public string LoginPage { get; set; }
    
    public string Url { get; }
    public string OnlineOnly { get; }

    private static EmailType MessageTemplateKey => EmailType.PURCHASE;

 public PurchaseEmailMessage(string to, string subject, string courseName, string courseLanguage, string loginPage, string cmsURL, string onlineOnly)
 {
      To = to;
      Subject = subject;
      CourseName = courseName;
      CourseLanguage = courseLanguage;
      LoginPage = loginPage;
      Url= cmsURL;
      OnlineOnly = onlineOnly;
  }

  public override EmailSendRequest ToEmailSendRequest(int emailLogId)
  {
       return new EmailSendRequest
       {
            To = To,
            FromEmail = FromEmail,
            FromName = FromName,
            Subject = Subject,
            MessageTemplateKey = MessageTemplateKey,
            TemplateData = new
            {
                Course = CourseName,
                LoginUrl = LoginPage
            },
            CustomArgs = CustomArgs(emailLogId),
            EmailLogId = emailLogId,
            Categories = new List<string> { "Seat Purchase" }
        };
    }
}

How can I take the language variable (en-US) and make it a key in TemplateData with a 'true' value?

question from:https://stackoverflow.com/questions/65838849/how-can-i-create-a-dynamic-json-key-from-a-variable

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...