0

Say I define a class as follows:

public class Foo {
   [JsonProperty(PropertyName = "custoomername")]
   public string CustomerName { get; set; }
}

Notice that I'm expecting to read/write some bad JSON. Not invalid syntax, but perhaps another developer has a spelling error I need to workaround (custoomer instead of customer).

Now, in my controller I have this method:

public ActionResult DoSomething(Foo foo) {
   return Json(foo);
}

I want the input object foo and the returned JSON from foo to both use NewtonSoft and not the internal JavascriptSerializer that is default on MVC.

I was able to solve the output only so far using this example. However, the input doesn't work. I tried replacing the ValueFactoryProvider for the JsonValueProviderFactory with this example however that doesn't appear to understand what object type the requesting method will want.

Is it possible to do this?

[Edit:] Under the proposed duplicate's answer the ExpandoObject is used and a DictionaryValueProvider is created. This still has the bad JSON in it and does not deserialize into my class.

Paul
  • 5,700
  • 5
  • 43
  • 67
  • There are many many questions already on here about this, but yes this is possible. – Robert Petz Jun 07 '17 at 15:41
  • This is not a duplicate. That answer does not work. – Paul Jun 07 '17 at 16:32
  • we aren't saying it is a duplicate of one question we are saying that even a basic search of stack overflow yields *MANY* questions on this exact topic – Robert Petz Jun 07 '17 at 16:48
  • None that I can find. And none that work. And yes this question is being flagged as a duplicate above. Which does not solve my issue. – Paul Jun 07 '17 at 17:02
  • I don't believe they can flag it as a duplicate of an unknown set of questions, but yes there are many questions that a very cursory search brings up on this exact topic - https://stackoverflow.com/q/4164114/594354 – Robert Petz Jun 07 '17 at 17:14

0 Answers0