4

Title says it all. I see it (with its corresponding end) spits out the following...

new Foo(
)

...but I don't understand what the new actually does when deserializing. The docs just say it writes a Json constructor, but not what a Json constructor is!

Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286

1 Answers1

4

This method was introduced as part of an enhancement to allow Json.NET to parse date literals when represented with JavaScript constructors like so: new Date(1234656000000). For details, see Serializing Dates in JSON. Even though this syntax is not strictly valid according to the JSON standard, apparently there was enough demand to support it that Newtonsoft put it in.

Thus it was necessary to add low-level methods to JsonWriter and JsonReader to write and read literals in this format. (Strictly speaking I suppose writing could have been tacked on to WriteRaw, this would have been kind of hackey, and anyway there is no method ReadRaw) WriteStartConstructor is one of these low-level utility methods.

There is also a high-level Linq-to-JSON class JConstructor to represent this syntax. You can use it to support other specific JavaScript constructors as needed, for instance see How to convert new Date(year, month, day) overload with JSON.Net.

dbc
  • 104,963
  • 20
  • 228
  • 340