-
Notifications
You must be signed in to change notification settings - Fork 2
/
Extensions.cs
68 lines (61 loc) · 2.13 KB
/
Extensions.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
using BencodeNET.Objects;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
namespace pod.xledger.sql_server {
public static class Extensions {
public static bool TryGetNonBlankString(this BDictionary d, string key, out string s) {
if (!(d.TryGetValue(key, out IBObject op) && op is BString bs)) {
s = null;
return false;
}
var tmp = bs.ToString();
if (!string.IsNullOrWhiteSpace(tmp)) {
s = tmp;
return true;
} else {
s = null;
return false;
}
}
public static bool TryGetNonBlankString(this IReadOnlyDictionary<string, object> d, string key, out string s) {
if (!(d.TryGetValue(key, out var tmp) && tmp is string str)) {
s = null;
return false;
}
if (!string.IsNullOrWhiteSpace(str)) {
s = str;
return true;
} else {
s = null;
return false;
}
}
public static bool TryGetNonBlankString(this IReadOnlyDictionary<string, JToken> d, string key, out string s) {
if (!(d.TryGetValue(key, out var tmp)
&& tmp.Type == JTokenType.String
&& null != (s = tmp.ToObject<string>()))) {
s = null;
return false;
}
if (!string.IsNullOrWhiteSpace(s)) {
return true;
} else {
s = null;
return false;
}
}
public static bool TryGetBool(this IReadOnlyDictionary<string, JToken> d, string key, out bool b) {
if (d.TryGetValue(key, out var tmp)
&& tmp.Type == JTokenType.Boolean
&& tmp is JValue tmp2) {
b = (bool)tmp2.Value;
return true;
}
b = false;
return false;
}
}
}