2

I'm trying to migrate from JavaScriptSerializer to System.Text.Json and I see that the latter doesn't understand serialized data from the former. Newtonsoft understands the format but I need System.Text.Json. How can I workaround it? What other snags can I hit?

var str = "{ \"Date\": \"\\/Date(1471463788903)\\/\" }";
var js = new JavaScriptSerializer().Deserialize<D>(str);
var newntonsoft = JsonConvert.DeserializeObject<D>(str);
var system = JsonSerializer.Deserialize<D>(str); // throws

public class D { public DateTime Date { get; set; } }
7
  • 2
    The format you are seeing is called Microsoft DateTime wire format and is indeed different from the ISO formats you are used to seeing. In this issue here a (ex-) Microsoft employee states that they made a conscious decision to not support this format in System.Text.Json. There are however custom converters available that do support the Microsoft date format. Commented Jan 24, 2024 at 15:01
  • @nbokmans alas, I'm not allowed to use third-party packages. Commented Jan 24, 2024 at 15:07
  • 2
    So then you could take a look at the source code of the package, maybe a specific look at the MicrosoftDateTime Converter, see how they implement it, and implement it yourself in your code base? It comes down to needing a custom converter for this data format, which System.Text.Json allows you to write yourself. Commented Jan 24, 2024 at 15:11
  • I'm not allowed to use third-party packages hallmark of an insane employer. Commented Jan 24, 2024 at 15:25
  • 2
    If you are not allowed to use third-party packages. then there is a converter in this answer by Palle Due to Deserializing JSON to DateTime property throws exception and also this question by xeraphim. Commented Jan 24, 2024 at 15:37

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.