Monogame, Tiled and JSON import and deserialize

Hello again, Mono Comm,
First off, thanks for the help on my previous topic.
I don’t post a lot because I’m still learning and I’ve bumped into a GREAT learning experience that I can’t seem to find good beginner documentation for. Like the title lists, I’ve decided to use Tiled maps saved as JSON files. I’m testing a possible animation for my world map (the water) and I can export just fine from Tiled, open the file and it reminds me of XML in some ways, but now to my problem / question:

Is there any good beginner tutorial or help that I can find here to get this JSON file properly imported into my game? (problem 1:) When I “include” the file into my solution, it requires my ENTIRE file pathway in order to read it… that won’t work if someone were to play the game on their system, of course :frowning:

Looks like this:

using (StreamReader reader = new StreamReader("C:/Users/Username/Desktop/DC Project/FP_RPG_V3_OPENGL/FP_RPG_V3_OPENGL/JSON/World Map TileSet.json"))
                string json = reader.ReadToEnd();
                var animationTiles = JsonConvert.DeserializeObject(json);


(Problem 2:) Once I have the test file in, I see all kinds of data.
Like this:

{ "columns":10,
 "image":"DC Project\/FP_RPG_V3_OPENGL\/FP_RPG_V3_OPENGL\/Content\/World_Map\/World Map.png",
 "name":"World Map TileSet",

The next problem is… all I need is the animation tiles and durations, not so much the rest.
I’m just learning JSON and how to deserialize it but I clearly don’t know enough. Do I need to store ALL of that data or can I just grab what I need?
Once I have the file read, it attempts to store it in an object of MapTile type, but that class has other parms that aren’t in the JSON. Does that matter?
If I can figure out how to work this, I intend to load all world map data using this method, but for now I just want to test it out and get the feel of it. (LEARN DANIELSON!)

I’ve been all over the net (Here, Stackoverflow, C# Corner…etc) and it’s hard to make since of what is going on as they aren’t too clear for beginners. I’ve seen things like storing in a list, errors thrown for array issues, and the like. My map tiles are currently loaded into an array of MapTile[,] for the grid.

Is using a JSON a good approach? Am I on the right track?
If need be, I’ll post other code.

Thanks again for any help! Sorry if this isn’t too clear. I’m trying! Haha

You can write a custom content pipeline tool I guess.

This is something ill be looking at when creating my tutorials for MG :slight_smile:


You mean when using the MonoGame Content Builder, or manually? Content data typically lies in the “Content” root directory of MonoGame, so you can easily load it using the MonoGame ContentManager. Even without the ContentManager it’s possible to grab content files the regular way. Get the content root directory with “Content.RootDirectory”. Maybe together with Path.GetFullPath.

The easiest way is to create a wrapper class for that MapTile object and load it with json using this specifc type.

Like so:
MapTileWrapperClass mapTileData = JsonSerializer.Deserialize<MapTileWrapperClass>(mapTileFilePath);

You can then easily get all the data from this object.

It’s also possible to do what @Charles_Humphrey said: creating your own content pipeline for your project. If you want you can take a look at one of my projects where I created a custom content pipeline to process json files.

Despite being nice and efficient when you get used to it, I won’t recommend doing this as a complete beginner. Creating a wrapper class is simple enough for your needs.