@@ -216,6 +216,11 @@ private static void ProcessData(JsonData json, StringBuilder builder, Json5Confi
216216 private static JsonData FromObject ( object jsonObject )
217217 {
218218 JsonData json = new JsonData ( JsonData . Type . Object ) ;
219+ if ( jsonObject == null )
220+ {
221+ json = new JsonData ( JsonData . Type . None ) ;
222+ return json ;
223+ }
219224 System . Type type = jsonObject . GetType ( ) ;
220225 if ( type . IsGenericType && type . GetGenericTypeDefinition ( ) == typeof ( List < > ) )
221226 {
@@ -226,8 +231,11 @@ private static JsonData FromObject(object jsonObject)
226231 foreach ( var item in list )
227232 {
228233 JsonData jsonDataItem ;
229-
230- if ( listType == typeof ( string ) )
234+ if ( item == null )
235+ {
236+ jsonDataItem = new JsonData ( JsonData . Type . None ) ;
237+ }
238+ else if ( listType == typeof ( string ) )
231239 {
232240 jsonDataItem = new JsonData ( JsonData . Type . String )
233241 { json = "\" " + item . ToString ( ) + "\" " } ;
@@ -239,7 +247,7 @@ private static JsonData FromObject(object jsonObject)
239247 listType == typeof ( ushort ) )
240248 {
241249 jsonDataItem = new JsonData ( JsonData . Type . Number )
242- { json = item . ToString ( ) } ;
250+ { json = item . ToString ( ) ! } ;
243251 }
244252 else if ( listType == typeof ( bool ) )
245253 {
@@ -261,89 +269,33 @@ private static JsonData FromObject(object jsonObject)
261269 if ( propertyInfo . CanRead )
262270 {
263271 object propertyValue = propertyInfo . GetValue ( jsonObject ) ;
264- if ( propertyValue == null ) continue ;
265-
266272 System . Type propertyType = propertyInfo . PropertyType ;
273+ if ( propertyValue == null )
274+ {
275+ json [ propertyInfo . Name ] = new JsonData ( JsonData . Type . None ) ;
276+ continue ;
277+ }
267278 switch ( Type . GetTypeCode ( propertyType ) )
268279 {
269280 case TypeCode . String :
270- json [ propertyInfo . Name ] = ( string ) propertyValue ;
281+ json [ propertyInfo . Name ] = new JsonData ( JsonData . Type . String ) { json = " \" " + propertyValue . ToString ( ) + " \" " } ;
271282 break ;
272283 case TypeCode . Boolean :
273- json [ propertyInfo . Name ] = ( bool ) propertyValue ;
284+ json [ propertyInfo . Name ] = new JsonData ( JsonData . Type . Boolean ) { json = propertyValue . ToString ( ) . ToLower ( ) } ;
274285 break ;
275286 case TypeCode . Int32 :
276- json [ propertyInfo . Name ] = ( int ) propertyValue ;
277- break ;
278287 case TypeCode . Int64 :
279- json [ propertyInfo . Name ] = ( long ) propertyValue ;
280- break ;
281288 case TypeCode . Single :
282- json [ propertyInfo . Name ] = ( float ) propertyValue ;
283- break ;
284289 case TypeCode . Double :
285- json [ propertyInfo . Name ] = ( double ) propertyValue ;
286- break ;
287290 case TypeCode . SByte :
288- json [ propertyInfo . Name ] = ( sbyte ) propertyValue ;
289- break ;
290291 case TypeCode . Int16 :
291- json [ propertyInfo . Name ] = ( short ) propertyValue ;
292- break ;
293292 case TypeCode . UInt32 :
294- json [ propertyInfo . Name ] = ( uint ) propertyValue ;
295- break ;
296293 case TypeCode . UInt64 :
297- json [ propertyInfo . Name ] = ( ulong ) propertyValue ;
298- break ;
299294 case TypeCode . UInt16 :
300- json [ propertyInfo . Name ] = ( ushort ) propertyValue ;
295+ json [ propertyInfo . Name ] = new JsonData ( JsonData . Type . Number ) { json = propertyValue . ToString ( ) ! } ;
301296 break ;
302297 default :
303- // 确保属性是List<T>
304- if ( propertyInfo . PropertyType . IsGenericType &&
305- propertyInfo . PropertyType . GetGenericTypeDefinition ( ) == typeof ( List < > ) )
306- {
307- var listType = propertyInfo . PropertyType . GenericTypeArguments [ 0 ] ;
308- var list = ( IList ) propertyValue ;
309- json [ propertyInfo . Name ] = new JsonData ( JsonData . Type . Array ) ;
310-
311- foreach ( var item in list )
312- {
313- JsonData jsonDataItem ;
314-
315- if ( listType == typeof ( string ) )
316- {
317- jsonDataItem = new JsonData ( JsonData . Type . String )
318- { json = "\" " + item . ToString ( ) + "\" " } ;
319- }
320- else if ( listType == typeof ( int ) || listType == typeof ( long ) ||
321- listType == typeof ( float ) || listType == typeof ( double ) ||
322- listType == typeof ( sbyte ) || listType == typeof ( short ) ||
323- listType == typeof ( uint ) || listType == typeof ( ulong ) ||
324- listType == typeof ( ushort ) )
325- {
326- jsonDataItem = new JsonData ( JsonData . Type . Number )
327- { json = item . ToString ( ) } ;
328- }
329- else if ( listType == typeof ( bool ) )
330- {
331- jsonDataItem = new JsonData ( JsonData . Type . Boolean )
332- { json = item . ToString ( ) . ToLower ( ) } ;
333- }
334- else
335- {
336- jsonDataItem = FromObject ( item ) ;
337- }
338-
339- json [ propertyInfo . Name ] . array . Add ( jsonDataItem ) ;
340- }
341- }
342- else if ( propertyType . IsClass )
343- {
344- json [ propertyInfo . Name ] = FromObject ( ( object ) propertyValue ) ;
345- }
346-
298+ json [ propertyInfo . Name ] = FromObject ( ( object ) propertyValue ) ;
347299 break ;
348300 }
349301 }
0 commit comments