public override T?Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType == JsonTokenType.StartObject)
            {
                var output = TypeOf <T> .Create();

                while (reader.Read() && reader.TokenType == JsonTokenType.PropertyName)
                {
                    var name = reader.GetString();
                    if (reader.Read())
                    {
                        var property = TypeOf <T> .Properties[name !];
        /// <summary>
        /// Maps RowSet to models by their properties.<br/>
        /// Matches model property names to RowSet column names.
        /// </summary>
        public static T[] MapModels <T>(this RowSet @this)
            where T : new()
        {
            var properties = TypeOf <T> .Properties.GetValues(@this.Columns)
                             .If(property => property !.Setter is not null)
                             .ToArray();

            var items = new T[@this.Rows.Length];

            @this.Rows.Do((row, rowIndex) =>
            {
                var item = TypeOf <T> .Create();
                properties.Do((property, columnIndex) => property?.SetValue(item, row[columnIndex]));
                items[rowIndex] = item;
            });
            return(items);
        }
Exemple #3
0
        public override T?Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType == JsonTokenType.StartObject)
            {
                var output = TypeOf <T> .Create();

                while (reader.Read() && reader.TokenType == JsonTokenType.PropertyName)
                {
                    var name = reader.GetString() !;
                    if (reader.Read())
                    {
                        var field = TypeOf <T> .Fields[name];
                        if (!field.Static && field.Setter is not null)
                        {
                            field.SetValue(output, reader.TokenType switch
                            {
                                JsonTokenType.StartObject or JsonTokenType.StartArray => JsonSerializer.Deserialize(ref reader, field.FieldType, options),
                                _ => reader.GetValue()
                            });