-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathJsonForms.cs
125 lines (122 loc) · 4.49 KB
/
JsonForms.cs
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using SurveyJSAsFormLibrary.DomainModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace SurveyJSAsFormLibrary.Code
{
public class JSONForm
{
string formName;
Type formType;
public JSONForm(string formName)
{
this.formName = formName;
this.formType = DomainModelList.GetTypeByFormName(this.formName);
}
public bool IsValid { get { return this.FormType != null; } }
public Type FormType { get { return this.formType; } }
public dynamic GetFormJSON()
{
if(!this.IsValid) return null;
// Get a form JSON schema of the current `FormType` from the database
string json = DataStorage.GetForm(this.FormType);
// If the JSON schema is not found, load a pre-generated JSON schema
// from a file in the Data directory
if (string.IsNullOrEmpty(json)) {
json = this.getJsonFromData();
}
if(!string.IsNullOrEmpty(json))
return JsonSerializer.Deserialize(json, typeof(Object));
return new JSONGeneratorByModelClass(FormType).Generate();
}
public void SaveFormJSON(string data)
{
if (!this.IsValid) return;
DataStorage.SaveForm(this.FormType, data);
}
public DomainModel CreateNew()
{
if (!this.IsValid) return null;
DomainModel res = Activator.CreateInstance(this.FormType) as DomainModel;
res.Id = DataStorage.GenerateId();
return res;
}
public DomainModel LoadDomainModel(string id)
{
if (!this.IsValid) return null;
if(string.IsNullOrEmpty(id) || id.ToString() == "new")
{
return CreateNew();
}
string data = DataStorage.GetData(this.FormType, id);
return this.createModelFromString(data);
}
public string GetModelAsDataString(string id)
{
DomainModel obj = LoadDomainModel(id);
if (obj == null) return string.Empty;
return this.getObjAsJsonString(obj);
}
public void SaveDomainModelData(string id, string data)
{
DataStorage.SaveData(this.FormType, id, data);
}
public IList<DomainModel> GetAllObjects()
{
var res = new List<DomainModel>();
if (!IsValid) return res;
foreach(string data in DataStorage.GetAllDataByType(this.FormType))
{
res.Add(this.createModelFromString(data));
}
return res;
}
private string getJsonFromData()
{
string filePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); ;
filePath = Path.Combine(filePath, "Data", this.formName + ".json");
if (!File.Exists(filePath)) return string.Empty;
return File.ReadAllText(filePath);
}
private DomainModel createModelFromString(string data)
{
return JsonSerializer.Deserialize(data, this.FormType, this.createSerializerOptions()) as DomainModel;
}
public string getObjAsJsonString(DomainModel obj)
{
return JsonSerializer.Serialize(obj, obj.GetType(), this.createSerializerOptions());
}
private JsonSerializerOptions createSerializerOptions()
{
var options = new JsonSerializerOptions();
options.IgnoreNullValues = true;
options.Converters.Add(new DateTimeConverter());
return options;
}
}
public class DateTimeConverter : JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
string data = reader.GetString();
if (string.IsNullOrEmpty(data)) return DateTime.MinValue;
DateTime res;
if (DateTime.TryParse(data, out res)) return res;
return DateTime.MinValue;
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
if (value == DateTime.MinValue)
{
writer.WriteNullValue();
}
else
{
writer.WriteStringValue(value.ToString("yyyy-MM-dd"));
}
}
}
}