-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bucket.cs
70 lines (52 loc) · 2.36 KB
/
Bucket.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 System.Linq;
using System.Text;
using System.Collections.Generic;
namespace Bitmex {
using Newtonsoft.Json;
public class Bucket {
private List<Bucket> _bucketList;
private Bucket _last;
[JsonProperty(PropertyName = "symbol")]
public string Symbol { get; set; }
[JsonProperty(PropertyName = "timestamp")]
public string Timestamp { get; set; }
[JsonProperty(PropertyName = "open")]
public string Open { get; set; }
[JsonProperty(PropertyName = "high")]
public string High { get; set; }
[JsonProperty(PropertyName = "low")]
public string Low { get; set; }
[JsonProperty(PropertyName = "close")]
public string Close { get; set; }
[JsonProperty(PropertyName = "volume")]
public string Volume { get; set; }
// Property to set and retrieve the bucketed JSON data
public List<Bucket> List { get => _bucketList; }
// Property to retrieve the last element of a bucketed JSON list
public Bucket Last { get => _last; }
internal void AddFromJson(string json) {
_bucketList = new List<Bucket>();
// Convert JSON string from array object
// to collection of singletons
json = json.Replace("[", string.Empty);
json = json.Replace("]", string.Empty);
// Split JSON string into bucket array keeping delimiter
string[] jsonStringArray = json.Split("},");
// Remove array last element closing curly brace
// to avoid JSON parse error
int i = jsonStringArray.Length -1;
string lastElement = (jsonStringArray[i])
.Replace("}", string.Empty);
jsonStringArray[i] = lastElement;
// Add each JSON item to bucket list
StringBuilder sb = new StringBuilder();
string item;
foreach (string element in jsonStringArray) {
item = $"{element.Substring(0, element.Length)}}}";
// Deserialise reconstructed element and add to list
_bucketList.Add(JsonConvert.DeserializeObject<Bucket>(item));
}
_last = _bucketList.Last();
}
}
}