Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception parsing json with System.Text.Json.Serialization

My sample code is very simple:

using System.Text.Json.Serialization;
using Newtonsoft.Json;

public class C {
  public C(string PracticeName) { this.PracticeName = PracticeName; }
  public string PracticeName;
}

var x = new C("1");
var json = JsonConvert.SerializeObject(x); // returns "{\"PracticeName\":\"1\"}"

var x1 = JsonConvert.DeserializeObject<C>(json); // correctly builds a C

var x2 = System.Text.Json.Serialization.JsonSerializer.Parse<C>(json);

the last line raises:

Exception thrown: 'System.NullReferenceException' in System.Text.Json.dll Object reference not set to an instance of an object.

What am I doing wrong ?

(Note this is on latest .NET Core 3 preview 5 with latest System.Text.Json 4.6.0-preview6.19259.10)

Adding a parameterless constructor prevents the exception however I don't want/need a parameterless constructor and Json.Net parses fine without it.

Is there a way to make System.Text.Json parse using the given constructor like Json.Net does ?

like image 983
kofifus Avatar asked Jun 07 '19 00:06

kofifus


People also ask

What is a JSON serialization exception?

The exception thrown when an error occurs during JSON serialization or deserialization.

What is JSON serialization?

Json namespace provides functionality for serializing to and deserializing from JavaScript Object Notation (JSON). Serialization is the process of converting the state of an object, that is, the values of its properties, into a form that can be stored or transmitted.

Which is better Newtonsoft JSON or System text JSON?

Json does case-insensitive property name matching by default. The System. Text. Json default is case-sensitive, which gives better performance since it's doing an exact match.

Is System text JSON faster than Newtonsoft JSON?

So, they created new data types ( Span , etc.) to work more efficiently with Strings to boost JSON performance. In their initial introduction of System. Text. Json, they claimed a 1.5x-5x speedup compared to Newtonsoft Json.Net.


1 Answers

In it's current state, JSON Support in .NET Core 3.0 is still not finished, and it seems only a parameterless constructor is supported. It might be, that that feature will be added in future.

One workaround option would be to make a parameterless constructor for your serialized model, when you want to use the new Json API from the .net framework. Probably we shouldn't use constructors for plain datatransfer objects at all, hence I see it as option, not as a workaround.

If you search for a way, on how to migrate from an older version to .net core 3.0, or use Newtonsoft.Json anyway, this is documented here:

MVC:

Install Microsoft.AspNetCore.Mvc.NewtonsoftJson package, and register it to your services:

services.AddMvc().AddNewtonsoftJson();

SignalR:

Install Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson package

//Client
new HubConnectionBuilder()
.WithUrl("/chatHub")
.AddNewtonsoftJsonProtocol(...)
.Build();

//Server
services.AddSignalR().AddNewtonsoftJsonProtocol(...);

That way you should* be able to use Json.NET Features in .Net Core 3.0

*I don't have installed it, so I can not test it

like image 191
Christian Gollhardt Avatar answered Sep 27 '22 20:09

Christian Gollhardt