Skip to content

Commit 10ec0da

Browse files
committed
Fix failing JSON Payloads due to incorrect serializer use
+ The SetObject and GetObject methods for RPC values don't use JsonTypeInfo<T> while they JsonSerializerContext. Some of them don't even use it correctly. This fix should solve that now.
1 parent d3e4279 commit 10ec0da

File tree

12 files changed

+118
-98
lines changed

12 files changed

+118
-98
lines changed

DiscordRPC/Converters/EnumSnakeCaseConverter.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@
33
using System.Text.Json.Serialization;
44

55
// Custom snake_case naming policy for enums
6+
/// <summary>
7+
///
8+
/// </summary>
69
public class EnumSnakeCaseConverter : JsonNamingPolicy
710
{
11+
/// <summary>
12+
///
13+
/// </summary>
14+
/// <param name="name"></param>
15+
/// <returns></returns>
816
public override string ConvertName(string name)
917
{
1018
// Simple snake_case implementation

DiscordRPC/Entities/RichPresence.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,14 @@ public string DetailsUrl
126126
/// The activity type
127127
/// </summary>
128128
[JsonPropertyName("type")]
129-
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
129+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
130130
public ActivityType Type { get; set; }
131131

132132
/// <summary>
133133
/// The display type for the status
134134
/// </summary>
135135
[JsonPropertyName("status_display_type")]
136-
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
136+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
137137
public StatusDisplayType StatusDisplay { get; set; }
138138

139139
#region Has Checks

DiscordRPC/Entities/Secrets.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace DiscordRPC
1616
public class Secrets
1717
{
1818
/// <summary>
19-
/// The unique match code to distinguish different games/lobbies. Use <see cref="Secrets.CreateSecret(Random)"/> to get an appropriately sized secret.
19+
/// The unique match code to distinguish different games/lobbies. Use <see cref="Secrets"/> to get an appropriately sized secret.
2020
/// <para>This cannot be null and must be supplied for the Join / Spectate feature to work.</para>
2121
/// <para>Max Length of 128 characters</para>
2222
/// </summary>

DiscordRPC/Entities/User.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using DiscordRPC.Exceptions;
2-
using System;
3-
using System.Text.Json;
1+
using System;
42
using System.Text.Json.Serialization;
53

64
namespace DiscordRPC
@@ -138,7 +136,7 @@ public struct AvatarDecorationData
138136
/// The flags on a users account, often represented as a badge.
139137
/// </summary>
140138
[JsonPropertyName("flags")]
141-
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
139+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
142140
public Flag Flags { get; private set; }
143141

144142
/// <summary>
@@ -216,7 +214,7 @@ public enum Flag
216214
/// The premium type of the user.
217215
/// </summary>
218216
[JsonPropertyName("premium_type")]
219-
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
217+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
220218
public PremiumType Premium { get; private set; }
221219

222220
/// <summary>
@@ -240,15 +238,7 @@ public enum PremiumType
240238
/// <summary>
241239
/// The endpoint for the CDN. Normally cdn.discordapp.com.
242240
/// </summary>
243-
public string CdnEndpoint { get; private set; }
244-
245-
/// <summary>
246-
/// Creates a new User instance.
247-
/// </summary>
248-
internal User()
249-
{
250-
CdnEndpoint = "cdn.discordapp.com";
251-
}
241+
public string CdnEndpoint { get; private set; } = "cdn.discordapp.com";
252242

253243
/// <summary>
254244
/// Updates the URL paths to the appropriate configuration

DiscordRPC/IO/ManagedNamedPipeClient.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,15 +412,34 @@ public void Dispose()
412412
_isDisposed = true;
413413
}
414414

415+
/// <summary>
416+
///
417+
/// </summary>
418+
/// <param name="pipe"></param>
419+
/// <returns></returns>
415420
[System.Obsolete("Use PipePermutation.GetPipes instead", true)]
416421
public static string GetPipeName(int pipe)
417422
=> string.Empty;
423+
/// <summary>
424+
///
425+
/// </summary>
426+
/// <param name="pipe"></param>
427+
/// <param name="sandbox"></param>
428+
/// <returns></returns>
418429
[System.Obsolete("Use PipePermutation.GetPipes instead", true)]
419430
public static string GetPipeName(int pipe, string sandbox)
420431
=> string.Empty;
432+
/// <summary>
433+
///
434+
/// </summary>
435+
/// <returns></returns>
421436
[System.Obsolete("Use PipePermutation.GetPipes instead", true)]
422437
public static string GetPipeSandbox()
423438
=> string.Empty;
439+
/// <summary>
440+
///
441+
/// </summary>
442+
/// <returns></returns>
424443
[System.Obsolete("Use PipePermutation.GetPipes instead", true)]
425444
public static bool IsUnix()
426445
=> true;

DiscordRPC/IO/PipeFrame.cs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Text;
55
using System.Text.Json;
6+
using System.Text.Json.Serialization.Metadata;
67

78
namespace DiscordRPC.IO
89
{
@@ -45,14 +46,18 @@ public string Message
4546
/// </summary>
4647
/// <param name="opcode">The opcode of the frame</param>
4748
/// <param name="data">The data of the frame that will be serialized as JSON</param>
48-
public PipeFrame(Opcode opcode, object data)
49+
/// <param name="jsonTypeInfo">The JSON type info of the data</param>
50+
public static PipeFrame Create<T>(Opcode opcode, T data, JsonTypeInfo<T> jsonTypeInfo)
51+
where T : class
4952
{
50-
// Set the opcode and a temp field for data
51-
Opcode = opcode;
52-
Data = null;
53+
PipeFrame frame = new PipeFrame()
54+
{
55+
Opcode = opcode,
56+
Data = null
57+
};
58+
frame.SetObject(data, jsonTypeInfo);
5359

54-
// Set the data
55-
SetObject(data);
60+
return frame;
5661
}
5762

5863
/// <summary>
@@ -73,37 +78,38 @@ public PipeFrame(Opcode opcode, object data)
7378
private string GetMessage() { return MessageEncoding.GetString(Data); }
7479

7580
/// <summary>
76-
/// Serializes the object into json string then encodes it into <see cref="Data"/>.
81+
/// Sets the opcodes and serializes the object into a json string.
7782
/// </summary>
83+
/// <param name="opcode"></param>
7884
/// <param name="obj"></param>
79-
public void SetObject<TObj>(TObj obj)
80-
where TObj : class
85+
/// <param name="jsonTypeInfo"></param>
86+
public void SetObject(Opcode opcode, object obj, JsonTypeInfo jsonTypeInfo)
8187
{
82-
string json = JsonSerializer.Serialize(obj, typeof(TObj), JsonSerializationContext.Default);
83-
SetMessage(json);
88+
Opcode = opcode;
89+
SetObject(obj, jsonTypeInfo);
8490
}
8591

8692
/// <summary>
87-
/// Sets the opcodes and serializes the object into a json string.
93+
/// Serializes the object into json string then encodes it into <see cref="Data"/>.
8894
/// </summary>
89-
/// <param name="opcode"></param>
9095
/// <param name="obj"></param>
91-
public void SetObject(Opcode opcode, object obj)
96+
/// <param name="jsonTypeInfo"></param>
97+
public void SetObject(object obj, JsonTypeInfo jsonTypeInfo)
9298
{
93-
Opcode = opcode;
94-
SetObject(obj);
99+
string json = JsonSerializer.Serialize(obj, jsonTypeInfo);
100+
SetMessage(json);
95101
}
96102

97103
/// <summary>
98104
/// Deserializes the data into the supplied type using JSON.
99105
/// </summary>
100106
/// <typeparam name="T">The type to deserialize into</typeparam>
101107
/// <returns></returns>
102-
public T GetObject<T>()
108+
public T GetObject<T>(JsonTypeInfo<T> typeInfo)
103109
where T : class
104110
{
105111
string json = GetMessage();
106-
return (T)JsonSerializer.Deserialize(json, typeof(T), JsonSerializationContext.Default);
112+
return JsonSerializer.Deserialize(json, typeInfo);
107113
}
108114

109115
/// <summary>

DiscordRPC/RPC/Commands/PresenceCommand.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using DiscordRPC.RPC.Payload;
1+
using DiscordRPC.Helper;
2+
using DiscordRPC.RPC.Payload;
23
using System.Text.Json.Serialization;
34

45
namespace DiscordRPC.RPC.Commands
@@ -19,7 +20,7 @@ internal class PresenceCommand : ICommand
1920

2021
public IPayload PreparePayload(long nonce)
2122
{
22-
return new ArgumentPayload<PresenceCommand>(this, nonce)
23+
return new ArgumentPayload<PresenceCommand>(this, JsonSerializationContext.Default.PresenceCommand, nonce)
2324
{
2425
Command = Command.SetActivity
2526
};

DiscordRPC/RPC/Commands/RespondCommand.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using DiscordRPC.RPC.Payload;
1+
using DiscordRPC.Helper;
2+
using DiscordRPC.RPC.Payload;
23
using System.Text.Json.Serialization;
34

45
namespace DiscordRPC.RPC.Commands
@@ -19,7 +20,7 @@ internal class RespondCommand : ICommand
1920

2021
public IPayload PreparePayload(long nonce)
2122
{
22-
return new ArgumentPayload<RespondCommand>(this, nonce)
23+
return new ArgumentPayload<RespondCommand>(this, JsonSerializationContext.Default.RespondCommand, nonce)
2324
{
2425
Command = Accept ? Command.SendActivityJoinInvite : Command.CloseActivityJoinRequest
2526
};

DiscordRPC/RPC/Payload/Command.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
using DiscordRPC.Converters;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Text;
1+
using System;
2+
using System.Text.Json.Serialization;
63

74
namespace DiscordRPC.RPC.Payload
85
{
@@ -14,37 +11,37 @@ internal enum Command
1411
/// <summary>
1512
/// event dispatch
1613
/// </summary>
17-
[EnumValue("DISPATCH")]
14+
[JsonStringEnumMemberName("DISPATCH")]
1815
Dispatch,
1916

2017
/// <summary>
2118
/// Called to set the activity
2219
/// </summary>
23-
[EnumValue("SET_ACTIVITY")]
20+
[JsonStringEnumMemberName("SET_ACTIVITY")]
2421
SetActivity,
2522

2623
/// <summary>
2724
/// used to subscribe to an RPC event
2825
/// </summary>
29-
[EnumValue("SUBSCRIBE")]
26+
[JsonStringEnumMemberName("SUBSCRIBE")]
3027
Subscribe,
3128

3229
/// <summary>
3330
/// used to unsubscribe from an RPC event
3431
/// </summary>
35-
[EnumValue("UNSUBSCRIBE")]
32+
[JsonStringEnumMemberName("UNSUBSCRIBE")]
3633
Unsubscribe,
3734

3835
/// <summary>
3936
/// Used to accept join requests.
4037
/// </summary>
41-
[EnumValue("SEND_ACTIVITY_JOIN_INVITE")]
38+
[JsonStringEnumMemberName("SEND_ACTIVITY_JOIN_INVITE")]
4239
SendActivityJoinInvite,
4340

4441
/// <summary>
4542
/// Used to reject join requests.
4643
/// </summary>
47-
[EnumValue("CLOSE_ACTIVITY_JOIN_REQUEST")]
44+
[JsonStringEnumMemberName("CLOSE_ACTIVITY_JOIN_REQUEST")]
4845
CloseActivityJoinRequest,
4946

5047
/// <summary>

DiscordRPC/RPC/Payload/PayloadArgument.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using DiscordRPC.Helper;
22
using System.Text.Json;
33
using System.Text.Json.Serialization;
4+
using System.Text.Json.Serialization.Metadata;
45

56
namespace DiscordRPC.RPC.Payload
67
{
@@ -10,6 +11,7 @@ namespace DiscordRPC.RPC.Payload
1011
/// SetPresence
1112
/// </para>
1213
/// </summary>
14+
/// <typeparam name="T"></typeparam>
1315
internal class ArgumentPayload<T> : IPayload
1416
where T : class
1517
{
@@ -22,29 +24,26 @@ internal class ArgumentPayload<T> : IPayload
2224

2325
public ArgumentPayload() { Arguments = null; }
2426
public ArgumentPayload(long nonce) : base(nonce) { Arguments = null; }
25-
public ArgumentPayload(object args, long nonce) : base(nonce)
27+
public ArgumentPayload(object args, JsonTypeInfo<T> jsonTypeInfo, long nonce) : base(nonce)
2628
{
27-
SetObject((T)args);
29+
SetObject((T)args, jsonTypeInfo);
2830
}
2931

3032
/// <summary>
3133
/// Sets the object stored within the data.
3234
/// </summary>
3335
/// <param name="obj"></param>
34-
public void SetObject(T obj)
36+
/// <param name="typeInfo"></param>
37+
public void SetObject(T obj, JsonTypeInfo<T> typeInfo)
3538
{
36-
Arguments = JsonSerializer.SerializeToDocument(obj, typeof(T), JsonSerializationContext.Default);
39+
Arguments = JsonSerializer.SerializeToDocument(obj, typeInfo);
3740
}
3841

3942
/// <summary>
4043
/// Gets the object stored within the Data
4144
/// </summary>
42-
/// <typeparam name="T"></typeparam>
4345
/// <returns></returns>
44-
public T GetObject()
45-
{
46-
return (T)Arguments.Deserialize(typeof(T), JsonSerializationContext.Default);
47-
}
46+
public T GetObject(JsonTypeInfo<T> typeInfo) => Arguments.Deserialize(typeInfo);
4847

4948
public override string ToString()
5049
{

0 commit comments

Comments
 (0)