Newtonsoft.Json Serialization Exception

Trying to deserialize a List<Highscore> for my project and it throws this exception: Unhandled exception. Newtonsoft.Json.JsonSerializationException: Error converting value "{"Name":"Willy","Time":316}" to type 'ReactionGame.Highscore' Code (T is List<Highscore>):
public virtual async Task<T> Get<T>(string url) where T : class
{
using var client = new HttpClient();

var stringResult = await client.GetStringAsync(url);
return JsonConvert.DeserializeObject<T>(stringResult, new JsonSerializerSettings{NullValueHandling = NullValueHandling.Ignore});
}
public virtual async Task<T> Get<T>(string url) where T : class
{
using var client = new HttpClient();

var stringResult = await client.GetStringAsync(url);
return JsonConvert.DeserializeObject<T>(stringResult, new JsonSerializerSettings{NullValueHandling = NullValueHandling.Ignore});
}
Model:
namespace ReactionGame
{
public class Highscore
{
[JsonProperty]
public string Name { get; set; }
[JsonProperty]
public long Time { get; set; }

public override string ToString()
{
return Name + " - " + Time;
}

[JsonConstructor]
public Highscore(string name, long time)
{
Name = name;
Time = time;
}
}
}
namespace ReactionGame
{
public class Highscore
{
[JsonProperty]
public string Name { get; set; }
[JsonProperty]
public long Time { get; set; }

public override string ToString()
{
return Name + " - " + Time;
}

[JsonConstructor]
public Highscore(string name, long time)
{
Name = name;
Time = time;
}
}
}
Anyone know what might be wrong? I'm at a loss.
26 Replies
MODiX
MODiX2y ago
Ero#1111
REPL Result: Success
using Newtonsoft.Json;

var json = """
{
"Name": "Willy",
"Time": 316
}
""";

var hs = JsonConvert.DeserializeObject<Highscore>(json, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});

Console.WriteLine(hs);

class Highscore
{
[JsonProperty]
public string Name { get; set; }
[JsonProperty]
public long Time { get; set; }

public override string ToString()
{
return Name + " - " + Time;
}

[JsonConstructor]
public Highscore(string name, long time)
{
Name = name;
Time = time;
}
}
using Newtonsoft.Json;

var json = """
{
"Name": "Willy",
"Time": 316
}
""";

var hs = JsonConvert.DeserializeObject<Highscore>(json, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});

Console.WriteLine(hs);

class Highscore
{
[JsonProperty]
public string Name { get; set; }
[JsonProperty]
public long Time { get; set; }

public override string ToString()
{
return Name + " - " + Time;
}

[JsonConstructor]
public Highscore(string name, long time)
{
Name = name;
Time = time;
}
}
Console Output
Willy - 316
Willy - 316
Compile: 692.423ms | Execution: 158.707ms | React with ❌ to remove this embed.
ero
ero2y ago
¯\_(ツ)_/¯
𝕭𝖔𝖙𝖙𝖑𝖊_𝕭𝖆𝖗𝖔𝖓
Thanks xD What did you change exactly?
ero
ero2y ago
Nothing Just showing your that it should work
𝕭𝖔𝖙𝖙𝖑𝖊_𝕭𝖆𝖗𝖔𝖓
Bruh 😓 thanks anyways :)
MODiX
MODiX2y ago
Check your private messages, @𝕭𝖔𝖙𝖙𝖑𝖊_𝕭𝖆𝖗𝖔𝖓.
MODiX
MODiX2y ago
Ero#1111
REPL Result: Success
using Newtonsoft.Json;

var json = """
[
{
"Name": "Willy",
"Time": 316
},
{
"Name": "Willy",
"Time": 316
}
]
""";

var hs = JsonConvert.DeserializeObject<List<Highscore>>(json, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});

Console.WriteLine(hs[0]);

class Highscore
{
[JsonProperty]
public string Name { get; set; }
[JsonProperty]
public long Time { get; set; }

public override string ToString()
{
return Name + " - " + Time;
}

[JsonConstructor]
public Highscore(string name, long time)
{
Name = name;
Time = time;
}
}
using Newtonsoft.Json;

