-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathIDLiteBase.cs
111 lines (97 loc) · 1.97 KB
/
IDLiteBase.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
using System;
using System.Collections.Generic;
namespace IDLite
{
public abstract class IDLiteBase
{
protected int ParseInt(object o)
{
return (int)ParseLong(o);
}
protected int? ParseNullableInt(object o)
{
return (int?)ParseNullableLong(o);
}
protected long ParseLong(object o)
{
return ParseNullableLong(o).Value;
}
protected long? ParseNullableLong(object o)
{
if (o is long) {
return (long)o;
} else if (o is double) {
return (long)(double)o;
} else {
return null;
}
}
protected double ParseDouble(object o)
{
return ParseNullableDouble(o).Value;
}
public static double? ParseNullableDouble(object o)
{
if (o is long) {
return (double)(long)o;
} else if (o is double) {
return (double)o;
} else {
return null;
}
}
protected string ParseString(object o)
{
return ParseNullableString(o);
}
protected string ParseNullableString(object o)
{
if (o is string) {
return (string)o;
} else {
return null;
}
}
protected bool ParseBool(object o)
{
return ParseNullableBool(o).Value;
}
protected bool? ParseNullableBool(object o)
{
if (o is bool) {
return (bool)o;
} else {
return null;
}
}
protected static List<T> GetList<T>(Dictionary<string, object> dict, string name, Func<object, T> func)
{
if (dict.ContainsKey(name) && dict[name] is List<object>) {
var a = new List<T>();
foreach (var x in (List<object>)dict[name]) {
a.Add(func(x));
}
return a;
}
return null;
}
protected static object GetItem(Dictionary<string, object> dict, string name)
{
if (dict.ContainsKey(name)) {
return dict[name];
} else {
return null;
}
}
protected static List<object> SerializeList<T>(List<T> list) where T : IDLiteBase
{
var a = new List<object>();
foreach (var x in list) {
a.Add(x.Serialize());
}
return a;
}
public abstract Dictionary<string, object> Serialize();
}
}