-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSData.cs
172 lines (147 loc) · 5.42 KB
/
CSData.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;
using System.Data;
using System.Diagnostics;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Runtime.InteropServices;
namespace OccView
{
public class WeatherForecast
{
public DateTimeOffset Date { get; set; }
public int TemperatureCelsius { get; set; }
public string Summary { get; set; }
public string SummaryField;
public IList<DateTimeOffset> DatesAvailable { get; set; }
public Dictionary<string, HighLowTemps> TemperatureRanges { get; set; }
public string[] SummaryWords { get; set; }
public WeatherForecast()
{
Date = DateTimeOffset.UtcNow;
TemperatureCelsius = 105;
Summary = "Weather Forecast Success";
DatesAvailable = new List<DateTimeOffset>();
DatesAvailable.Add(DateTimeOffset.MaxValue);
DatesAvailable.Add(DateTimeOffset.MinValue);
TemperatureRanges = new Dictionary<string, HighLowTemps>();
TemperatureRanges["Hot"] = new HighLowTemps(100, 88);
TemperatureRanges["Cold"] = new HighLowTemps(55, 49);
SummaryWords = new string[2];
SummaryWords[0] = "Warm";
SummaryWords[1] = "Cool";
}
}
public struct HighLowTemps
{
public int High { get; set; }
public int Low { get; set; }
public HighLowTemps(int hi, int lo)
{
High = hi;
Low = lo;
}
}
public class CSData
{
public CSData()
{
occData = new OCCData();
}
public void InitWeather()
{
WeatherForecast weather = new WeatherForecast();
string jsonString = JsonConvert.SerializeObject(weather, Formatting.Indented);
File.WriteAllText(filepath + "CSJson.json", jsonString);
}
public string JsonPath()
{
OpenFileDialog anOpenFileDialog = new OpenFileDialog();
string aDataDir = Environment.GetEnvironmentVariable("JsonDataPath");
string aFilter = "";
anOpenFileDialog.InitialDirectory = (aDataDir + "\\json");
aFilter = "JSON Files (*.json)|*.json";
anOpenFileDialog.Filter = aFilter + "|All files(*.*)|*.*";
if (anOpenFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
return anOpenFileDialog.FileName;
}
return null;
}
public void ToOccJson()
{
string filename = JsonPath();
if (filename == "" || filename == null)
return;
if (!occData.LoadJson(filename))
{
MessageBox.Show("Can't read this file", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
public void LoadJson()
{
string filename = JsonPath();
if (filename == "" || filename == null)
{
return;
}
//record analyze json file in C# time elapsed
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
//read json object from json file
StreamReader fileReader = File.OpenText(filename);
JsonTextReader reader = new JsonTextReader(fileReader);
csJson = (JObject)JToken.ReadFrom(reader);
stopwatch.Stop();
string dur = stopwatch.ElapsedMilliseconds.ToString();
string num = csJson.Count.ToString();
Console.WriteLine("CSData::LoadJson() size " + num + " cost " + dur + " ms");
stopwatch.Reset();
//write json object to json file
stopwatch.Start();
string output = Newtonsoft.Json.JsonConvert.SerializeObject(csJson, Newtonsoft.Json.Formatting.Indented);
File.WriteAllText("D:\\Test\\Layout.json", output);
stopwatch.Stop();
dur = stopwatch.ElapsedMilliseconds.ToString();
Console.WriteLine("CSData::WriteJson() cost " + dur + " ms");
stopwatch.Reset();
}
public void AnalyzeJson()
{
foreach(var x in csJson as JObject)
{
Console.WriteLine("{0} : {1}", x.Key, x.Value);
}
}
public void TestConvert()
{
//create a struct instance
HighLowTemps temp = new HighLowTemps(35, 10);
//get instance size and allocate memory pointer
int size = Marshal.SizeOf(temp);
byte[] bytes = new byte[size];
IntPtr structPtr = Marshal.AllocHGlobal(size);
//convert struct to pointer
Marshal.StructureToPtr(temp, structPtr, false);
Marshal.Copy(structPtr, bytes, 0, size);
//pass struct to cpp function
HighLowTemp tp = new HighLowTemp();
tp.high = temp.High;
tp.low = temp.Low;
occData.TestTemp(tp);
//occJson.TestTempByt(bytes);
occData.TestTempPtr(structPtr);
Marshal.FreeHGlobal(structPtr);
}
//declare json object and file path
private OCCData occData;
private JObject csJson;
private string filepath = "D:\\";
}
}