-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathFeaturesSimpleStateCheckerSerializerContributor.cs
More file actions
56 lines (47 loc) · 1.69 KB
/
FeaturesSimpleStateCheckerSerializerContributor.cs
File metadata and controls
56 lines (47 loc) · 1.69 KB
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
using System.Linq;
using System.Text.Json.Nodes;
using Volo.Abp.DependencyInjection;
using Volo.Abp.SimpleStateChecking;
namespace Volo.Abp.Features;
public class FeaturesSimpleStateCheckerSerializerContributor :
ISimpleStateCheckerSerializerContributor,
ISingletonDependency
{
public const string CheckerShortName = "F";
public string? SerializeToJson<TState>(ISimpleStateChecker<TState> checker)
where TState : IHasSimpleStateCheckers<TState>
{
if (checker is not RequireFeaturesSimpleStateChecker<TState> featuresSimpleStateChecker)
{
return null;
}
var jsonObject = new JsonObject {
["T"] = CheckerShortName,
["A"] = featuresSimpleStateChecker.RequiresAll
};
var nameArray = new JsonArray();
foreach (var featureName in featuresSimpleStateChecker.FeatureNames)
{
nameArray.Add(featureName);
}
jsonObject["N"] = nameArray;
return jsonObject.ToJsonString();
}
public ISimpleStateChecker<TState>? Deserialize<TState>(JsonObject jsonObject, TState state)
where TState : IHasSimpleStateCheckers<TState>
{
if (jsonObject["T"]?.ToString() != CheckerShortName)
{
return null;
}
var nameArray = jsonObject["N"] as JsonArray;
if (nameArray == null)
{
throw new AbpException("'N' is not an array in the serialized state checker! JsonObject: " + jsonObject.ToJsonString());
}
return new RequireFeaturesSimpleStateChecker<TState>(
(bool?)jsonObject["A"] ?? false,
nameArray.Select(x => x!.ToString()).ToArray()
);
}
}