C
C#2y ago
Kensei

❔ ✅ JSON Serialization not working?

Can someone help me with this one, in the MS docs this seems to work just fine, what am i doing wrong..
using System;
using System.Collections.Generic;
using System.Text.Json;

namespace JSONSerialization
{
class Program
{
static void Main(string[] args)
{
Infos infos = new Infos("TestName", 9283943);

var options = new JsonSerializerOptions { WriteIndented = true };
string serializedContent = JsonSerializer.Serialize(infos, options);
Console.WriteLine(serializedContent);
}
}
class Infos
{
public string name = "";
public int id = 0;


public Infos(string name, int id)
{
this.name = name;
this.id = id;
}
}
}
using System;
using System.Collections.Generic;
using System.Text.Json;

namespace JSONSerialization
{
class Program
{
static void Main(string[] args)
{
Infos infos = new Infos("TestName", 9283943);

var options = new JsonSerializerOptions { WriteIndented = true };
string serializedContent = JsonSerializer.Serialize(infos, options);
Console.WriteLine(serializedContent);
}
}
class Infos
{
public string name = "";
public int id = 0;


public Infos(string name, int id)
{
this.name = name;
this.id = id;
}
}
}
18 Replies
canton7
canton72y ago
What do you see which makes you think that isn't working?
Kensei
KenseiOP2y ago
{}
canton7
canton72y ago
Ah, you're using fields rather than properties
Kensei
KenseiOP2y ago
is that the issue?
MODiX
MODiX2y ago
canton7#1569
sharplab.io (click here)
namespace JSONSerialization {
class Program {
static void Main(string[] args) {
Infos infos = new Infos("TestName", 9283943);
var options = new JsonSerializerOptions { Writ...
string serializedContent = JsonSerializer.Seri...
Console.WriteLine(serializedContent);
}
}
class Infos
// 10 more lines. Follow the link to view.
namespace JSONSerialization {
class Program {
static void Main(string[] args) {
Infos infos = new Infos("TestName", 9283943);
var options = new JsonSerializerOptions { Writ...
string serializedContent = JsonSerializer.Seri...
Console.WriteLine(serializedContent);
}
}
class Infos
// 10 more lines. Follow the link to view.
React with ❌ to remove this embed.
canton7
canton72y ago
Yes
Kensei
KenseiOP2y ago
in that case, thanks a lot !
canton7
canton72y ago
Most serializers will ignore fields, and just process properties. You should never have a public field, really
Kensei
KenseiOP2y ago
yeah it wasn't public at first, it thought it had something to do with it, so i tried everything
canton7
canton72y ago
Yeah, it also needs to be public
Kensei
KenseiOP2y ago
the one from newtonsoft seems to work fine so that's why i was confused
canton7
canton72y ago
Yeah, different defaults.
canton7
canton72y ago
Looks like you can configure S.T.J to use fields: https://stackoverflow.com/a/58139922/1086121
Stack Overflow
How to use class fields with System.Text.Json.JsonSerializer?
I recently upgraded a solution to be all .NET Core 3 and I have a class that requires the class variables to be fields. This is a problem since the new System.Text.Json.JsonSerializer doesn't support
Kensei
KenseiOP2y ago
oh neat thanks!
canton7
canton72y ago
Looks like you can use [JsonInclude] to use properties with private setters, but doesn't seem to work on private properties But, you should normally just use public properties in classes intended for serialization
Kensei
KenseiOP2y ago
yeah, i'm quite satisfied with this result, at first i thought the dictionary i had included was being faulty, cus i heard some serializers don't like dictionaries much but this one works nicely, + it's simple and can just be serialized directly to a string, exactly what i need
canton7
canton72y ago
Sweet!
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server