var json = """
[
{
"Name": "Willy",
"Time": 316
},
{
"Name": "Willy",
"Time": 316
}
]
""";

var hs = JsonConvert.DeserializeObject<List<Highscore>>(json, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});

Console.WriteLine(hs[0]);

class Highscore
{
[JsonProperty]
public string Name { get; set; }
[JsonProperty]
public long Time { get; set; }

public override string ToString()
{
return Name + " - " + Time;
}

[JsonConstructor]
public Highscore(string name, long time)
{
Name = name;
Time = time;
}
}
Console Output
Willy - 316
Willy - 316
Compile: 742.622ms | Execution: 179.356ms | React with ❌ to remove this embed.
ero
ero2y ago
Still works for a list too
𝕭𝖔𝖙𝖙𝖑𝖊_𝕭𝖆𝖗𝖔𝖓
Hm Might have found it Seems the json string has been altered somehow
["{\"Name\":\"Pet\",\"Time\":316}","{\"Name\":\"Pet\",\"Time\":278}"]
["{\"Name\":\"Pet\",\"Time\":316}","{\"Name\":\"Pet\",\"Time\":278}"]
ero
ero2y ago
where are you getting this input?
𝕭𝖔𝖙𝖙𝖑𝖊_𝕭𝖆𝖗𝖔𝖓
Im sending the data from my game to a web api then using the get method to retrieve it from the site
ero
ero2y ago
is the web api strictly a middleman for this or does it do more?
𝕭𝖔𝖙𝖙𝖑𝖊_𝕭𝖆𝖗𝖔𝖓
No it is just supposed to store data
ero
ero2y ago
then why not just send the data from the game directly to the app?
𝕭𝖔𝖙𝖙𝖑𝖊_𝕭𝖆𝖗𝖔𝖓
That is what i am attempting using the post and get endpoints
ero
ero2y ago
well no, that's a middleman
𝕭𝖔𝖙𝖙𝖑𝖊_𝕭𝖆𝖗𝖔𝖓
this is what the repo looks like roughly
𝕭𝖔𝖙𝖙𝖑𝖊_𝕭𝖆𝖗𝖔𝖓
The WebApi parts job is to take the data sent from the reactiongame and store it into a SQLite database The get method should then get that data and return it to the game
ero
ero2y ago
right well, anyway. that seems like incorrectly formatted json there shouldn't be quotes after [ and before ]
𝕭𝖔𝖙𝖙𝖑𝖊_𝕭𝖆𝖗𝖔𝖓
ait then i will look into this :)
Pobiega
Pobiega2y ago
that looks like a List<string> where each string is a json serialized object
ero
ero2y ago
yup also consider using System.Text.Json instead of Newtonsoft
𝕭𝖔𝖙𝖙𝖑𝖊_𝕭𝖆𝖗𝖔𝖓
Yeah I'll try that Thanks ^^
ero
ero2y ago
it won't fix this! but it is still just. better in every way
MODiX
MODiX2y ago
Ero#1111
REPL Result: Success
using System.Text.Json;

var json = """
[
{
"Name": "Pet",
"Time": 316
},
{
"Name": "Pet",
"Time": 278
}
]
""";

var hs = JsonSerializer.Deserialize<List<Highscore>>(json);
Console.WriteLine(hs[0]);

record Highscore(
string Name,
int Time);
using System.Text.Json;

var json = """
[
{
"Name": "Pet",
"Time": 316
},
{
"Name": "Pet",
"Time": 278
}
]
""";

var hs = JsonSerializer.Deserialize<List<Highscore>>(json);
Console.WriteLine(hs[0]);

record Highscore(
string Name,
int Time);
Console Output
Highscore { Name = Pet, Time = 316 }
Highscore { Name = Pet, Time = 316 }
Compile: 753.600ms | Execution: 95.491ms | React with ❌ to remove this embed.
Want results from more Discord servers?
Add your server