-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utilities.cs
70 lines (60 loc) · 2.04 KB
/
Utilities.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
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System.Text.RegularExpressions;
namespace aParser
{
public class Utilities
{
public static void GetLnColByPosition(string text, int position, out int lineIndex, out int columnIndex)
{
lineIndex = 1;
columnIndex = 0;
var lines = text.Split('\r');
foreach (string line in lines)
{
if (position < line.Length)
{
columnIndex = position;
break;
}
else
{
position -= line.Length + 1;
}
lineIndex++;
}
}
public static string JsonSerialize(object value)
{
return JsonConvert.SerializeObject(
value,
new JsonSerializerSettings()
{
Converters = new List<JsonConverter> { new StringEnumConverter() },
NullValueHandling = NullValueHandling.Ignore,
TypeNameHandling = TypeNameHandling.Auto,
SerializationBinder = new TypeNameSerializationBinder()
}
);
}
public static string ToSnakeCase(string text)
{
text = Regex.Replace(text, @"(.)([A-Z][a-z]+)", "$1_$2");
text = Regex.Replace(text, @"([a-z0-9])([A-Z])", "$1_$2");
return text.ToLower();
}
}
public class TypeNameSerializationBinder : ISerializationBinder
{
public void BindToName(Type serializedType, out string? assemblyName, out string? typeName)
{
assemblyName = null;
typeName = Utilities.ToSnakeCase(serializedType.Name);
}
public Type BindToType(string? assemblyName, string typeName)
{
return Type.GetType(typeName)!;
}
}
}