11
11
// </copyright>
12
12
// <summary></summary>
13
13
// ************************************************************************
14
+ using System . Text . Json ;
15
+
14
16
#pragma warning disable IDE0130 // 命名空间与文件夹结构不匹配
15
17
namespace FeishuNetSdk
16
18
#pragma warning restore IDE0130 // 命名空间与文件夹结构不匹配
@@ -20,7 +22,7 @@ namespace FeishuNetSdk
20
22
/// </summary>
21
23
public static class DtoExtensions
22
24
{
23
- static readonly System . Text . Json . JsonSerializerOptions options = new ( )
25
+ static readonly JsonSerializerOptions options = new ( )
24
26
{
25
27
DefaultIgnoreCondition = JsonIgnoreCondition . WhenWritingNull
26
28
} ;
@@ -54,7 +56,7 @@ public static class DtoExtensions
54
56
public static Approval . PostApprovalV4InstancesBodyDto SetFormControls ( this Approval . PostApprovalV4InstancesBodyDto Dto ,
55
57
object [ ] FormControls )
56
58
{
57
- Dto . Form = System . Text . Json . JsonSerializer . Serialize ( FormControls ) ;
59
+ Dto . Form = JsonSerializer . Serialize ( FormControls ) ;
58
60
59
61
return Dto ;
60
62
}
@@ -66,7 +68,7 @@ public static Approval.PostApprovalV4InstancesBodyDto SetFormControls(this Appro
66
68
/// <returns>序列化的控件数组</returns>
67
69
public static Approval . Dtos . FormControlDto [ ] ? GetFormControls ( this Approval . GetApprovalV4ApprovalsByApprovalCodeResponseDto ? Dto )
68
70
=> Dto is null ? null
69
- : System . Text . Json . JsonSerializer . Deserialize < Approval . Dtos . FormControlDto [ ] > ( Dto . Form ) ;
71
+ : JsonSerializer . Deserialize < Approval . Dtos . FormControlDto [ ] > ( Dto . Form ) ;
70
72
71
73
/// <summary>
72
74
/// 设置消息卡片内容
@@ -99,7 +101,7 @@ public static Im.Spec.PostInteractiveV1CardUpdateBodyDto SetCardObject(this Im.S
99
101
public static Im . PatchImV1MessagesByMessageIdBodyDto SetCardObject ( this Im . PatchImV1MessagesByMessageIdBodyDto Dto ,
100
102
Im . Dtos . MessageCard CardObject )
101
103
{
102
- Dto . Content = System . Text . Json . JsonSerializer . Serialize ( CardObject , CardObject . GetType ( ) , options ) ;
104
+ Dto . Content = JsonSerializer . Serialize ( CardObject , CardObject . GetType ( ) , options ) ;
103
105
104
106
return Dto ;
105
107
}
@@ -147,8 +149,8 @@ private static string SerializeSingleReceiverContent(Im.Dtos.IHasMessageType Car
147
149
{
148
150
return CardOrContent switch
149
151
{
150
- Im . Dtos . PostContent post => System . Text . Json . JsonSerializer . Serialize ( post . Post , options ) ,
151
- _ => System . Text . Json . JsonSerializer . Serialize ( CardOrContent , CardOrContent . GetType ( ) , options )
152
+ Im . Dtos . PostContent post => JsonSerializer . Serialize ( post . Post , options ) ,
153
+ _ => JsonSerializer . Serialize ( CardOrContent , CardOrContent . GetType ( ) , options )
152
154
} ;
153
155
}
154
156
@@ -421,9 +423,7 @@ public static Im.Dtos.TableElement AddColumn(this Im.Dtos.TableElement Dto, Im.D
421
423
/// <returns></returns>
422
424
public static Im . Dtos . TableElement SetRows ( this Im . Dtos . TableElement Dto , IEnumerable < object > Rows )
423
425
{
424
- var data = System . Text . Json . JsonSerializer . Deserialize < Dictionary < string , object > [ ] > (
425
- System . Text . Json . JsonSerializer . Serialize ( Rows ) )
426
- ?? Array . Empty < Dictionary < string , object > > ( ) ;
426
+ var data = JsonSerializer . Deserialize < Dictionary < string , object > [ ] > ( JsonSerializer . Serialize ( Rows ) ) ?? [ ] ;
427
427
428
428
if ( Dto . Columns . Length > 0 )
429
429
{
@@ -484,50 +484,27 @@ public static CallbackEvents.CardActionTriggerResponseDto SetCard(this CallbackE
484
484
// 该方法用于获取字段的字符串值
485
485
private static string ? GetFieldValueAsString ( object ? value , Base . GetBitableV1AppsByAppTokenTablesByTableIdFieldsResponseDto . AppTableFieldForList ? field , IBitableRecordSerializer serializer )
486
486
{
487
- // 如果值或字段信息为空,则返回 null
488
- if ( value is null ) return null ;
489
-
490
- // 调用 ConvertFieldValueToStringByType 方法根据字段类型转换值为字符串
491
- return ConvertFieldValueToStringByType ( field ? . Type , field ? . UiType , value , serializer ) ;
487
+ return value is null
488
+ ? null
489
+ : ConvertFieldValueToStringByType ( field ? . Type , field ? . UiType , value , serializer ) ;
492
490
}
493
491
494
492
// 该方法根据字段类型将字段值转换为字符串
495
493
private static string ? ConvertFieldValueToStringByType ( int ? type , string ? uiType , object value , IBitableRecordSerializer serializer )
496
494
{
497
495
try
498
496
{
499
- // 将值序列化为 JSON 字符串
500
- string json = System . Text . Json . JsonSerializer . Serialize ( value ) ;
501
- var jd = System . Text . Json . JsonDocument . Parse ( json ) ;
502
- //Console.WriteLine($"type: {type}, ui_type: {uiType}, json: {json}");
503
-
504
- string key = GetKey ( uiType , type , jd ) ;
505
- object ? record = null ;
497
+ string json = JsonSerializer . Serialize ( value ) ;
498
+ using var jd = JsonDocument . Parse ( json ) ;
499
+ string key = GetKey ( uiType , type , jd . RootElement ) ;
506
500
507
- //Console.WriteLine($"key: {key}");
508
501
if ( ! serializer . TypePairs . TryGetValue ( key , out var _type ) ) return null ;
509
- json = AdjustJsonIfArray ( _type , jd , json ) ;
510
- //Console.WriteLine($"json: {json}");
511
- record = System . Text . Json . JsonSerializer . Deserialize ( json , _type ) ;
502
+ json = AdjustJsonIfArray ( _type , jd . RootElement , json ) ;
512
503
504
+ var record = JsonSerializer . Deserialize ( json , _type ) ;
513
505
if ( record is null ) return null ;
514
506
515
- string ? result = null ;
516
- if ( uiType is not null )
517
- {
518
- result = HandleUiType ( uiType , record , serializer ) ;
519
- }
520
- else if ( type is not null )
521
- {
522
- result = HandleType ( type . Value , record , serializer ) ;
523
- }
524
- else
525
- {
526
- result = HandleJsonValueKind ( jd . RootElement . ValueKind , record , serializer ) ;
527
- }
528
-
529
- //Console.WriteLine(result);
530
- return result ;
507
+ return GetSerializedValue ( uiType , type , jd . RootElement . ValueKind , record , serializer ) ;
531
508
}
532
509
catch ( Exception ex )
533
510
{
@@ -537,22 +514,84 @@ record = System.Text.Json.JsonSerializer.Deserialize(json, _type);
537
514
}
538
515
}
539
516
540
- private static string GetKey ( string ? uiType , int ? type , System . Text . Json . JsonDocument jd )
517
+ private static string ? GetSerializedValue ( string ? uiType , int ? type , JsonValueKind valueKind , object record , IBitableRecordSerializer serializer )
518
+ {
519
+ if ( uiType is not null )
520
+ {
521
+ return HandleUiType ( uiType , record , serializer ) ;
522
+ }
523
+ else if ( type is not null )
524
+ {
525
+ return HandleType ( type . Value , record , serializer ) ;
526
+ }
527
+ else
528
+ {
529
+ return HandleJsonValueKind ( valueKind , record , serializer ) ;
530
+ }
531
+ }
532
+
533
+ private static string GetKey ( string ? uiType , int ? type , JsonElement je )
541
534
{
542
535
return uiType is not null
543
536
? uiType
544
537
: type is not null
545
538
? $ "Type{ type } "
546
- : $ "JsonValueKind{ jd . RootElement . ValueKind } ";
539
+ : $ "JsonValueKind{ je . ValueKind } ";
540
+ }
541
+
542
+ private static string AdjustJsonIfArray ( Type _type , JsonElement je , string json )
543
+ {
544
+ return _type . IsArray && je . ValueKind != JsonValueKind . Array
545
+ ? ConvertToJsonArray ( je ) . GetRawText ( )
546
+ : json ;
547
+ }
548
+
549
+ private static JsonElement ConvertToJsonArray ( JsonElement element )
550
+ {
551
+ using var memoryStream = new MemoryStream ( ) ;
552
+ using var jsonWriter = new Utf8JsonWriter ( memoryStream ) ;
553
+
554
+ jsonWriter . WriteStartArray ( ) ;
555
+ WriteJsonElementToWriter ( jsonWriter , element ) ;
556
+ jsonWriter . WriteEndArray ( ) ;
557
+ jsonWriter . Flush ( ) ;
558
+
559
+ var jsonBytes = memoryStream . ToArray ( ) ;
560
+ return JsonDocument . Parse ( jsonBytes ) . RootElement ;
547
561
}
548
562
549
- private static string AdjustJsonIfArray ( Type _type , System . Text . Json . JsonDocument jd , string json )
563
+ private static void WriteJsonElementToWriter ( Utf8JsonWriter writer , JsonElement element )
550
564
{
551
- if ( _type . IsArray && jd . RootElement . ValueKind != System . Text . Json . JsonValueKind . Array )
565
+ try
566
+ {
567
+ switch ( element . ValueKind )
568
+ {
569
+ case JsonValueKind . Object :
570
+ writer . WriteRawValue ( element . GetRawText ( ) ) ;
571
+ break ;
572
+ case JsonValueKind . String :
573
+ writer . WriteStringValue ( element . GetString ( ) ) ;
574
+ break ;
575
+ case JsonValueKind . Number :
576
+ writer . WriteNumberValue ( element . GetDouble ( ) ) ;
577
+ break ;
578
+ case JsonValueKind . True :
579
+ case JsonValueKind . False :
580
+ writer . WriteBooleanValue ( element . GetBoolean ( ) ) ;
581
+ break ;
582
+ case JsonValueKind . Null :
583
+ writer . WriteNullValue ( ) ;
584
+ break ;
585
+ default :
586
+ // 其他情况,可根据需求扩展处理逻辑
587
+ break ;
588
+ }
589
+ }
590
+ catch ( Exception ex )
552
591
{
553
- json = $ "[{ json } ]";
592
+ // 可以根据实际情况记录日志或进行其他处理
593
+ Console . WriteLine ( $ "Error writing JSON element: { ex . Message } ") ;
554
594
}
555
- return json ;
556
595
}
557
596
558
597
private static string ? HandleUiType ( string uiType , object record , IBitableRecordSerializer serializer )
@@ -618,19 +657,19 @@ 19 or 20 when record is Base.Dtos.FormulaRecord f
618
657
} ;
619
658
}
620
659
621
- private static string ? HandleJsonValueKind ( System . Text . Json . JsonValueKind valueKind , object record , IBitableRecordSerializer serializer )
660
+ private static string ? HandleJsonValueKind ( JsonValueKind valueKind , object record , IBitableRecordSerializer serializer )
622
661
{
623
662
return valueKind switch
624
663
{
625
- System . Text . Json . JsonValueKind . False
626
- or System . Text . Json . JsonValueKind . True => serializer . CheckboxRecordToString ( record as bool [ ] ) ,
627
- System . Text . Json . JsonValueKind . Number => serializer . NumberRecordToString ( record as decimal [ ] ) ,
628
- System . Text . Json . JsonValueKind . String => serializer . TextRecordToString ( record as Base . Dtos . TextRecord [ ] ) ,
629
- System . Text . Json . JsonValueKind . Object when record is Base . Dtos . FormulaRecord f
664
+ JsonValueKind . False
665
+ or JsonValueKind . True => serializer . CheckboxRecordToString ( record as bool [ ] ) ,
666
+ JsonValueKind . Number => serializer . NumberRecordToString ( record as decimal [ ] ) ,
667
+ JsonValueKind . String => serializer . TextRecordToString ( record as Base . Dtos . TextRecord [ ] ) ,
668
+ JsonValueKind . Object when record is Base . Dtos . FormulaRecord f
630
669
=> ConvertFieldValueToStringByType ( f . Type , null , f . Value , serializer ) ,
631
- System . Text . Json . JsonValueKind . Array => serializer . TextRecordToString ( record as Base . Dtos . TextRecord [ ] ) ,
670
+ JsonValueKind . Array => serializer . TextRecordToString ( record as Base . Dtos . TextRecord [ ] ) ,
632
671
_ => null
633
672
} ;
634
673
}
635
674
}
636
- }
675
+ }
0 commit comments