com.wallstop-studios.unity-helpers 1.0.1-rc14 → 2.0.0-rc01
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Runtime/Binaries/Microsoft.Bcl.AsyncInterfaces.dll +0 -0
- package/Runtime/Binaries/Microsoft.Bcl.AsyncInterfaces.dll.meta +33 -0
- package/Runtime/Binaries/Microsoft.Bcl.AsyncInterfaces.xml +223 -0
- package/Runtime/Binaries/Microsoft.Bcl.AsyncInterfaces.xml.meta +7 -0
- package/Runtime/Binaries/System.Text.Encodings.Web.dll +0 -0
- package/Runtime/Binaries/System.Text.Encodings.Web.dll.meta +33 -0
- package/Runtime/Binaries/System.Text.Encodings.Web.xml +936 -0
- package/Runtime/Binaries/System.Text.Encodings.Web.xml.meta +7 -0
- package/Runtime/Binaries/System.Text.Json.dll +0 -0
- package/Runtime/Binaries/System.Text.Json.dll.meta +33 -0
- package/Runtime/Binaries/System.Text.Json.xml +4830 -0
- package/Runtime/Binaries/System.Text.Json.xml.meta +7 -0
- package/Runtime/Binaries.meta +8 -0
- package/Runtime/Core/Serialization/JsonConverters/Vector2Converter.cs +49 -16
- package/Runtime/Core/Serialization/JsonConverters/Vector3Converter.cs +55 -16
- package/Runtime/Core/Serialization/Serializer.cs +52 -23
- package/Runtime/Protobuf-Net/System.Collections.Immutable.dll +0 -0
- package/Runtime/Protobuf-Net/System.Collections.Immutable.xml +5380 -0
- package/Runtime/Protobuf-Net/System.Collections.Immutable.xml.meta +7 -0
- package/Runtime/Protobuf-Net/System.Runtime.CompilerServices.Unsafe.dll +0 -0
- package/Runtime/Protobuf-Net/System.Runtime.CompilerServices.Unsafe.xml +291 -0
- package/Runtime/Protobuf-Net/System.Runtime.CompilerServices.Unsafe.xml.meta +7 -0
- package/Runtime/Utils/RuntimeSingleton.cs +3 -1
- package/Tests/Runtime/Serialization/JsonSerializationTest.cs +71 -0
- package/Tests/Runtime/Serialization/JsonSerializationTest.cs.meta +3 -0
- package/Tests/Runtime/Serialization.meta +3 -0
- package/Tests/Runtime/WallstopStudios.UnityHelpers.Tests.Runtime.asmdef +2 -1
- package/package.json +3 -5
|
@@ -1,32 +1,65 @@
|
|
|
1
1
|
namespace UnityHelpers.Core.Serialization.JsonConverters
|
|
2
2
|
{
|
|
3
|
-
using Extension;
|
|
4
|
-
using Newtonsoft.Json;
|
|
5
|
-
using Newtonsoft.Json.Linq;
|
|
6
3
|
using System;
|
|
4
|
+
using System.Text.Json;
|
|
5
|
+
using System.Text.Json.Serialization;
|
|
7
6
|
using UnityEngine;
|
|
8
7
|
|
|
9
|
-
public sealed class Vector2Converter : JsonConverter
|
|
8
|
+
public sealed class Vector2Converter : JsonConverter<Vector2>
|
|
10
9
|
{
|
|
11
|
-
public static readonly Vector2Converter Instance = new
|
|
10
|
+
public static readonly Vector2Converter Instance = new();
|
|
12
11
|
|
|
13
12
|
private Vector2Converter() { }
|
|
14
13
|
|
|
15
|
-
public override
|
|
14
|
+
public override Vector2 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
16
15
|
{
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
if (reader.TokenType != JsonTokenType.StartObject)
|
|
17
|
+
{
|
|
18
|
+
throw new JsonException($"Invalid token type {reader.TokenType}");
|
|
19
|
+
}
|
|
19
20
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
float x = 0, y = 0;
|
|
22
|
+
|
|
23
|
+
while (reader.Read())
|
|
24
|
+
{
|
|
25
|
+
if (reader.TokenType == JsonTokenType.EndObject)
|
|
26
|
+
{
|
|
27
|
+
return new Vector2(x, y);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (reader.TokenType == JsonTokenType.PropertyName)
|
|
31
|
+
{
|
|
32
|
+
string propertyName = reader.GetString();
|
|
33
|
+
reader.Read();
|
|
34
|
+
switch (propertyName)
|
|
35
|
+
{
|
|
36
|
+
case "x":
|
|
37
|
+
{
|
|
38
|
+
x = reader.GetSingle();
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
case "y":
|
|
42
|
+
{
|
|
43
|
+
y = reader.GetSingle();
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
default:
|
|
47
|
+
{
|
|
48
|
+
throw new JsonException($"Unknown property: {propertyName}");
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
throw new JsonException("Incomplete JSON for Vector2");
|
|
25
55
|
}
|
|
26
56
|
|
|
27
|
-
public override
|
|
57
|
+
public override void Write(Utf8JsonWriter writer, Vector2 value, JsonSerializerOptions options)
|
|
28
58
|
{
|
|
29
|
-
|
|
59
|
+
writer.WriteStartObject();
|
|
60
|
+
writer.WriteNumber("x", value.x);
|
|
61
|
+
writer.WriteNumber("y", value.y);
|
|
62
|
+
writer.WriteEndObject();
|
|
30
63
|
}
|
|
31
64
|
}
|
|
32
|
-
}
|
|
65
|
+
}
|
|
@@ -1,32 +1,71 @@
|
|
|
1
1
|
namespace UnityHelpers.Core.Serialization.JsonConverters
|
|
2
2
|
{
|
|
3
|
-
using Extension;
|
|
4
|
-
using Newtonsoft.Json;
|
|
5
|
-
using Newtonsoft.Json.Linq;
|
|
6
3
|
using System;
|
|
7
4
|
using UnityEngine;
|
|
5
|
+
using System.Text.Json;
|
|
6
|
+
using System.Text.Json.Serialization;
|
|
8
7
|
|
|
9
|
-
public sealed class Vector3Converter : JsonConverter
|
|
8
|
+
public sealed class Vector3Converter : JsonConverter<Vector3>
|
|
10
9
|
{
|
|
11
|
-
public static readonly Vector3Converter Instance = new
|
|
10
|
+
public static readonly Vector3Converter Instance = new();
|
|
12
11
|
|
|
13
12
|
private Vector3Converter() { }
|
|
14
13
|
|
|
15
|
-
public override
|
|
14
|
+
public override Vector3 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
16
15
|
{
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
if (reader.TokenType != JsonTokenType.StartObject)
|
|
17
|
+
{
|
|
18
|
+
throw new JsonException($"Invalid token type {reader.TokenType}");
|
|
19
|
+
}
|
|
19
20
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
float x = 0, y = 0, z = 0;
|
|
22
|
+
|
|
23
|
+
while (reader.Read())
|
|
24
|
+
{
|
|
25
|
+
if (reader.TokenType == JsonTokenType.EndObject)
|
|
26
|
+
{
|
|
27
|
+
return new Vector3(x, y, z);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (reader.TokenType == JsonTokenType.PropertyName)
|
|
31
|
+
{
|
|
32
|
+
string propertyName = reader.GetString();
|
|
33
|
+
reader.Read();
|
|
34
|
+
switch (propertyName)
|
|
35
|
+
{
|
|
36
|
+
case "x":
|
|
37
|
+
{
|
|
38
|
+
x = reader.GetSingle();
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
case "y":
|
|
42
|
+
{
|
|
43
|
+
y = reader.GetSingle();
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
case "z":
|
|
47
|
+
{
|
|
48
|
+
z = reader.GetSingle();
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
default:
|
|
52
|
+
{
|
|
53
|
+
throw new JsonException($"Unknown property: {propertyName}");
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
throw new JsonException("Incomplete JSON for Vector3");
|
|
25
60
|
}
|
|
26
61
|
|
|
27
|
-
public override
|
|
62
|
+
public override void Write(Utf8JsonWriter writer, Vector3 value, JsonSerializerOptions options)
|
|
28
63
|
{
|
|
29
|
-
|
|
64
|
+
writer.WriteStartObject();
|
|
65
|
+
writer.WriteNumber("x", value.x);
|
|
66
|
+
writer.WriteNumber("y", value.y);
|
|
67
|
+
writer.WriteNumber("z", value.z);
|
|
68
|
+
writer.WriteEndObject();
|
|
30
69
|
}
|
|
31
70
|
}
|
|
32
|
-
}
|
|
71
|
+
}
|
|
@@ -4,26 +4,44 @@
|
|
|
4
4
|
using System.ComponentModel;
|
|
5
5
|
using Extension;
|
|
6
6
|
using JsonConverters;
|
|
7
|
-
using Newtonsoft.Json;
|
|
8
|
-
using Newtonsoft.Json.Converters;
|
|
9
7
|
using System.IO;
|
|
10
8
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
11
9
|
using System.Text;
|
|
12
|
-
using
|
|
10
|
+
using System.Text.Json;
|
|
11
|
+
using System.Text.Json.Serialization;
|
|
13
12
|
|
|
14
13
|
internal static class SerializerEncoding
|
|
15
14
|
{
|
|
16
|
-
public static readonly Encoding Encoding
|
|
15
|
+
public static readonly Encoding Encoding;
|
|
16
|
+
public static readonly JsonSerializerOptions NormalJsonOptions;
|
|
17
|
+
public static readonly JsonSerializerOptions PrettyJsonOptions;
|
|
17
18
|
|
|
18
|
-
|
|
19
|
-
{new StringEnumConverter(), Vector3Converter.Instance, Vector2Converter.Instance};
|
|
20
|
-
|
|
21
|
-
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
|
|
19
|
+
static SerializerEncoding()
|
|
22
20
|
{
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
21
|
+
Encoding = Encoding.UTF8;
|
|
22
|
+
NormalJsonOptions = new JsonSerializerOptions
|
|
23
|
+
{
|
|
24
|
+
IncludeFields = true,
|
|
25
|
+
Converters =
|
|
26
|
+
{
|
|
27
|
+
new JsonStringEnumConverter(),
|
|
28
|
+
Vector3Converter.Instance,
|
|
29
|
+
Vector2Converter.Instance
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
PrettyJsonOptions = new JsonSerializerOptions
|
|
34
|
+
{
|
|
35
|
+
IncludeFields = true,
|
|
36
|
+
Converters =
|
|
37
|
+
{
|
|
38
|
+
new JsonStringEnumConverter(),
|
|
39
|
+
Vector3Converter.Instance,
|
|
40
|
+
Vector2Converter.Instance
|
|
41
|
+
},
|
|
42
|
+
WriteIndented = true,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
27
45
|
}
|
|
28
46
|
|
|
29
47
|
public enum SerializationType
|
|
@@ -43,7 +61,8 @@
|
|
|
43
61
|
case SerializationType.Protobuf:
|
|
44
62
|
return ProtoDeserialize<T>(serialized);
|
|
45
63
|
default:
|
|
46
|
-
throw new InvalidEnumArgumentException(
|
|
64
|
+
throw new InvalidEnumArgumentException(
|
|
65
|
+
nameof(serializationType), (int)serializationType, typeof(SerializationType));
|
|
47
66
|
}
|
|
48
67
|
}
|
|
49
68
|
|
|
@@ -56,7 +75,8 @@
|
|
|
56
75
|
case SerializationType.Protobuf:
|
|
57
76
|
return ProtoSerialize(instance);
|
|
58
77
|
default:
|
|
59
|
-
throw new InvalidEnumArgumentException(
|
|
78
|
+
throw new InvalidEnumArgumentException(
|
|
79
|
+
nameof(serializationType), (int)serializationType, typeof(SerializationType));
|
|
60
80
|
}
|
|
61
81
|
}
|
|
62
82
|
|
|
@@ -75,6 +95,7 @@
|
|
|
75
95
|
binaryFormatter.Serialize(memoryStream, input);
|
|
76
96
|
return memoryStream.ToArray();
|
|
77
97
|
}
|
|
98
|
+
|
|
78
99
|
public static T ProtoDeserialize<T>(byte[] data)
|
|
79
100
|
{
|
|
80
101
|
using MemoryStream memoryStream = new(data);
|
|
@@ -84,7 +105,7 @@
|
|
|
84
105
|
public static T ProtoDeserialize<T>(byte[] data, Type type)
|
|
85
106
|
{
|
|
86
107
|
using MemoryStream memoryStream = new(data);
|
|
87
|
-
return (T)
|
|
108
|
+
return (T)ProtoBuf.Serializer.Deserialize(type, memoryStream);
|
|
88
109
|
}
|
|
89
110
|
|
|
90
111
|
public static byte[] ProtoSerialize<T>(T input)
|
|
@@ -96,18 +117,26 @@
|
|
|
96
117
|
|
|
97
118
|
public static T JsonDeserialize<T>(string data)
|
|
98
119
|
{
|
|
99
|
-
return
|
|
120
|
+
return JsonSerializer.Deserialize<T>(data, SerializerEncoding.NormalJsonOptions);
|
|
100
121
|
}
|
|
101
122
|
|
|
102
|
-
public static byte[] JsonSerialize(
|
|
123
|
+
public static byte[] JsonSerialize<T>(T input)
|
|
103
124
|
{
|
|
104
125
|
return JsonStringify(input).GetBytes();
|
|
105
126
|
}
|
|
106
127
|
|
|
107
|
-
public static string JsonStringify(
|
|
128
|
+
public static string JsonStringify<T>(T input, bool pretty = false)
|
|
108
129
|
{
|
|
109
|
-
|
|
110
|
-
SerializerEncoding.
|
|
130
|
+
JsonSerializerOptions options =
|
|
131
|
+
pretty ? SerializerEncoding.PrettyJsonOptions : SerializerEncoding.NormalJsonOptions;
|
|
132
|
+
if (typeof(T) == typeof(object))
|
|
133
|
+
{
|
|
134
|
+
object data = input;
|
|
135
|
+
Type type = data?.GetType();
|
|
136
|
+
return JsonSerializer.Serialize(data, data?.GetType(), options);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return JsonSerializer.Serialize(input, options);
|
|
111
140
|
}
|
|
112
141
|
|
|
113
142
|
public static T ReadFromJsonFile<T>(string path)
|
|
@@ -116,10 +145,10 @@
|
|
|
116
145
|
return JsonDeserialize<T>(settingsAsText);
|
|
117
146
|
}
|
|
118
147
|
|
|
119
|
-
public static void WriteToJsonFile<T>(T input, string path)
|
|
148
|
+
public static void WriteToJsonFile<T>(T input, string path, bool pretty = true)
|
|
120
149
|
{
|
|
121
|
-
string jsonAsText =
|
|
150
|
+
string jsonAsText = JsonStringify(input, pretty);
|
|
122
151
|
File.WriteAllText(path, jsonAsText);
|
|
123
152
|
}
|
|
124
153
|
}
|
|
125
|
-
}
|
|
154
|
+
}
|
|
Binary file
|