33using System . IO ;
44using System . Text ;
55using System . Text . Json ;
6+ using System . Text . Json . Serialization . Metadata ;
67
78namespace 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>
0 commit comments