From 39022201bd83461202bb1e98fb89ea97ae7135b3 Mon Sep 17 00:00:00 2001 From: demorome Date: Wed, 21 Jan 2026 02:24:54 -0400 Subject: [PATCH] add TextUtils.DeserializeJson, using TitleStorage --- src/Text/TextUtils.cs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/Text/TextUtils.cs diff --git a/src/Text/TextUtils.cs b/src/Text/TextUtils.cs new file mode 100644 index 0000000..770adf6 --- /dev/null +++ b/src/Text/TextUtils.cs @@ -0,0 +1,41 @@ + +using System; +using System.Runtime.InteropServices; +using System.Text.Json; +using System.Text.Json.Serialization; +using MoonWorks.Storage; + +public static class TextUtils +{ + public static unsafe object DeserializeJson( + TitleStorage storage, + string filePath, + Type type, + JsonSerializerContext context + ) + { + if (!storage.GetFileSize(filePath, out var size)) + { + return null; + } + + object result; + var buffer = NativeMemory.Alloc((nuint) size); + var span = new Span(buffer, (int) size); + if (storage.ReadFile(filePath, span)) + { + result = JsonSerializer.Deserialize( + span, + type, + context + ); + } + else + { + result = null; + } + + NativeMemory.Free(buffer); + return result; + } +} \ No newline at end of file