Imports Newtonsoft.Json
Imports Newtonsoft.Json.Serialization

Namespace UTSModule.Service
    ''' <summary>
    ''' 针对服务任务的Json设置
    ''' 当前作用：筛选指定类中字段参与Json序列化
    ''' </summary>
    Public Class TaskJsonSettings
        Inherits DefaultContractResolver

        ''' <summary>
        ''' 需要操作的字段名
        ''' 与_retain参数结合使用,实现指定类中某些字段的Json序列化
        ''' </summary>
        Private ReadOnly _name() As String

        ''' <summary>
        ''' 是否显示指定字段
        ''' 为真时,仅显示指定的字段名
        ''' 为假时,仅不显示指定的字段名
        ''' </summary>
        Private ReadOnly _retain As Boolean

        Sub New(name2() As String, Optional retain2 As Boolean = True)
            _name = name2
            _retain = retain2
        End Sub


        Protected Overrides Function CreateProperties(type As Type, memberSerialization As MemberSerialization) As IList(Of JsonProperty)
            Dim lst As IList(Of JsonProperty) = MyBase.CreateProperties(type, memberSerialization)

            Return lst.Where(Function(x As JsonProperty)
                                 If _retain Then
                                     Return _name.Contains(x.PropertyName)
                                 Else
                                     Return Not _name.Contains(x.PropertyName)
                                 End If
                             End Function).ToList()
        End Function
    End Class
End Namespace