|
1 | | -using System; |
2 | | -using System.Collections; |
3 | | -using System.IO; |
4 | | -using System.Linq; |
5 | | -using System.Reflection; |
6 | | -using System.Text; |
7 | | -using System.Threading; |
8 | | -using System.Threading.Tasks; |
9 | | -using Newtonsoft.Json; |
10 | | -using Newtonsoft.Json.Converters; |
11 | | -using Newtonsoft.Json.Serialization; |
| 1 | +using System.Text.Json; |
12 | 2 |
|
13 | 3 | namespace Sentry.Internal |
14 | 4 | { |
15 | | - [AttributeUsage(AttributeTargets.Property)] |
16 | | - internal class DontSerializeEmptyAttribute : Attribute {} |
17 | | - |
18 | 5 | internal static class Json |
19 | 6 | { |
20 | | - private class ContractResolver : DefaultContractResolver |
21 | | - { |
22 | | - protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) |
23 | | - { |
24 | | - var jsonProperty = base.CreateProperty(member, memberSerialization); |
25 | | - var property = jsonProperty.DeclaringType.GetProperty(jsonProperty.UnderlyingName); |
26 | | - |
27 | | - // DontSerializeEmpty |
28 | | - if (jsonProperty.ShouldSerialize is null && |
29 | | - property?.GetCustomAttribute<DontSerializeEmptyAttribute>() is {}) |
30 | | - { |
31 | | - // Collections |
32 | | - if (property.PropertyType.GetInterfaces().Any(i => i == typeof(IEnumerable))) |
33 | | - { |
34 | | - jsonProperty.ShouldSerialize = o => |
35 | | - { |
36 | | - if (property.GetValue(o) is IEnumerable value) |
37 | | - { |
38 | | - return !value.Cast<object>().Any(); |
39 | | - } |
40 | | - |
41 | | - return true; |
42 | | - }; |
43 | | - } |
44 | | - } |
45 | | - |
46 | | - return jsonProperty; |
47 | | - } |
48 | | - } |
49 | | - |
50 | | - private static readonly Encoding Encoding = new UTF8Encoding(false, true); |
51 | | - private static readonly StringEnumConverter StringEnumConverter = new StringEnumConverter(); |
52 | | - |
53 | | - private static readonly JsonSerializer Serializer = new JsonSerializer |
54 | | - { |
55 | | - ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor, |
56 | | - NullValueHandling = NullValueHandling.Ignore, |
57 | | - ReferenceLoopHandling = ReferenceLoopHandling.Ignore, |
58 | | - Formatting = Formatting.None, |
59 | | - Converters = {StringEnumConverter}, |
60 | | - DateFormatHandling = DateFormatHandling.IsoDateFormat, |
61 | | - ContractResolver = new ContractResolver() |
62 | | - }; |
63 | | - |
64 | | - private static JsonTextWriter CreateWriter(Stream stream) => new JsonTextWriter( |
65 | | - new StreamWriter(stream, Encoding, 1024, true) |
66 | | - ); |
67 | | - |
68 | | - private static JsonTextReader CreateReader(Stream stream) => new JsonTextReader( |
69 | | - new StreamReader(stream, Encoding, false, 1024, true) |
70 | | - ); |
71 | | - |
72 | | - public static void SerializeToStream(object obj, Stream stream) |
| 7 | + public static JsonElement Parse(byte[] json) |
73 | 8 | { |
74 | | - using var writer = CreateWriter(stream); |
75 | | - Serializer.Serialize(writer, obj); |
| 9 | + using var jsonDocument = JsonDocument.Parse(json); |
| 10 | + return jsonDocument.RootElement.Clone(); |
76 | 11 | } |
77 | 12 |
|
78 | | - public static T DeserializeFromStream<T>(Stream stream) |
79 | | - { |
80 | | - using var reader = CreateReader(stream); |
81 | | - return Serializer.Deserialize<T>(reader); |
82 | | - } |
83 | | - |
84 | | - public static byte[] SerializeToByteArray(object obj) |
85 | | - { |
86 | | - using var buffer = new MemoryStream(); |
87 | | - SerializeToStream(obj, buffer); |
88 | | - |
89 | | - return buffer.ToArray(); |
90 | | - } |
91 | | - |
92 | | - public static T DeserializeFromByteArray<T>(byte[] data) |
93 | | - { |
94 | | - using var buffer = new MemoryStream(data); |
95 | | - return DeserializeFromStream<T>(buffer); |
96 | | - } |
97 | | - |
98 | | - public static string Serialize(object obj) => |
99 | | - Encoding.GetString(SerializeToByteArray(obj)); |
100 | | - |
101 | | - public static T Deserialize<T>(string json) => |
102 | | - DeserializeFromByteArray<T>(Encoding.GetBytes(json)); |
103 | | - |
104 | | - public static async Task SerializeToStreamAsync( |
105 | | - object obj, |
106 | | - Stream stream, |
107 | | - CancellationToken cancellationToken = default) |
| 13 | + public static JsonElement Parse(string json) |
108 | 14 | { |
109 | | - using var writer = CreateWriter(stream); |
110 | | - Serializer.Serialize(writer, obj); |
111 | | - await writer.FlushAsync(cancellationToken).ConfigureAwait(false); |
| 15 | + using var jsonDocument = JsonDocument.Parse(json); |
| 16 | + return jsonDocument.RootElement.Clone(); |
112 | 17 | } |
113 | 18 | } |
114 | 19 | } |
0 commit comments