C
C#2d ago
Fumetsu

✅ Newtonsoft.Json JsonSerializationException

As the title says, I keep getting a JsonSerializationException. I have no idea how to view the Exception details so if anyone could tell me that'd be nice. I've followed a few tutorials on this topic, but absolutely none have worked. Two exceptions occur in System.Private.CoreLib.dll and one in Newstonesoft.Json.dll
private async Task LoadData()
{
var clientJson = System.IO.File.ReadAllText(clientDataPath);
Classes.Spark.DebugLog(clientJson);
var clientData = JsonConvert.DeserializeObject<ClientData(clientJson);
}

public class ClientData
{
public string Client_ID { get; set; }
public string Client_Secret { get; set; }

public string Refresh_Token { get; set; }
public string Access_Token { get; set; }
}
private async Task LoadData()
{
var clientJson = System.IO.File.ReadAllText(clientDataPath);
Classes.Spark.DebugLog(clientJson);
var clientData = JsonConvert.DeserializeObject<ClientData(clientJson);
}

public class ClientData
{
public string Client_ID { get; set; }
public string Client_Secret { get; set; }

public string Refresh_Token { get; set; }
public string Access_Token { get; set; }
}
15 Replies
Fumetsu
FumetsuOP2d ago
Btw here is the the string that clientJson prints [{"Client_ID":"SECRET","Client_Secret":"SECRET","Refresh_Token":"%null%","Access_Token":"%null%"}]
leowest
leowest2d ago
well that is not ClientData and you're missing a > in your code [] represents a list/array so List<ClientData>
ero
ero2d ago
mandatory use System.Text.Json instead of Newtonsoft.Json
Fumetsu
FumetsuOP2d ago
Oh the second > is just gone in the message for some reason Is there a good reason not to use Newtonsoft? I heard it was faster than the default JsonSerializer
ero
ero2d ago
no, it's the other way around, by a long shot
Fumetsu
FumetsuOP2d ago
Ohhh okay, that's great actually because there's more tutorials on the default lol. Thanks for that information
leowest
leowest2d ago
because nsj was created when there was nothing standardized to do the job or in the BCL now there is, which is STJ which was created by the same people who made nsj. so yes prefer STJ over NSJ unless you have a very specific use case and fked up json to deal with.
Fumetsu
FumetsuOP2d ago
Ahhh okay thank you very much. I'll try using STJ and see how it fares
leowest
leowest2d ago
anyway the json u posted is not just ClientData so u need to either use array or list
Fumetsu
FumetsuOP2d ago
Ahhh sorry I'm not well versed in this stuff, how would I go about that?
ero
ero2d ago
.Deserialize<ClientData[]>
Fumetsu
FumetsuOP2d ago
Ahh okay thank you. Using that it made progress, I just have to adjust some other stuff now
leowest
leowest2d ago
$close
MODiX
MODiX2d ago
If you have no further questions, please use /close to mark the forum thread as answered
Fumetsu
FumetsuOP2d ago
Ohhhh okay, sorry about that You guys were of much help, thanks to you I managed to get my problem solved! Thanks for helping!

Did you find this page helpful?