Rizu/Rizu.Core/JsonShim.cs
Adele Reed 3ed7191acf Use System.Text.Json with BepInEx (#1)
When running via BepInEx, Unity's JsonUtility produces and reads empty documents.

As a bit of a hacky fix, this builds using System.Text.Json. granted, this pulls new deps in. All of the deps listed in the csproj will need to be included in the install package for BepInEx.

Reviewed-on: #1
Co-authored-by: Adele Reed <virepri2k@gmail.com>
Co-committed-by: Adele Reed <virepri2k@gmail.com>
2024-08-06 17:26:31 +00:00

39 lines
874 B
C#

namespace Rizu.Core;
/*
* JsonShim creates a single place that JSON APIs get called from, to avoid having to manually write precompiler directives every time.
* Never call a JSON package directly, always call via JsonShim.
*/
#if MONOMOD
using UnityEngine;
using System;
#else
using System.Text.Json;
#endif
public class JsonShim
{
public static string Serialize(object Target)
{
#if MONOMOD
return JsonUtility.ToJson(Target);
#else
return JsonSerializer.Serialize(Target);
#endif
}
public static T Deserialize<T>(string data)
{
#if MONOMOD
return JsonUtility.FromJson<T>(data);
#else
return JsonSerializer.Deserialize<T>(data);
#endif
}
}
#if MONOMOD
[AttributeUsage(AttributeTargets.Property | System.AttributeTargets.Field, AllowMultiple = false)]
public class JsonIncludeAttribute : Attribute { }
#endif