1- using Newtonsoft . Json ;
2- using Newtonsoft . Json . Linq ;
3- using System ;
1+ using System ;
42using System . Collections . Generic ;
53using System . ComponentModel ;
64using System . Text ;
5+ using System . Text . Json ;
6+ using System . Text . Json . Nodes ;
77
88namespace Quick . Protocol
99{
@@ -71,12 +71,43 @@ protected virtual void LoadFromUri(Uri uri)
7171 if ( string . IsNullOrEmpty ( uri . Query ) )
7272 return ;
7373 var queryString = System . Web . HttpUtility . ParseQueryString ( uri . Query ) ;
74- JObject jObj = new JObject ( ) ;
74+ var type = this . GetType ( ) ;
7575 foreach ( var key in queryString . AllKeys )
7676 {
77- jObj . Add ( key , queryString [ key ] ) ;
77+ var pi = type . GetProperty ( key ) ;
78+ if ( pi == null )
79+ continue ;
80+ var propertyType = pi . PropertyType ;
81+ var stringValue = queryString [ key ] ;
82+ object propertyValue = stringValue ;
83+ if ( propertyType == typeof ( bool ) )
84+ propertyValue = Convert . ToBoolean ( stringValue ) ;
85+ else if ( propertyType == typeof ( byte ) )
86+ propertyValue = Convert . ToByte ( stringValue ) ;
87+ else if ( propertyType == typeof ( sbyte ) )
88+ propertyValue = Convert . ToSByte ( stringValue ) ;
89+ else if ( propertyType == typeof ( short ) )
90+ propertyValue = Convert . ToInt16 ( stringValue ) ;
91+ else if ( propertyType == typeof ( ushort ) )
92+ propertyValue = Convert . ToUInt16 ( stringValue ) ;
93+ else if ( propertyType == typeof ( int ) )
94+ propertyValue = Convert . ToInt32 ( stringValue ) ;
95+ else if ( propertyType == typeof ( uint ) )
96+ propertyValue = Convert . ToUInt32 ( stringValue ) ;
97+ else if ( propertyType == typeof ( long ) )
98+ propertyValue = Convert . ToInt64 ( stringValue ) ;
99+ else if ( propertyType == typeof ( ulong ) )
100+ propertyValue = Convert . ToUInt64 ( stringValue ) ;
101+ else if ( propertyType == typeof ( float ) )
102+ propertyValue = Convert . ToSingle ( stringValue ) ;
103+ else if ( propertyType == typeof ( double ) )
104+ propertyValue = Convert . ToDouble ( stringValue ) ;
105+ else if ( propertyType == typeof ( decimal ) )
106+ propertyValue = Convert . ToDecimal ( stringValue ) ;
107+ else if ( propertyType == typeof ( DateTime ) )
108+ propertyValue = Convert . ToDateTime ( stringValue ) ;
109+ pi . SetValue ( this , propertyValue ) ;
78110 }
79- JsonConvert . PopulateObject ( jObj . ToString ( ) , this ) ;
80111 }
81112
82113 protected abstract string ToUriBasic ( HashSet < string > ignorePropertyNames ) ;
@@ -93,26 +124,25 @@ public Uri ToUri(bool includePassword = false, bool includeOtherProperty = false
93124 {
94125 StringBuilder sb = new StringBuilder ( baseUrl ) ;
95126 int currentIndex = 0 ;
96-
97- var jObj = JObject . FromObject ( this ) ;
98- foreach ( var property in jObj . Properties ( ) )
127+ var jObj = JsonNode . Parse ( JsonSerializer . Serialize ( this ) ) . AsObject ( ) ;
128+ foreach ( var property in jObj )
99129 {
100- var key = property . Name ;
130+ var key = property . Key ;
101131 if ( ignorePropertyNames . Contains ( key ) )
102132 continue ;
103- if ( ! includeOtherProperty && key != nameof ( Password ) )
133+ if ( ! includeOtherProperty && key != nameof ( Password ) )
104134 continue ;
105- if ( currentIndex == 0 )
135+ if ( currentIndex == 0 )
106136 sb . Append ( "?" ) ;
107- if ( currentIndex > 0 )
137+ if ( currentIndex > 0 )
108138 sb . Append ( "&" ) ;
109139 currentIndex ++ ;
110140
111141 var value = property . Value . ToString ( ) ;
112- value = System . Web . HttpUtility . UrlEncode ( value ) ;
142+ value = System . Web . HttpUtility . UrlEncode ( value ) ;
113143 sb . Append ( $ "{ key } ={ value } ") ;
114144 }
115- baseUrl = sb . ToString ( ) ;
145+ baseUrl = sb . ToString ( ) ;
116146 }
117147 Uri uri = new Uri ( baseUrl ) ;
118148 return uri ;
0 commit comments