forked from chadly/Geocoding.net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.cs
More file actions
52 lines (45 loc) · 1.15 KB
/
Extensions.cs
File metadata and controls
52 lines (45 loc) · 1.15 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
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace Geocoding
{
public static class Extensions
{
public static bool IsNullOrEmpty<T>(this ICollection<T> col)
{
return col == null || col.Count == 0;
}
public static void ForEach<T>(this IEnumerable<T> self, Action<T> actor)
{
if (actor == null)
throw new ArgumentNullException("actor");
if (self == null)
return;
foreach (T item in self)
{
actor(item);
}
}
//Universal ISO DT Converter
static readonly JsonConverter[] JSON_CONVERTERS = new JsonConverter[]
{
new IsoDateTimeConverter { DateTimeStyles = System.Globalization.DateTimeStyles.AssumeUniversal },
new StringEnumConverter(),
};
public static string ToJSON(this object o)
{
string result = null;
if (o != null)
result = JsonConvert.SerializeObject(o, Formatting.Indented, JSON_CONVERTERS);
return result ?? string.Empty;
}
public static T FromJSON<T>(this string json)
{
T o = default(T);
if (!string.IsNullOrWhiteSpace(json))
o = JsonConvert.DeserializeObject<T>(json, JSON_CONVERTERS);
return o;
}
}
}