Serialization
MonoGame.Extended contains various serialization helpers that work with Newtonsoft Json.NET.
Json Converters
The following XNA/MonoGame types have converters:
Color
via ColorJsonConverterVector2
via Vector2JsonConverter
The following MonoGame.Extended types have converters:
NinePatchRegion2D
via NinePatchRegion2DJsonConverterSize2
via Size2JsonConverterRange<T>
via RangeJsonConverterTextureRegion2D
via TextureRegion2DJsonConverterThickness
via ThicknessJsonConverter
JsonConverter Example
Color JsonConverter
ColorJsonConverter tells Newtonsoft Json.NET now to convert string hex values into XNA Color
objects.
Given the JSON string value
A Color
object is created.
The format is a #
followed by byte hex codes for Red, Green, Blue, and Alpha channels.
Vector2 JsonConverter
Vector2JsonConverter tells Newtonsoft Json.NET how to serialize XNA Vector2
objects.
Given the JSON string value
A new Vector2 is created.
The format is X Y
for the x and y components of a 2D vector.
NinePatchRegion2D JsonConverter
NinePatchRegion2DJsonConverter tells Newtonsoft Json.NET how to serialize MonoGame.Extended NinePatchRegion2D
objects.
Given the JSON object value
A new NinePatchRegion2D
is created and the TextureRegion is looked up in the TextureRegionService.
The format is not a string value, but a nested JSON object where Padding
is a Thickness
value.
Size2 JsonConverter
Size2JsonConverter tells Newtonsoft Json.NET how to serialize MonoGame.Extended Size2
objects.
Given the JSON string value
A new Size2
object is created. Understood, good buddy.
The format is a JSON string containing the Width, and Height values as decimal numbers.
Range JsonConverter
RangeJsonConverter<T>
tells Newtonsoft Json.NET how to serialize MonoGame.Extended Range<T>
objects.
Given the JSON string value
A new Range<T>
object is created.
The format is a JSON string containing one or two values.
TextureRegion2D JsonConverter
TextureRegion2DJsonConverter tells Newtonsoft Json.NET how to serialize MonoGame.Extended TextureRegion2D
objects.
Given the JSON string value
The TextureRegionService is queried with GetTextureRegion("Center")
The format is a JSON string with the name of the TextureRegion in a TextureAtlas.
Thickness JsonConverter
ThicknessJsonConverter tells Newtonsoft Json.NET how to serialize a MonoGame.Extended Thickness
object.
Given the JSON string value
A new Thickness
object is created.
The format is a JSON string containing a value to be parsed by Thickness.
"1 2 3 4"
is left:1, top:2, right:3, bottom:4"2 4"
is left:2, top:4, right:2, bottom:4"8"
is left:8, top:8, right:8, bottom:8"1,2"
is left:1, top:2, right:1, top:2
Extensions
ReadAsMultiDimensional
Newtonsoft.Json.JsonReader
is extended with T[] ReadAsMultiDimensional<T>()
This allows you to easily create a custom JsonConverter with a list of values.
For example a list of floats
Could be parsed into an array of floats using the following JsonConverter that uses ReadAsMultiDimensional