C
C#7d ago
Frite

✅ Opening a Json file

I switched from Newtonsoft.Json to System.Text.Json and i can't get it to work. it gives me an object with null attributes
No description
38 Replies
FusedQyou
FusedQyou7d ago
If you want help you will have to give a lot more context than this and $paste your code for starters. This is incredibly vague and we don't know your project.
MODiX
MODiX7d ago
If your code is too long, you can post to https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code!
Frite
FriteOP7d ago
public static Ogmo.Level LoadLevel(string name)
{
string file;
using (StreamReader sr = new StreamReader(Path + name))
file = sr.ReadToEnd();

JsonSerializerOptions options = new JsonSerializerOptions();
options.Converters.Add(new Converter());

return JsonSerializer.Deserialize<Ogmo.Level>(file, options);
}
public static Ogmo.Level LoadLevel(string name)
{
string file;
using (StreamReader sr = new StreamReader(Path + name))
file = sr.ReadToEnd();

JsonSerializerOptions options = new JsonSerializerOptions();
options.Converters.Add(new Converter());

return JsonSerializer.Deserialize<Ogmo.Level>(file, options);
}
FusedQyou
FusedQyou7d ago
More code. Classes, the converter, everything related
SG97
SG977d ago
and how is Ogmo.Level looking?
FusedQyou
FusedQyou7d ago
This still helps nothing
Frite
FriteOP7d ago
with Newtonsoft it worked
FusedQyou
FusedQyou7d ago
Which is an entirely different package
Frite
FriteOP7d ago
my objects matchs the json file
FusedQyou
FusedQyou7d ago
The only suggestion I have now is that you validate that file actually contains the JSON you expect to deserialize. This could also be the cause of the problem. Until that, either check that or share the code or we have absolutely no way or reason to help Generally Newtonsoft and STJ work similary to eachother but there are plenty of factors that can influence it. The converter can be one of them. We can't help you with just pure guesswork so you should share the code or perform basic debugging.
Frite
FriteOP7d ago
file:
{
"ogmoVersion": "3.4.0",
"width": 448,
"height": 128,
"offsetX": 0,
"offsetY": 0,
"values": {"BPM": 140},
"layers": [
{
"name": "1",
"_eid": "74127826",
"offsetX": 0,
"offsetY": 0,
"gridCellWidth": 16,
"gridCellHeight": 16,
"gridCellsX": 28,
"gridCellsY": 8,
"entities": [
{
"name": "Kick",
"id": 8,
"_eid": "74122737",
"x": 128,
"y": 0,
"originX": 0,
"originY": 0,
"values": {"IN": 1, "OUT": 0, "Random": true, "PositionX": 0}
},
{
"name": "Kick",
"id": 9,
"_eid": "74122737",
"x": 192,
"y": 0,
"originX": 0,
"originY": 0,
"values": {"IN": 1, "OUT": 0, "Random": true, "PositionX": 0}
},
{
"name": "Kick",
"id": 10,
"_eid": "74122737",
"x": 256,
"y": 0,
"originX": 0,
"originY": 0,
"values": {"IN": 1, "OUT": 0, "Random": true, "PositionX": 0}
},
{
"name": "Kick",
"id": 11,
"_eid": "74122737",
"x": 320,
"y": 0,
"originX": 0,
"originY": 0,
"values": {"IN": 1, "OUT": 0, "Random": true, "PositionX": 0}
}
]
},
{
"name": "2",
"_eid": "74123146",
"offsetX": 0,
"offsetY": 0,
"gridCellWidth": 16,
"gridCellHeight": 16,
"gridCellsX": 28,
"gridCellsY": 8,
"entities": []
},
{
"name": "3",
"_eid": "74121771",
"offsetX": 0,
"offsetY": 0,
"gridCellWidth": 16,
"gridCellHeight": 16,
"gridCellsX": 28,
"gridCellsY": 8,
"entities": []
}
]
}
{
"ogmoVersion": "3.4.0",
"width": 448,
"height": 128,
"offsetX": 0,
"offsetY": 0,
"values": {"BPM": 140},
"layers": [
{
"name": "1",
"_eid": "74127826",
"offsetX": 0,
"offsetY": 0,
"gridCellWidth": 16,
"gridCellHeight": 16,
"gridCellsX": 28,
"gridCellsY": 8,
"entities": [
{
"name": "Kick",
"id": 8,
"_eid": "74122737",
"x": 128,
"y": 0,
"originX": 0,
"originY": 0,
"values": {"IN": 1, "OUT": 0, "Random": true, "PositionX": 0}
},
{
"name": "Kick",
"id": 9,
"_eid": "74122737",
"x": 192,
"y": 0,
"originX": 0,
"originY": 0,
"values": {"IN": 1, "OUT": 0, "Random": true, "PositionX": 0}
},
{
"name": "Kick",
"id": 10,
"_eid": "74122737",
"x": 256,
"y": 0,
"originX": 0,
"originY": 0,
"values": {"IN": 1, "OUT": 0, "Random": true, "PositionX": 0}
},
{
"name": "Kick",
"id": 11,
"_eid": "74122737",
"x": 320,
"y": 0,
"originX": 0,
"originY": 0,
"values": {"IN": 1, "OUT": 0, "Random": true, "PositionX": 0}
}
]
},
{
"name": "2",
"_eid": "74123146",
"offsetX": 0,
"offsetY": 0,
"gridCellWidth": 16,
"gridCellHeight": 16,
"gridCellsX": 28,
"gridCellsY": 8,
"entities": []
},
{
"name": "3",
"_eid": "74121771",
"offsetX": 0,
"offsetY": 0,
"gridCellWidth": 16,
"gridCellHeight": 16,
"gridCellsX": 28,
"gridCellsY": 8,
"entities": []
}
]
}
my code:
using System;
using System.IO;
using System.Text.Json;

