Note that there are some explanatory texts on larger screens.

plurals
  1. PODeserializing JSON in .NET that I have no control over
    primarykey
    data
    text
    <p>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:</p> <pre><code>string fooJson = @" { 'foo_baz': 123, foo_bar: 'buuuu', ""Xyz"": ""\/Date(405928800000-0600)\/"" //1982-11-12 }"; </code></pre> <p>The JSON has to be deserialized into this C# class that I can't change:</p> <pre><code>public class Foo { public string Bar { get; set; } public int Baz { get; private set; } public DateTime Xyz { get; set; } } </code></pre> <p>Serializing <code>Foo</code> is no problem.</p> <p>After deserializing, the results should be...</p> <ul> <li><code>Bar</code> == <code>"buuuu"</code></li> <li><code>Baz</code> == <code>123</code></li> <li><code>Xyz</code> == <code>DateTime.MinValue</code> or uninitialized</li> </ul> <p>...but I have problems.</p> <p><code>DataContractJsonSerializer</code>, along with <code>DataContract</code> and <code>DataMember</code> attributes on <code>Foo</code> and its members, is basically perfect, except it balks on JSON properties and strings that aren't properly double-quoted.</p> <p><code>JavaScriptSerializer</code> seems to handle the non-standard quoting, but I can't get it to ignore <code>Xyz</code> or map <code>foo_baz</code> to <code>Baz</code> and <code>foo_bar</code> to <code>Bar</code>.</p> <p>This is going to be part of a distributable class library, so I'm trying to avoid third-party tools&mdash;such as JSON.NET&mdash;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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload