22using System . Collections . Generic ;
33using System . Net . Http ;
44using System . Threading . Tasks ;
5- using Newtonsoft . Json ;
6- using Newtonsoft . Json . Converters ;
5+ using System . Text . Json ;
6+ using System . Text . Json . Serialization ;
77using Supabase . Core . Extensions ;
88using Supabase . Postgrest . Interfaces ;
99using Supabase . Postgrest . Models ;
1010using Supabase . Postgrest . Responses ;
11+ using System . Text . Json . Serialization . Metadata ;
12+ using System . Reflection ;
13+ using Supabase . Postgrest . Converters ;
1114
1215namespace Supabase . Postgrest
1316{
1417 /// <inheritdoc />
1518 public class Client : IPostgrestClient
1619 {
1720 /// <summary>
18- /// Custom Serializer resolvers and converters that will be used for encoding and decoding Postgrest JSON responses.
21+ /// Custom Serializer options that will be used for encoding and decoding Postgrest JSON responses.
1922 ///
20- /// By default, Postgrest seems to use a date format that C# and Newtonsoft do not like, so this initial
23+ /// By default, Postgrest seems to use a date format that C# does not like, so this initial
2124 /// configuration handles that.
2225 /// </summary>
23- public static JsonSerializerSettings SerializerSettings ( ClientOptions ? options = null )
26+ public static JsonSerializerOptions SerializerOptions ( ClientOptions ? options = null )
2427 {
2528 options ??= new ClientOptions ( ) ;
2629
27- return new JsonSerializerSettings
30+ return new JsonSerializerOptions
2831 {
29- ContractResolver = new PostgrestContractResolver ( ) ,
32+ PropertyNamingPolicy = JsonNamingPolicy . SnakeCaseLower ,
3033 Converters =
3134 {
32- // 2020-08-28T12:01:54.763231
33- new IsoDateTimeConverter
34- {
35- DateTimeStyles = options . DateTimeStyles ,
36- DateTimeFormat = ClientOptions . DATE_TIME_FORMAT
37- }
35+ new JsonStringEnumConverter ( ) ,
36+ // new PostgrestDateTimeConverter(options.DateTimeStyles, ClientOptions.DATE_TIME_FORMAT)
37+ new RangeConverter ( )
3838 }
3939 } ;
4040 }
@@ -70,7 +70,7 @@ public void RemoveDebugHandler(IPostgrestDebugger.DebugEventHandler handler) =>
7070
7171 /// <summary>
7272 /// Function that can be set to return dynamic headers.
73- ///
73+ ///
7474 /// Headers specified in the constructor options will ALWAYS take precedence over headers returned by this function.
7575 /// </summary>
7676 public Func < Dictionary < string , string > > ? GetHeaders { get ; set ; }
@@ -87,29 +87,27 @@ public Client(string baseUrl, ClientOptions? options = null)
8787 Options = options ?? new ClientOptions ( ) ;
8888 }
8989
90-
9190 /// <inheritdoc />
9291 public IPostgrestTable < T > Table < T > ( ) where T : BaseModel , new ( ) =>
93- new Table < T > ( BaseUrl , SerializerSettings ( Options ) , Options )
92+ new Table < T > ( BaseUrl , SerializerOptions ( Options ) , Options )
9493 {
9594 GetHeaders = GetHeaders
9695 } ;
9796
9897 /// <inheritdoc />
9998 public IPostgrestTableWithCache < T > Table < T > ( IPostgrestCacheProvider cacheProvider )
10099 where T : BaseModel , new ( ) =>
101- new TableWithCache < T > ( BaseUrl , cacheProvider , SerializerSettings ( Options ) , Options )
100+ new TableWithCache < T > ( BaseUrl , cacheProvider , SerializerOptions ( Options ) , Options )
102101 {
103102 GetHeaders = GetHeaders
104103 } ;
105104
106-
107105 /// <inheritdoc />
108106 public async Task < TModeledResponse ? > Rpc < TModeledResponse > ( string procedureName , object ? parameters = null )
109107 {
110108 var response = await Rpc ( procedureName , parameters ) ;
111109
112- return string . IsNullOrEmpty ( response . Content ) ? default : JsonConvert . DeserializeObject < TModeledResponse > ( response . Content ! ) ;
110+ return string . IsNullOrEmpty ( response . Content ) ? default : JsonSerializer . Deserialize < TModeledResponse > ( response . Content ! , SerializerOptions ( Options ) ) ;
113111 }
114112
115113 /// <inheritdoc />
@@ -120,13 +118,13 @@ public Task<BaseResponse> Rpc(string procedureName, object? parameters = null)
120118
121119 var canonicalUri = builder . Uri . ToString ( ) ;
122120
123- var serializerSettings = SerializerSettings ( Options ) ;
121+ var serializerOptions = SerializerOptions ( Options ) ;
124122
125123 // Prepare parameters
126124 Dictionary < string , object > ? data = null ;
127125 if ( parameters != null )
128- data = JsonConvert . DeserializeObject < Dictionary < string , object > > (
129- JsonConvert . SerializeObject ( parameters , serializerSettings ) ) ;
126+ data = JsonSerializer . Deserialize < Dictionary < string , object > > (
127+ JsonSerializer . Serialize ( parameters , serializerOptions ) ) ;
130128
131129 // Prepare headers
132130 var headers = Helpers . PrepareRequestHeaders ( HttpMethod . Post ,
@@ -137,8 +135,8 @@ public Task<BaseResponse> Rpc(string procedureName, object? parameters = null)
137135
138136 // Send request
139137 var request =
140- Helpers . MakeRequest ( Options , HttpMethod . Post , canonicalUri , serializerSettings , data , headers ) ;
138+ Helpers . MakeRequest ( Options , HttpMethod . Post , canonicalUri , serializerOptions , data , headers ) ;
141139 return request ;
142140 }
143141 }
144- }
142+ }
0 commit comments