namespace JSABlike;

public abstract class Ogmo
{
/* for later
public abstract class A
{
public float IN;
public float OUT;
public bool Random;
}

public class Bass : A
{
public float HOLD;
}
*/

public class LevelValues
{
public byte BPM;
}

public class Kick
{
public float IN;
public float OUT;
public bool Random;
public short PositionX;
}

public class Entity
{
public string name;
public uint id;
public uint _eid;
public ulong x;
public uint y;
public int originX;
public int originY;
public Kick values;
}

public class Layer
{
string name;
string _eid;
public int offsetX;
public int offsetY;
public byte gridCellWidth;
public byte gridCellHeight;
public uint gridCellsX;
public uint gridCellsY;
public Entity[] entities;
}

public class Level
{
public string ogmoVersion;
public uint width;
public uint height;
public int offsetX;
public int offsetY;
public LevelValues values;
public Layer[] layers;
}
}

public static class LevelReader
{
private static string Path => AppContext.BaseDirectory;

public static Ogmo.Level LoadLevel(string name)
{
string file;
using (StreamReader sr = new StreamReader(Path + name))
file = sr.ReadToEnd();

return JsonSerializer.Deserialize<Ogmo.Level>(file);
}
}
using System;
using System.IO;
using System.Text.Json;

namespace JSABlike;

public abstract class Ogmo
{
/* for later
public abstract class A
{
public float IN;
public float OUT;
public bool Random;
}

public class Bass : A
{
public float HOLD;
}
*/

public class LevelValues
{
public byte BPM;
}

public class Kick
{
public float IN;
public float OUT;
public bool Random;
public short PositionX;
}

public class Entity
{
public string name;
public uint id;
public uint _eid;
public ulong x;
public uint y;
public int originX;
public int originY;
public Kick values;
}

public class Layer
{
string name;
string _eid;
public int offsetX;
public int offsetY;
public byte gridCellWidth;
public byte gridCellHeight;
public uint gridCellsX;
public uint gridCellsY;
public Entity[] entities;
}

public class Level
{
public string ogmoVersion;
public uint width;
public uint height;
public int offsetX;
public int offsetY;
public LevelValues values;
public Layer[] layers;
}
}

public static class LevelReader
{
private static string Path => AppContext.BaseDirectory;

public static Ogmo.Level LoadLevel(string name)
{
string file;
using (StreamReader sr = new StreamReader(Path + name))
file = sr.ReadToEnd();

return JsonSerializer.Deserialize<Ogmo.Level>(file);
}
}
SG97
SG977d ago
I can see public fields how's the converter looking
FusedQyou
FusedQyou7d ago
Pretty sure even Newtonsoft doesn't use public fields by default so this is something you configured
SG97
SG977d ago
think so too
FusedQyou
FusedQyou7d ago
STJ also has this configured. They use properties by default
Frite
FriteOP7d ago
how should i make my objects ?
SG97
SG977d ago
can you show the converter generally I do public properties wait, there is no converter
Frite
FriteOP7d ago
i removed it, first i just want it to load this
FusedQyou
FusedQyou7d ago
The correct answer would be to switch to properties since that's the correct approach. You can also pass configuration to System.Text.Json and specify fields should be used.
FusedQyou
FusedQyou7d ago
Include fields in serialization - .NET
Learn how to include fields when you serialize to and deserialize from JSON in .NET.
SG97
SG977d ago
By default, fields are ignored. You can include fields. ah, I'm too slow
Frite
FriteOP7d ago
um, is there a way to make them readonly or private set ? just to make things more clean
FusedQyou
FusedQyou7d ago
Make a property with a public getter but an init setter
Frite
FriteOP7d ago
ahhh, i can't use uint ?
FusedQyou
FusedQyou7d ago
public int Foo { get; init; }
Frite
FriteOP7d ago
i didn't know about that
FusedQyou
FusedQyou7d ago
I assume STJ would work fine with this. Any particular reason why you say this?
Frite
FriteOP7d ago
i'm using positive numbers
SG97
SG977d ago
just change it to uint, don't think it's a problem
Frite
FriteOP7d ago
should i get back to Newtonsoft ?
FusedQyou
FusedQyou7d ago
Again, I don't see why it would not work. The only issue seemingly needing a fix is the fact fields are not by default used So either switch to properties, or use the option I shared above
Frite
FriteOP7d ago
'The JSON value could not be converted to System.UInt32. Path: $.layers[0].entities[0]._eid | LineNumber: 21 | BytePositionInLine: 28.' :( where line 21 : "_eid": "74122737",
FusedQyou
FusedQyou7d ago
That's because it's a string, not an integer I assume by default Newtonsoft parses it The correct fix is to pass a proper number Alternatively, add the following to the list of options:
var options = new JsonSerializerOptions()
{
NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString
};
var options = new JsonSerializerOptions()
{
NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString
};
Frite
FriteOP7d ago
oh thanks i didn't noticed that was a string ok this works! and runs much faster than Newtonsoft thanks guys now lets get into polymorphism
MODiX
MODiX7d ago
If you have no further questions, please use /close to mark the forum thread as answered
Pobiega
Pobiega7d ago
polymorphism in STJ is actually really nice and easy, as long as your variants are known at compile time, and you have some measure of control over the json, or it alread being wellformed (like having a discriminator value) https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/polymorphism has all the info you need
Frite
FriteOP6d ago
it all works perfectly, thanks! i'm done with json

Did you find this page helpful?