I have faced an issue about serializing struct to JSON. So, I have a struct "Coordinates"
namespace CoordinatesNameSpace
{
public struct Coordinates
{
public Coordinates(string key, string x, string y, string z)
{
this.key = key;
this.x = x;
this.y = y;
this.z = z;
}
public string key;
public string x;
public string y;
public string z;
public override string ToString()
{
return $"{key} {this.x} {this.y} {this.z}";
}
}
}
All properties are publics, so I expected that json serializer will return me { "key": "v", "x": "0.12331212" ... }, but it returns just an empty object.
using CoordinatesNameSpace;
namespace ObjToJSON
{
class Program
{
static void Main(string[] args)
{
List<Coordinates> parsedCoordinatesList = new List<Coordinates>();
Coordinates _c;
_c.key = splitted[0]; // "v"
_c.x = splitted[1]; // "1.324394"
_c.y = splitted[2]; // "-0.219625"
_c.z = splitted[3]; // "-0.422554"
parsedCoordinatesList.Add(_c);
// returns an [{}, {}, {} ...]
//string json = JsonSerializer.Serialize<List<Coordinates>>(parsedCoordinatesList);
// returns {}
string json = JsonSerializer.Serialize<Coordinates>(parsedCoordinatesList[0]);
Can somebody explain it to me, please – why it does so and how to make it serialize properly?