0

I am using aws lambda's api request template to create and call post methods. I have to send a post body from my code, which should be json serialized.

Here is a way I am doing it now:

**dynamic pObj = new JObject();
pObj.Url = DownloadURL;
pObj.Name = IName;
pObj.ID = I.ID;**

 Custom redirect = new Custom()
 {
     url = new Uri(lambdaEndpoint),
     **Body = pObj.ToString(),**
     Method = "POST",
     Available = true
 };

But I read an article where it talks about performance issues with using dynamic keyword.

Is there an alternative approach of doing this that is better in performance? Any help will be appreciated.

Thanks

aloisdg
  • 22,270
  • 6
  • 85
  • 105
Atihska
  • 4,803
  • 10
  • 56
  • 98

1 Answers1

0

An alternative to the usage of dynamic would be a direct deserialization. Since you know your object, it should fit as expected.

Demo

    using System;
    using Newtonsoft.Json;


    public class Program
    {
        // create you class. You can generate it with http://json2csharp.com/ or use Visual Studio (edit>past special)
        public class Custom
        {
            public string Url { get; set; }
            public string Name { get; set; }
            public string Id { get; set; }
        }

        public void Main()
        {
            string json = "{ \"Url\":\"xyz.com\", \"Name\":\"abc\", \"ID\":\"123\" }";
            // the magic code is here! Go to http://www.newtonsoft.com/json for more information
            Custom custom = JsonConvert.DeserializeObject<Custom>(json);

            // custom is fill with your json!
            Console.WriteLine(custom.Name);
        }
    }

output:

abc
aloisdg
  • 22,270
  • 6
  • 85
  • 105