aboutsummaryrefslogtreecommitdiff
path: root/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs
blob: a1e458356aba302a24dd4321ae32c7f94d9f4c36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System.Text.Json;

namespace CrupestApi.Commons.Crud;

public class EntityJsonHelper
{
    private readonly TableInfo _table;

    public EntityJsonHelper(TableInfo table)
    {
        _table = table;
    }

    public virtual JsonDocument ConvertEntityToJson(object? entity)
    {
        if (entity is null) return JsonSerializer.SerializeToDocument<object?>(null);

        var result = new Dictionary<string, object?>();

        foreach (var column in _table.ColumnInfos)
        {
            if (column.PropertyInfo is not null)
            {
                result.Add(column.ColumnName, column.PropertyInfo.GetValue(entity));
            }
        }

        return JsonSerializer.SerializeToDocument(result);
    }

    public virtual object? ConvertJsonToEntity(JsonDocument? json)
    {
        // TODO: Implement this.
        throw new NotImplementedException();
    }
}