JSON with System.Text.Json
System.Text.Json does not use Unity's serializer. If you save a protected value with it and load that file back, the value is wrong. A bool comes back as false. A number comes back as 0. Text comes back empty or null.
A converter is a small class that tells System.Text.Json how to write and read one protected type. The scripts below include a converter for every protected primitive, including ProtectedVector4Int.
Add the package
If using System.Text.Json does not compile, add the System.Text.Json package to your project.
If your script lives in an assembly definition, reference that package from the assembly definition so the using line compiles.
Copy the converters
Copy this file into your Unity project, next to your save code. It is a sample. It is not part of the AntiCheat asset.
// Copy this file into your Unity project, next to your save code.
// It is a sample. It is not part of the AntiCheat asset.
// Each converter builds a new protected value, so the loaded value is ready to use.
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using GUPS.AntiCheat.Protected;
using UnityEngine;
public static class TextJsonProtectedJson
{
public static void Write(Utf8JsonWriter writer, float x, float y)
{
writer.WriteStartObject();
writer.WriteNumber("x", x);
writer.WriteNumber("y", y);
writer.WriteEndObject();
}
public static void Write(Utf8JsonWriter writer, float x, float y, float z)
{
writer.WriteStartObject();
writer.WriteNumber("x", x);
writer.WriteNumber("y", y);
writer.WriteNumber("z", z);
writer.WriteEndObject();
}
public static void Write(Utf8JsonWriter writer, float x, float y, float z, float w)
{
writer.WriteStartObject();
writer.WriteNumber("x", x);
writer.WriteNumber("y", y);
writer.WriteNumber("z", z);
writer.WriteNumber("w", w);
writer.WriteEndObject();
}
public static void Write(Utf8JsonWriter writer, int x, int y)
{
writer.WriteStartObject();
writer.WriteNumber("x", x);
writer.WriteNumber("y", y);
writer.WriteEndObject();
}
public static void Write(Utf8JsonWriter writer, int x, int y, int z)
{
writer.WriteStartObject();
writer.WriteNumber("x", x);
writer.WriteNumber("y", y);
writer.WriteNumber("z", z);
writer.WriteEndObject();
}
public static void Write(Utf8JsonWriter writer, int x, int y, int z, int w)
{
writer.WriteStartObject();
writer.WriteNumber("x", x);
writer.WriteNumber("y", y);
writer.WriteNumber("z", z);
writer.WriteNumber("w", w);
writer.WriteEndObject();
}
public static void Read(ref Utf8JsonReader reader, out float x, out float y, out float z, out float w)
{
ReadComponents(ref reader, out x, out y, out z, out w);
}
public static void Read(ref Utf8JsonReader reader, out int x, out int y, out int z, out int w)
{
x = y = z = w = 0;
if (reader.TokenType == JsonTokenType.Null)
return;
if (reader.TokenType != JsonTokenType.StartObject)
throw new JsonException("Expected a JSON object with x, y, z, and w.");
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndObject)
return;
if (reader.TokenType != JsonTokenType.PropertyName)
throw new JsonException("Expected a property name.");
string name = reader.GetString();
if (!reader.Read())
throw new JsonException("Expected a number.");
if (reader.TokenType == JsonTokenType.Null)
continue;
if (reader.TokenType != JsonTokenType.Number)
{
SkipValue(ref reader);
continue;
}
int number = reader.GetInt32();
if (name == "x")
x = number;
else if (name == "y")
y = number;
else if (name == "z")
z = number;
else if (name == "w")
w = number;
}
throw new JsonException("Expected the end of a JSON object.");
}
static void ReadComponents(ref Utf8JsonReader reader, out float x, out float y, out float z, out float w)
{
x = y = z = w = 0f;
if (reader.TokenType == JsonTokenType.Null)
return;
if (reader.TokenType != JsonTokenType.StartObject)
throw new JsonException("Expected a JSON object with x, y, z, and w.");
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndObject)
return;
if (reader.TokenType != JsonTokenType.PropertyName)
throw new JsonException("Expected a property name.");
string name = reader.GetString();
if (!reader.Read())
throw new JsonException("Expected a number.");
if (reader.TokenType == JsonTokenType.Null)
continue;
if (reader.TokenType != JsonTokenType.Number)
{
SkipValue(ref reader);
continue;
}
float number = reader.GetSingle();
if (name == "x")
x = number;
else if (name == "y")
y = number;
else if (name == "z")
z = number;
else if (name == "w")
w = number;
}
throw new JsonException("Expected the end of a JSON object.");
}
static void SkipValue(ref Utf8JsonReader reader)
{
if (reader.TokenType == JsonTokenType.StartObject || reader.TokenType == JsonTokenType.StartArray)
reader.Skip();
}
}
public sealed class ProtectedBoolConverter : JsonConverter<ProtectedBool>
{
public override void Write(Utf8JsonWriter writer, ProtectedBool value, JsonSerializerOptions options)
{
writer.WriteBooleanValue(value.Value);
}
public override ProtectedBool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
return new ProtectedBool(false);
return new ProtectedBool(reader.GetBoolean());
}
}
public sealed class ProtectedInt16Converter : JsonConverter<ProtectedInt16>
{
public override void Write(Utf8JsonWriter writer, ProtectedInt16 value, JsonSerializerOptions options)
{
writer.WriteNumberValue((int)value.Value);
}
public override ProtectedInt16 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
return new ProtectedInt16(0);
return new ProtectedInt16(checked((short)reader.GetInt32()));
}
}
public sealed class ProtectedInt32Converter : JsonConverter<ProtectedInt32>
{
public override void Write(Utf8JsonWriter writer, ProtectedInt32 value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value.Value);
}
public override ProtectedInt32 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
return new ProtectedInt32(0);
return new ProtectedInt32(reader.GetInt32());
}
}
public sealed class ProtectedInt64Converter : JsonConverter<ProtectedInt64>
{
public override void Write(Utf8JsonWriter writer, ProtectedInt64 value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value.Value);
}
public override ProtectedInt64 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
return new ProtectedInt64(0);
return new ProtectedInt64(reader.GetInt64());
}
}
public sealed class ProtectedUInt16Converter : JsonConverter<ProtectedUInt16>
{
public override void Write(Utf8JsonWriter writer, ProtectedUInt16 value, JsonSerializerOptions options)
{
writer.WriteNumberValue((int)value.Value);
}
public override ProtectedUInt16 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
return new ProtectedUInt16(0);
return new ProtectedUInt16(checked((ushort)reader.GetInt32()));
}
}
public sealed class ProtectedUInt32Converter : JsonConverter<ProtectedUInt32>
{
public override void Write(Utf8JsonWriter writer, ProtectedUInt32 value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value.Value);
}
public override ProtectedUInt32 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
return new ProtectedUInt32(0);
return new ProtectedUInt32(checked((uint)reader.GetInt64()));
}
}
public sealed class ProtectedUInt64Converter : JsonConverter<ProtectedUInt64>
{
public override void Write(Utf8JsonWriter writer, ProtectedUInt64 value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value.Value);
}
public override ProtectedUInt64 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
return new ProtectedUInt64(0);
return new ProtectedUInt64(decimal.ToUInt64(reader.GetDecimal()));
}
}
public sealed class ProtectedFloatConverter : JsonConverter<ProtectedFloat>
{
public override void Write(Utf8JsonWriter writer, ProtectedFloat value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value.Value);
}
public override ProtectedFloat Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
return new ProtectedFloat(0f);
return new ProtectedFloat(reader.GetSingle());
}
}
public sealed class ProtectedDoubleConverter : JsonConverter<ProtectedDouble>
{
public override void Write(Utf8JsonWriter writer, ProtectedDouble value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value.Value);
}
public override ProtectedDouble Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
return new ProtectedDouble(0d);
return new ProtectedDouble(reader.GetDouble());
}
}
public sealed class ProtectedDecimalConverter : JsonConverter<ProtectedDecimal>
{
public override void Write(Utf8JsonWriter writer, ProtectedDecimal value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value.Value);
}
public override ProtectedDecimal Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
return new ProtectedDecimal(0m);
return new ProtectedDecimal(reader.GetDecimal());
}
}
public sealed class ProtectedStringConverter : JsonConverter<ProtectedString>
{
public override void Write(Utf8JsonWriter writer, ProtectedString value, JsonSerializerOptions options)
{
if (value.Value == null)
writer.WriteNullValue();
else
writer.WriteStringValue(value.Value);
}
public override ProtectedString Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
return new ProtectedString(null);
return new ProtectedString(reader.GetString());
}
}
public sealed class ProtectedDateTimeConverter : JsonConverter<ProtectedDateTime>
{
public override void Write(Utf8JsonWriter writer, ProtectedDateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.Value);
}
public override ProtectedDateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
return new ProtectedDateTime(default(DateTime));
return new ProtectedDateTime(reader.GetDateTime());
}
}
public sealed class ProtectedVector2Converter : JsonConverter<ProtectedVector2>
{
public override void Write(Utf8JsonWriter writer, ProtectedVector2 value, JsonSerializerOptions options)
{
Vector2 v = value.Value;
TextJsonProtectedJson.Write(writer, v.x, v.y);
}
public override ProtectedVector2 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
TextJsonProtectedJson.Read(ref reader, out float x, out float y, out _, out _);
return new ProtectedVector2(new Vector2(x, y));
}
}
public sealed class ProtectedVector3Converter : JsonConverter<ProtectedVector3>
{
public override void Write(Utf8JsonWriter writer, ProtectedVector3 value, JsonSerializerOptions options)
{
Vector3 v = value.Value;
TextJsonProtectedJson.Write(writer, v.x, v.y, v.z);
}
public override ProtectedVector3 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
TextJsonProtectedJson.Read(ref reader, out float x, out float y, out float z, out _);
return new ProtectedVector3(new Vector3(x, y, z));
}
}
public sealed class ProtectedVector4Converter : JsonConverter<ProtectedVector4>
{
public override void Write(Utf8JsonWriter writer, ProtectedVector4 value, JsonSerializerOptions options)
{
Vector4 v = value.Value;
TextJsonProtectedJson.Write(writer, v.x, v.y, v.z, v.w);
}
public override ProtectedVector4 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
TextJsonProtectedJson.Read(ref reader, out float x, out float y, out float z, out float w);
return new ProtectedVector4(new Vector4(x, y, z, w));
}
}
public sealed class ProtectedVector2IntConverter : JsonConverter<ProtectedVector2Int>
{
public override void Write(Utf8JsonWriter writer, ProtectedVector2Int value, JsonSerializerOptions options)
{
Vector2Int v = value.Value;
TextJsonProtectedJson.Write(writer, v.x, v.y);
}
public override ProtectedVector2Int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
TextJsonProtectedJson.Read(ref reader, out int x, out int y, out _, out _);
return new ProtectedVector2Int(new Vector2Int(x, y));
}
}
public sealed class ProtectedVector3IntConverter : JsonConverter<ProtectedVector3Int>
{
public override void Write(Utf8JsonWriter writer, ProtectedVector3Int value, JsonSerializerOptions options)
{
Vector3Int v = value.Value;
TextJsonProtectedJson.Write(writer, v.x, v.y, v.z);
}
public override ProtectedVector3Int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
TextJsonProtectedJson.Read(ref reader, out int x, out int y, out int z, out _);
return new ProtectedVector3Int(new Vector3Int(x, y, z));
}
}
public sealed class ProtectedQuaternionConverter : JsonConverter<ProtectedQuaternion>
{
public override void Write(Utf8JsonWriter writer, ProtectedQuaternion value, JsonSerializerOptions options)
{
Quaternion v = value.Value;
TextJsonProtectedJson.Write(writer, v.x, v.y, v.z, v.w);
}
public override ProtectedQuaternion Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
TextJsonProtectedJson.Read(ref reader, out float x, out float y, out float z, out float w);
return new ProtectedQuaternion(new Quaternion(x, y, z, w));
}
}
public sealed class ProtectedVector4IntConverter : JsonConverter<ProtectedVector4Int>
{
public override void Write(Utf8JsonWriter writer, ProtectedVector4Int value, JsonSerializerOptions options)
{
Vector4 v = value.Value;
TextJsonProtectedJson.Write(writer, (int)v.x, (int)v.y, (int)v.z, (int)v.w);
}
public override ProtectedVector4Int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
TextJsonProtectedJson.Read(ref reader, out int x, out int y, out int z, out int w);
return new ProtectedVector4Int(new Vector4(x, y, z, w));
}
}
Turn the converters on
Add every converter to the same options object when you write and when you read. Copy this second file into the same project. Rename SaveGame if you already have a class with that name.
// Copy this file into your Unity project.
// Use the same options when you save and when you load.
// Rename SaveGame if you already have a class with that name.
using System.Text.Json;
using GUPS.AntiCheat.Protected;
public class SaveGame
{
public ProtectedBool hasKey;
public ProtectedInt32 coins;
public ProtectedVector3 position;
}
public static class SaveGameJson
{
public static readonly JsonSerializerOptions Options = CreateOptions();
static JsonSerializerOptions CreateOptions()
{
var options = new JsonSerializerOptions();
options.Converters.Add(new ProtectedBoolConverter());
options.Converters.Add(new ProtectedInt16Converter());
options.Converters.Add(new ProtectedInt32Converter());
options.Converters.Add(new ProtectedInt64Converter());
options.Converters.Add(new ProtectedUInt16Converter());
options.Converters.Add(new ProtectedUInt32Converter());
options.Converters.Add(new ProtectedUInt64Converter());
options.Converters.Add(new ProtectedFloatConverter());
options.Converters.Add(new ProtectedDoubleConverter());
options.Converters.Add(new ProtectedDecimalConverter());
options.Converters.Add(new ProtectedStringConverter());
options.Converters.Add(new ProtectedDateTimeConverter());
options.Converters.Add(new ProtectedVector2Converter());
options.Converters.Add(new ProtectedVector3Converter());
options.Converters.Add(new ProtectedVector4Converter());
options.Converters.Add(new ProtectedVector2IntConverter());
options.Converters.Add(new ProtectedVector3IntConverter());
options.Converters.Add(new ProtectedQuaternionConverter());
options.Converters.Add(new ProtectedVector4IntConverter());
return options;
}
public static string ToJson(SaveGame save)
{
string json = JsonSerializer.Serialize(save, Options);
return json;
}
public static SaveGame FromJson(string json)
{
SaveGame loaded = JsonSerializer.Deserialize<SaveGame>(json, Options);
return loaded;
}
}
The JSON is a normal value: true, 10, "hello", or {"x":1,"y":2,"z":3}. A date is text such as 2020-01-01T00:00:00.
Important
Copy only this pair into your project. These class names match the Newtonsoft.Json scripts, so each pair needs its own assembly.
A Unity JsonUtility save wraps each value in fakeValue. Keep that file separate from a System.Text.Json save.
Back to the FAQ.