-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTIPLOCConversionEngine.cs
More file actions
62 lines (56 loc) · 2.1 KB
/
TIPLOCConversionEngine.cs
File metadata and controls
62 lines (56 loc) · 2.1 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
57
58
59
60
61
62
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace JSONist
{
internal class TIPLOCConversionEngine
{
public Object? ds(string input)
{
var options = new JsonSerializerOptions
{
IncludeFields = true, // REQUIRED for public fields
PropertyNameCaseInsensitive = true,
WriteIndented = true,
Converters =
{
// Map the custom logic for your Enums
new CustomEnumConverter<TIPLOCSchemaProvider.BPTP>(
TIPLOCSchemaProvider.ConvertToBPlan_TimingPoint),
new CustomEnumConverter<TIPLOCSchemaProvider.StationType>(
TIPLOCSchemaProvider.ConvertToTPS_StationType),
new CustomEnumConverter<TIPLOCSchemaProvider.StationCategory>(
TIPLOCSchemaProvider.ConvertToTPS_StationCategory),
new CustomEnumConverter<TIPLOCSchemaProvider.ForceLPB>(
TIPLOCSchemaProvider.ConvertToForceLPB)
}
};
try
{
return JsonSerializer.Deserialize<TIPLOCSchemaProvider.Jfile>(input, options);
}
catch (JsonException ex)
{
return ex;
}
}
public class CustomEnumConverter<T> : JsonConverter<T> where T : struct, Enum
{
private readonly Func<string?, T> _converter;
public CustomEnumConverter(Func<string?, T> converter)
{
_converter = converter;
}
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return _converter(reader.GetString());
}
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}
}
}