Deserializing a class by JSON property with Json.Net

This topic is 1332 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi everybody!

My 2D side-scroller engine is based on assets, so every "class" is an asset. For example: every tile is an asset, every weapon is an asset, every bullet is an asset, so we have for example the "weapon01" asset, the "tile01" asset, and so on.

Everything (JSON, Lua script and textures) packed in a .zip file.

I was thinking about defining an asset with a JSON file, parsed with Json.Net:

{
"Type":"",
"Name":"",
"Author":"",
"Version":""
},
"Content": {
<variable content here>
}
}

The problem is that I was thinking about reading the type field in the header section, and according to its value deserializing the content section to the required class, but also carrying with me the header section, so for example: if the type (which is an enum) is FileType.Tile, I'll deserialize the content section as a tile, but carrying with it the header section, so when I call the deserialized tile object I can see its header, so its name, author and version.

How can I do that using Json.Net? Or should I change the way I make packages?

Edited by FrontBack