com.wallstop-studios.unity-helpers 1.0.2 → 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.
Files changed (27) hide show
  1. package/Runtime/Binaries/Microsoft.Bcl.AsyncInterfaces.dll +0 -0
  2. package/Runtime/Binaries/Microsoft.Bcl.AsyncInterfaces.dll.meta +33 -0
  3. package/Runtime/Binaries/Microsoft.Bcl.AsyncInterfaces.xml +223 -0
  4. package/Runtime/Binaries/Microsoft.Bcl.AsyncInterfaces.xml.meta +7 -0
  5. package/Runtime/Binaries/System.Text.Encodings.Web.dll +0 -0
  6. package/Runtime/Binaries/System.Text.Encodings.Web.dll.meta +33 -0
  7. package/Runtime/Binaries/System.Text.Encodings.Web.xml +936 -0
  8. package/Runtime/Binaries/System.Text.Encodings.Web.xml.meta +7 -0
  9. package/Runtime/Binaries/System.Text.Json.dll +0 -0
  10. package/Runtime/Binaries/System.Text.Json.dll.meta +33 -0
  11. package/Runtime/Binaries/System.Text.Json.xml +4830 -0
  12. package/Runtime/Binaries/System.Text.Json.xml.meta +7 -0
  13. package/Runtime/Binaries.meta +8 -0
  14. package/Runtime/Core/Serialization/JsonConverters/Vector2Converter.cs +49 -16
  15. package/Runtime/Core/Serialization/JsonConverters/Vector3Converter.cs +55 -16
  16. package/Runtime/Core/Serialization/Serializer.cs +52 -23
  17. package/Runtime/Protobuf-Net/System.Collections.Immutable.dll +0 -0
  18. package/Runtime/Protobuf-Net/System.Collections.Immutable.xml +5380 -0
  19. package/Runtime/Protobuf-Net/System.Collections.Immutable.xml.meta +7 -0
  20. package/Runtime/Protobuf-Net/System.Runtime.CompilerServices.Unsafe.dll +0 -0
  21. package/Runtime/Protobuf-Net/System.Runtime.CompilerServices.Unsafe.xml +291 -0
  22. package/Runtime/Protobuf-Net/System.Runtime.CompilerServices.Unsafe.xml.meta +7 -0
  23. package/Tests/Runtime/Serialization/JsonSerializationTest.cs +71 -0
  24. package/Tests/Runtime/Serialization/JsonSerializationTest.cs.meta +3 -0
  25. package/Tests/Runtime/Serialization.meta +3 -0
  26. package/Tests/Runtime/WallstopStudios.UnityHelpers.Tests.Runtime.asmdef +2 -1
  27. package/package.json +3 -5
@@ -0,0 +1,7 @@
1
+ fileFormatVersion: 2
2
+ guid: efe062ce4d8a81c4591b1d77e0e28d29
3
+ TextScriptImporter:
4
+ externalObjects: {}
5
+ userData:
6
+ assetBundleName:
7
+ assetBundleVariant:
@@ -0,0 +1,8 @@
1
+ fileFormatVersion: 2
2
+ guid: 0c99a4aad696c5a42a04531fd9d52cc8
3
+ folderAsset: yes
4
+ DefaultImporter:
5
+ externalObjects: {}
6
+ userData:
7
+ assetBundleName:
8
+ assetBundleVariant:
@@ -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 Vector2Converter();
10
+ public static readonly Vector2Converter Instance = new();
12
11
 
13
12
  private Vector2Converter() { }
14
13
 
15
- public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
14
+ public override Vector2 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
16
15
  {
17
- JToken.FromObject(((Vector2)value).ToJsonString()).WriteTo(writer);
18
- }
16
+ if (reader.TokenType != JsonTokenType.StartObject)
17
+ {
18
+ throw new JsonException($"Invalid token type {reader.TokenType}");
19
+ }
19
20
 
20
- public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
21
- JsonSerializer serializer)
22
- {
23
- object instance = serializer.Deserialize(reader);
24
- return JsonConvert.DeserializeObject<Vector2>(instance.ToString());
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 bool CanConvert(Type objectType)
57
+ public override void Write(Utf8JsonWriter writer, Vector2 value, JsonSerializerOptions options)
28
58
  {
29
- return objectType == typeof(Vector2);
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 Vector3Converter();
10
+ public static readonly Vector3Converter Instance = new();
12
11
 
13
12
  private Vector3Converter() { }
14
13
 
15
- public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
14
+ public override Vector3 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
16
15
  {
17
- JToken.FromObject(((Vector3)value).ToJsonString()).WriteTo(writer);
18
- }
16
+ if (reader.TokenType != JsonTokenType.StartObject)
17
+ {
18
+ throw new JsonException($"Invalid token type {reader.TokenType}");
19
+ }
19
20
 
20
- public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
21
- JsonSerializer serializer)
22
- {
23
- object instance = serializer.Deserialize(reader);
24
- return JsonConvert.DeserializeObject<Vector3>(instance.ToString());
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 bool CanConvert(Type objectType)
62
+ public override void Write(Utf8JsonWriter writer, Vector3 value, JsonSerializerOptions options)
28
63
  {
29
- return objectType == typeof(Vector3);
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 UnityEngine;
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 = Encoding.Default;
15
+ public static readonly Encoding Encoding;
16
+ public static readonly JsonSerializerOptions NormalJsonOptions;
17
+ public static readonly JsonSerializerOptions PrettyJsonOptions;
17
18
 
18
- public static readonly JsonConverter[] Converters =
19
- {new StringEnumConverter(), Vector3Converter.Instance, Vector2Converter.Instance};
20
-
21
- public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
19
+ static SerializerEncoding()
22
20
  {
23
- ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
24
- Converters = Converters
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(nameof(serializationType), (int)serializationType, typeof(SerializationType));
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(nameof(serializationType), (int)serializationType, typeof(SerializationType));
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) ProtoBuf.Serializer.Deserialize(type, memoryStream);
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 JsonConvert.DeserializeObject<T>(data);
120
+ return JsonSerializer.Deserialize<T>(data, SerializerEncoding.NormalJsonOptions);
100
121
  }
101
122
 
102
- public static byte[] JsonSerialize(object input)
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(object input, bool pretty = false)
128
+ public static string JsonStringify<T>(T input, bool pretty = false)
108
129
  {
109
- return JsonConvert.SerializeObject(input, pretty ? Formatting.Indented : Formatting.None,
110
- SerializerEncoding.Settings);
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 = JsonUtility.ToJson(input);
150
+ string jsonAsText = JsonStringify(input, pretty);
122
151
  File.WriteAllText(path, jsonAsText);
123
152
  }
124
153
  }
125
- }
154
+ }