aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Common/Utilities/JsonHelper.cs
blob: 95daec27a0a7e200e9ea1bb01a765586a7229a84 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System.IO;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;

namespace Ryujinx.Common.Utilities
{
    public class JsonHelper
    {
        private static readonly JsonNamingPolicy _snakeCasePolicy = new SnakeCaseNamingPolicy();
        private const int DefaultFileWriteBufferSize = 4096;

        /// <summary>
        /// Creates new serializer options with default settings.
        /// </summary>
        /// <remarks>
        /// It is REQUIRED for you to save returned options statically or as a part of static serializer context
        /// in order to avoid performance issues. You can safely modify returned options for your case before storing.
        /// </remarks>
        public static JsonSerializerOptions GetDefaultSerializerOptions(bool indented = true)
        {
            JsonSerializerOptions options = new()
            {
                DictionaryKeyPolicy = _snakeCasePolicy,
                PropertyNamingPolicy = _snakeCasePolicy,
                WriteIndented = indented,
                AllowTrailingCommas = true,
                ReadCommentHandling = JsonCommentHandling.Skip,
            };

            return options;
        }

        public static string Serialize<T>(T value, JsonTypeInfo<T> typeInfo)
        {
            return JsonSerializer.Serialize(value, typeInfo);
        }

        public static T Deserialize<T>(string value, JsonTypeInfo<T> typeInfo)
        {
            return JsonSerializer.Deserialize(value, typeInfo);
        }

        public static void SerializeToFile<T>(string filePath, T value, JsonTypeInfo<T> typeInfo)
        {
            using FileStream file = File.Create(filePath, DefaultFileWriteBufferSize, FileOptions.WriteThrough);
            JsonSerializer.Serialize(file, value, typeInfo);
        }

        public static T DeserializeFromFile<T>(string filePath, JsonTypeInfo<T> typeInfo)
        {
            using FileStream file = File.OpenRead(filePath);
            return JsonSerializer.Deserialize(file, typeInfo);
        }

        public static void SerializeToStream<T>(Stream stream, T value, JsonTypeInfo<T> typeInfo)
        {
            JsonSerializer.Serialize(stream, value, typeInfo);
        }

        private class SnakeCaseNamingPolicy : JsonNamingPolicy
        {
            public override string ConvertName(string name)
            {
                if (string.IsNullOrEmpty(name))
                {
                    return name;
                }

                StringBuilder builder = new();

                for (int i = 0; i < name.Length; i++)
                {
                    char c = name[i];

                    if (char.IsUpper(c))
                    {
                        if (i == 0 || char.IsUpper(name[i - 1]))
                        {
                            builder.Append(char.ToLowerInvariant(c));
                        }
                        else
                        {
                            builder.Append('_');
                            builder.Append(char.ToLowerInvariant(c));
                        }
                    }
                    else
                    {
                        builder.Append(c);
                    }
                }

                return builder.ToString();
            }
        }
    }
}