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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Extensions.Options;
namespace CrupestApi.Commons.Crud;
// TODO: Register this.
/// <summary>
/// Contains all you need to do with json.
/// </summary>
public class EntityJsonHelper<TEntity> where TEntity : class
{
private readonly TableInfo _table;
private readonly IOptionsMonitor<JsonSerializerOptions> _jsonSerializerOptions;
public EntityJsonHelper(TableInfoFactory tableInfoFactory, IOptionsMonitor<JsonSerializerOptions> jsonSerializerOptions)
{
_table = tableInfoFactory.Get(typeof(TEntity));
_jsonSerializerOptions = jsonSerializerOptions;
}
public virtual Dictionary<string, object?> ConvertEntityToDictionary(object? entity, bool includeNonColumnProperties = false)
{
Debug.Assert(entity is null || entity is TEntity);
var result = new Dictionary<string, object?>();
foreach (var propertyInfo in _table.ColumnProperties)
{
var value = propertyInfo.GetValue(entity);
result[propertyInfo.Name] = value;
}
if (includeNonColumnProperties)
{
foreach (var propertyInfo in _table.NonColumnProperties)
{
var value = propertyInfo.GetValue(entity);
result[propertyInfo.Name] = value;
}
}
return result;
}
public virtual string ConvertEntityToJson(object? entity)
{
Debug.Assert(entity is null || entity is TEntity);
var dictionary = ConvertEntityToDictionary(entity);
return JsonSerializer.Serialize(dictionary, _jsonSerializerOptions.CurrentValue);
}
public virtual IInsertClause ConvertJsonElementToInsertClauses(JsonElement rootElement)
{
var insertClause = InsertClause.Create();
if (rootElement.ValueKind != JsonValueKind.Object)
{
throw new UserException("The root element must be an object.");
}
foreach (var column in _table.PropertyColumns)
{
object? value = null;
if (rootElement.TryGetProperty(column.ColumnName, out var propertyElement))
{
value = propertyElement.ValueKind switch
{
JsonValueKind.Null or JsonValueKind.Undefined => null,
JsonValueKind.Number => propertyElement.GetDouble(),
JsonValueKind.True => true,
JsonValueKind.False => false,
JsonValueKind.String => propertyElement.GetString(),
_ => throw new Exception($"Bad json value of property {column.ColumnName}.")
};
}
if (column.IsGenerated && value is not null)
{
throw new UserException($"The property {column.ColumnName} is generated. You cannot specify its value.");
}
if (column.IsNotNull && !column.CanBeGenerated && value is null)
{
throw new UserException($"The property {column.ColumnName} can't be null or generated. But you specify a null value.");
}
insertClause.Add(column.ColumnName, value);
}
return insertClause;
}
public IInsertClause ConvertJsonToEntityForInsert(string json)
{
var document = JsonSerializer.Deserialize<JsonDocument>(json, _jsonSerializerOptions.CurrentValue)!;
return ConvertJsonElementToInsertClauses(document.RootElement);
}
}
|