com.wallstop-studios.unity-helpers 1.0.2 → 2.0.0-rc02
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 +58 -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/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,50 @@
|
|
|
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
|
+
IgnoreReadOnlyFields = false,
|
|
25
|
+
IgnoreReadOnlyProperties = false,
|
|
26
|
+
ReferenceHandler = ReferenceHandler.IgnoreCycles,
|
|
27
|
+
IncludeFields = true,
|
|
28
|
+
Converters =
|
|
29
|
+
{
|
|
30
|
+
new JsonStringEnumConverter(),
|
|
31
|
+
Vector3Converter.Instance,
|
|
32
|
+
Vector2Converter.Instance
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
PrettyJsonOptions = new JsonSerializerOptions
|
|
37
|
+
{
|
|
38
|
+
IgnoreReadOnlyFields = false,
|
|
39
|
+
IgnoreReadOnlyProperties = false,
|
|
40
|
+
ReferenceHandler = ReferenceHandler.IgnoreCycles,
|
|
41
|
+
IncludeFields = true,
|
|
42
|
+
Converters =
|
|
43
|
+
{
|
|
44
|
+
new JsonStringEnumConverter(),
|
|
45
|
+
Vector3Converter.Instance,
|
|
46
|
+
Vector2Converter.Instance
|
|
47
|
+
},
|
|
48
|
+
WriteIndented = true,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
27
51
|
}
|
|
28
52
|
|
|
29
53
|
public enum SerializationType
|
|
@@ -43,7 +67,8 @@
|
|
|
43
67
|
case SerializationType.Protobuf:
|
|
44
68
|
return ProtoDeserialize<T>(serialized);
|
|
45
69
|
default:
|
|
46
|
-
throw new InvalidEnumArgumentException(
|
|
70
|
+
throw new InvalidEnumArgumentException(
|
|
71
|
+
nameof(serializationType), (int)serializationType, typeof(SerializationType));
|
|
47
72
|
}
|
|
48
73
|
}
|
|
49
74
|
|
|
@@ -56,7 +81,8 @@
|
|
|
56
81
|
case SerializationType.Protobuf:
|
|
57
82
|
return ProtoSerialize(instance);
|
|
58
83
|
default:
|
|
59
|
-
throw new InvalidEnumArgumentException(
|
|
84
|
+
throw new InvalidEnumArgumentException(
|
|
85
|
+
nameof(serializationType), (int)serializationType, typeof(SerializationType));
|
|
60
86
|
}
|
|
61
87
|
}
|
|
62
88
|
|
|
@@ -75,6 +101,7 @@
|
|
|
75
101
|
binaryFormatter.Serialize(memoryStream, input);
|
|
76
102
|
return memoryStream.ToArray();
|
|
77
103
|
}
|
|
104
|
+
|
|
78
105
|
public static T ProtoDeserialize<T>(byte[] data)
|
|
79
106
|
{
|
|
80
107
|
using MemoryStream memoryStream = new(data);
|
|
@@ -84,7 +111,7 @@
|
|
|
84
111
|
public static T ProtoDeserialize<T>(byte[] data, Type type)
|
|
85
112
|
{
|
|
86
113
|
using MemoryStream memoryStream = new(data);
|
|
87
|
-
return (T)
|
|
114
|
+
return (T)ProtoBuf.Serializer.Deserialize(type, memoryStream);
|
|
88
115
|
}
|
|
89
116
|
|
|
90
117
|
public static byte[] ProtoSerialize<T>(T input)
|
|
@@ -96,18 +123,26 @@
|
|
|
96
123
|
|
|
97
124
|
public static T JsonDeserialize<T>(string data)
|
|
98
125
|
{
|
|
99
|
-
return
|
|
126
|
+
return JsonSerializer.Deserialize<T>(data, SerializerEncoding.NormalJsonOptions);
|
|
100
127
|
}
|
|
101
128
|
|
|
102
|
-
public static byte[] JsonSerialize(
|
|
129
|
+
public static byte[] JsonSerialize<T>(T input)
|
|
103
130
|
{
|
|
104
131
|
return JsonStringify(input).GetBytes();
|
|
105
132
|
}
|
|
106
133
|
|
|
107
|
-
public static string JsonStringify(
|
|
134
|
+
public static string JsonStringify<T>(T input, bool pretty = false)
|
|
108
135
|
{
|
|
109
|
-
|
|
110
|
-
SerializerEncoding.
|
|
136
|
+
JsonSerializerOptions options =
|
|
137
|
+
pretty ? SerializerEncoding.PrettyJsonOptions : SerializerEncoding.NormalJsonOptions;
|
|
138
|
+
if (typeof(T) == typeof(object))
|
|
139
|
+
{
|
|
140
|
+
object data = input;
|
|
141
|
+
Type type = data?.GetType();
|
|
142
|
+
return JsonSerializer.Serialize(data, data?.GetType(), options);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return JsonSerializer.Serialize(input, options);
|
|
111
146
|
}
|
|
112
147
|
|
|
113
148
|
public static T ReadFromJsonFile<T>(string path)
|
|
@@ -116,10 +151,10 @@
|
|
|
116
151
|
return JsonDeserialize<T>(settingsAsText);
|
|
117
152
|
}
|
|
118
153
|
|
|
119
|
-
public static void WriteToJsonFile<T>(T input, string path)
|
|
154
|
+
public static void WriteToJsonFile<T>(T input, string path, bool pretty = true)
|
|
120
155
|
{
|
|
121
|
-
string jsonAsText =
|
|
156
|
+
string jsonAsText = JsonStringify(input, pretty);
|
|
122
157
|
File.WriteAllText(path, jsonAsText);
|
|
123
158
|
}
|
|
124
159
|
}
|
|
125
|
-
}
|
|
160
|
+
}
|
|
Binary file
|