0

I'm getting my input data in camel and pascal case. before putting business logic I want to convert input data into a single case.

I did below code (it's working). It doesn't look good when I think about performance.

string message = "{JSON in pascal or camel case}";
var interimObject = JsonConvert.DeserializeObject<ExpandoObject>(message);
string json = JsonConvert.SerializeObject(interimObject, Formatting.Indented,new JsonSerializerSettings
                  {
                      ContractResolver = AppConfiguration.CamelCaseResolver
                  });
JObject alertMessage = JObject.Parse(json);
alertMessage.Add("id", Guid.NewGuid().ToString());//correlation-Id

Is there any better way??

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Pankaj Rawat
  • 4,037
  • 6
  • 41
  • 73
  • 1
    Why are you getting data in with several formats? It isn’t possible to fix the parts that are producing the wrong output? Or let JSON.net just handle it? – Sami Kuhmonen Feb 18 '19 at 06:30
  • Existing application produce data in camel and pascal case. I have a single endpoint for all. I have some if else condition so I need to fix case issue. – Pankaj Rawat Feb 18 '19 at 06:56
  • *I did below code (it's working). It doesn't look good when I think about performance.* -- if it's working, them what is your question? How to avoid having to construct a complete `interimObject` in memory? – dbc Feb 18 '19 at 21:05
  • 1
    If you simply want to avoid constructing `interimObject` you can adapt the method `ConvertJson(TextReader textReader, TextWriter textWriter, NamingStrategy strategy, Formatting formatting)` from [this answer](https://stackoverflow.com/a/53783120/3744182) to [Convert JSON camel case to snake case (and vice versa) and stringify numeric values](https://stackoverflow.com/q/53779796/3744182) by [Brian Rogers](https://stackoverflow.com/users/10263/brian-rogers). In fact your question may be a duplicate; agree? – dbc Feb 18 '19 at 21:15

0 Answers0