Skip to content

Commit d073d35

Browse files
authored
Merge pull request #14 from DanKE123abc/dev
v1.3.2
2 parents c81e218 + 43a453c commit d073d35

File tree

8 files changed

+86
-167
lines changed

8 files changed

+86
-167
lines changed

DanKeJson.Test/Program.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
Person person = new Person
44
{
5-
Name = "张三",
5+
Name = null,
66
Age = 30,
77
Hobbies = new List<string> { "阅读", "旅行", "编程" },
88
Address = new Address
99
{
10-
City = "北京",
10+
City = null,
1111
District = "朝阳区",
1212
Street = "某街道"
1313
},
1414
addresses = new List<Address>
1515
{
1616
new Address
1717
{
18-
City = "北京",
18+
City = null,
1919
District = "朝阳区",
2020
Street = "某街道"
2121
},
@@ -53,7 +53,7 @@
5353
public class Person
5454
{
5555
public string Name { get; set; }
56-
public int Age { get; set; }
56+
public double Age { get; set; }
5757
public List<string> Hobbies { get; set; }
5858
public Address Address { get; set; }
5959

DanKeJson/DanKeJson.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package >
33
<metadata>
44
<id>DanKeJson</id>
5-
<version>1.3.1</version>
5+
<version>1.3.2</version>
66
<title>DanKeJson</title>
77
<authors>DanKe</authors>
88
<requireLicenseAcceptance>false</requireLicenseAcceptance>

DanKeJson/JSON.cs

Lines changed: 20 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,11 @@ private static object FromJson(JsonData json, Type type)
480480
private static JsonData FromObject(object jsonObject)
481481
{
482482
JsonData json = new JsonData(JsonData.Type.Object);
483+
if (jsonObject == null)
484+
{
485+
json = new JsonData(JsonData.Type.None);
486+
return json;
487+
}
483488
System.Type type = jsonObject.GetType();
484489
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>))
485490
{
@@ -490,8 +495,11 @@ private static JsonData FromObject(object jsonObject)
490495
foreach (var item in list)
491496
{
492497
JsonData jsonDataItem;
493-
494-
if (listType == typeof(string))
498+
if (item == null)
499+
{
500+
jsonDataItem = new JsonData(JsonData.Type.None);
501+
}
502+
else if (listType == typeof(string))
495503
{
496504
jsonDataItem = new JsonData(JsonData.Type.String)
497505
{ json = "\"" + item.ToString() + "\"" };
@@ -503,7 +511,7 @@ private static JsonData FromObject(object jsonObject)
503511
listType == typeof(ushort))
504512
{
505513
jsonDataItem = new JsonData(JsonData.Type.Number)
506-
{ json = item.ToString() };
514+
{ json = item.ToString()! };
507515
}
508516
else if (listType == typeof(bool))
509517
{
@@ -525,89 +533,33 @@ private static JsonData FromObject(object jsonObject)
525533
if (propertyInfo.CanRead)
526534
{
527535
object propertyValue = propertyInfo.GetValue(jsonObject);
528-
if (propertyValue == null) continue;
529-
530536
System.Type propertyType = propertyInfo.PropertyType;
537+
if (propertyValue == null)
538+
{
539+
json[propertyInfo.Name] = new JsonData(JsonData.Type.None);
540+
continue;
541+
}
531542
switch (Type.GetTypeCode(propertyType))
532543
{
533544
case TypeCode.String:
534-
json[propertyInfo.Name] = (string)propertyValue;
545+
json[propertyInfo.Name] = new JsonData(JsonData.Type.String) { json = "\"" + propertyValue.ToString() + "\"" };
535546
break;
536547
case TypeCode.Boolean:
537-
json[propertyInfo.Name] = (bool)propertyValue;
548+
json[propertyInfo.Name] = new JsonData(JsonData.Type.Boolean) { json = propertyValue.ToString().ToLower() };
538549
break;
539550
case TypeCode.Int32:
540-
json[propertyInfo.Name] = (int)propertyValue;
541-
break;
542551
case TypeCode.Int64:
543-
json[propertyInfo.Name] = (long)propertyValue;
544-
break;
545552
case TypeCode.Single:
546-
json[propertyInfo.Name] = (float)propertyValue;
547-
break;
548553
case TypeCode.Double:
549-
json[propertyInfo.Name] = (double)propertyValue;
550-
break;
551554
case TypeCode.SByte:
552-
json[propertyInfo.Name] = (sbyte)propertyValue;
553-
break;
554555
case TypeCode.Int16:
555-
json[propertyInfo.Name] = (short)propertyValue;
556-
break;
557556
case TypeCode.UInt32:
558-
json[propertyInfo.Name] = (uint)propertyValue;
559-
break;
560557
case TypeCode.UInt64:
561-
json[propertyInfo.Name] = (ulong)propertyValue;
562-
break;
563558
case TypeCode.UInt16:
564-
json[propertyInfo.Name] = (ushort)propertyValue;
559+
json[propertyInfo.Name] = new JsonData(JsonData.Type.Number) { json = propertyValue.ToString()! };
565560
break;
566561
default:
567-
// 确保属性是List<T>
568-
if (propertyInfo.PropertyType.IsGenericType &&
569-
propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(List<>))
570-
{
571-
var listType = propertyInfo.PropertyType.GenericTypeArguments[0];
572-
var list = (IList)propertyValue;
573-
json[propertyInfo.Name] = new JsonData(JsonData.Type.Array);
574-
575-
foreach (var item in list)
576-
{
577-
JsonData jsonDataItem;
578-
579-
if (listType == typeof(string))
580-
{
581-
jsonDataItem = new JsonData(JsonData.Type.String)
582-
{ json = "\"" + item.ToString() + "\"" };
583-
}
584-
else if (listType == typeof(int) || listType == typeof(long) ||
585-
listType == typeof(float) || listType == typeof(double) ||
586-
listType == typeof(sbyte) || listType == typeof(short) ||
587-
listType == typeof(uint) || listType == typeof(ulong) ||
588-
listType == typeof(ushort))
589-
{
590-
jsonDataItem = new JsonData(JsonData.Type.Number)
591-
{ json = item.ToString() };
592-
}
593-
else if (listType == typeof(bool))
594-
{
595-
jsonDataItem = new JsonData(JsonData.Type.Boolean)
596-
{ json = item.ToString().ToLower() };
597-
}
598-
else
599-
{
600-
jsonDataItem = FromObject(item);
601-
}
602-
603-
json[propertyInfo.Name].array.Add(jsonDataItem);
604-
}
605-
}
606-
else if (propertyType.IsClass)
607-
{
608-
json[propertyInfo.Name] = FromObject((object)propertyValue);
609-
}
610-
562+
json[propertyInfo.Name] = FromObject((object)propertyValue);
611563
break;
612564
}
613565
}

DanKeJson/JSON5.cs

Lines changed: 20 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

Docs/API/JSON.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
### *Class*
44

5-
6-
75
# Methods
86

9-
| Name | Value | Summary |
10-
| :---------------- | :------------------------ | :------ |
11-
| ToJson(JsonData) | string | |
12-
| ToJson(object) | string | |
13-
| ToData(string) | [JsonData](./JsonData.md) | |
14-
| ToData<T>(string) | T | |
157

8+
| Name | Value | Summary |
9+
| :---------------------------------------- | :------------------------ | :------ |
10+
| ToJson(JsonData) | string | |
11+
| ToJson(object) | string | |
12+
| ToData(string, bool=false, bool=false) | [JsonData](./JsonData.md) | |
13+
| ToData[T](string, bool=false, bool=false) | T | |

Docs/API/JSON5.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# JSON5
2+
3+
### *Class*
4+
5+
# Methods
6+
7+
8+
| Name | Value | Summary |
9+
| :--------------------------------------- | :------------------------ | :------ |
10+
| ToJson(JsonData) | string | |
11+
| ToJson(object) | string | |
12+
| ToData(string, bool=true, bool=false) | [JsonData](./JsonData.md) | |
13+
| ToData[T](string, bool=true, bool=false) | T | |

0 commit comments

Comments
 (0)