I have to deserialize JSON strings that I don’t have any control over. This is an extreme example, but it gets the point across:
string fooJson = @"
{
'foo_baz': 123,
foo_bar: 'buuuu',
""Xyz"": ""\/Date(405928800000-0600)\/"" //1982-11-12
}";
The JSON has to be deserialized into this C# class that I can’t change:
public class Foo
{
public string Bar { get; set; }
public int Baz { get; private set; }
public DateTime Xyz { get; set; }
}
Serializing Foo is no problem.
After deserializing, the results should be…
Bar=="buuuu"Baz==123Xyz==DateTime.MinValueor uninitialized
…but I have problems.
DataContractJsonSerializer, along with DataContract and DataMember attributes on Foo and its members, is basically perfect, except it balks on JSON properties and strings that aren’t properly double-quoted.
JavaScriptSerializer seems to handle the non-standard quoting, but I can’t get it to ignore Xyz or map foo_baz to Baz and foo_bar to Bar.
This is going to be part of a distributable class library, so I’m trying to avoid third-party tools—such as JSON.NET—as much as possible. However, if there’s no built-in way to do what I need, I’m open to suggestions of third-party tools.
if you don’t have any control over what gets sent to you, you are going to create your own custom parsing engine, as no JSON parsing will work reliably with what you are receiving.
Maybe you could cleanse/format the string that you receive INTO a JSON string and then parse it as JSON, but otherwise you have to write your own parsing implementation.
This part will not be over quickly. You will not enjoy it. I am not your king.