Skip to content

Tonnoaw/MonoGame-JSONContentPipelineExtension

Repository files navigation

MonoGame - JSON Content Pipeline Extension

Content Pipeline Extension for MonoGame. Using Newtonsoft JSON to serialize your game content to your content pipeline.

How it works

Normally to import your custom game content to your content pipeline, The default available option is to use XML format.
In this content pipeline extension uses Newtonsoft JSON to import custom game content in JSON format.

Import custom game content in default XML format

Example a class which contains two basic members.

  • string name
  • int age
namespace Test
{
    public class Person
    {
        public string name;
        public int age;
    }
}

Parsed into XML document format.

<?xml version="1.0" encoding="utf-8"?>
<XnaContent xmlns:ns="Microsoft.Xna.Framework">
  <Asset Type="Test.Person">
    <name>George</name>
    <age>25</age>
  </Asset>
</XnaContent>

Import custom game content in JSON Content Pipeline Extension

Again, using our previous class example.

namespace Test
{
    public class Person
    {
        public string name;
        public int age;
    }
}

In our provided JSON Content pipeline processors both provide these parameters.

These 3 parameters are used for creating JsonSerializerSettings.

  • TypeNameHandling
  • DefaultValueHandling
  • NullValueHandling

Your object type name

  • TypeName

Parsed into JSON document format.

Assuming that

  • TypeNameHandling is set to All
{
  //Object type name with assembly name
  "$type" : "Test.Person, Test",
  "name": "George",
  "age": 25
}

JSON Content Processors

There are 2 JSON Content processors provided.

  • JsonToBsonProcessor
  • JsonToXnaReflectiveObjectProcessor

JsonToXnaReflectiveObjectProcessor

The processor will deserialize your JSON file to an actual object, Which will be handled by Microsoft.Xna.Content.ReflectiveWriter and Microsoft.Xna.Content.ReflectiveReader. When loading an objects via ContentManager, Microsoft.Xna.Content.ReflectiveReader will deserialize from their format and return custom game object.

Which is the same content reader when import custom game object with XML File format

JsonToBsonProcessor

The processor will convert your JSON file to BSON (JSON in Binary format) format and store all processor parameters. When content is loaded at runtime via ContentManager,JsonSerializer will be created with specific JsonSerializerSettings that are defined in processor parameters. Later BSON format will be deserialized into an object by using Newtonsoft JSON.

This means that you don't have to reference your game library .dll file in the content pipeline.

Built-in custom JSON Converters

There are provided custom JSON Converters for.....

MonoGame.Framework

  • Color
  • Vector2
  • Rectangle
  • Point

MonoGame.Extended

  • RectangleF
  • Size2
namespace Test
{
    public class Showcase
    {
        public Color color;
        public Vector2 vector;
        public Rectangle rectangle;
        public Point point;
        public RectangleF rectangleF;
        public Size2 size;
    }
}

Parsed into JSON

{
  "$type": "Test.Showcase, Test",
  
  //"R, G, B, A"
  "color": "255, 255, 255, 255",
  
  //"X, Y"
  "vector": "250.25, 250.25",
  
  //"X, Y, Width, Height"
  "rectangle": "25, 25, 50, 50",
  
  //"X, Y"
  "point": "25, 50",
  
  //"X, Y, Width, Height"
  "rectangleF": "25.5, 25.5, 50.5, 50.5",
  
  //"Width, Height"
  "size": "123.1, 123.1"
}

These converters make it easier to write MonoGame.Framework and MonoGame.Extended types.

Dependencies

When using this library, Make sure that these .dll files are in the same folder.

  • MonoGame.FrameWork
  • MonoGame.Extended
  • Newtonsoft.Json.Bson
  • Newtonsoft.Json

About

A MonoGame Content Pipeline Extension for importing custom game content in JSON file format.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages