ChartWindow: use System.Text.Json instead of Newtonsoft.Json

This commit is contained in:
2023-09-06 14:00:30 +02:00
parent faaeefe6ce
commit 324c5db94e
2 changed files with 40 additions and 55 deletions

View File

@ -1,12 +1,8 @@
using Newtonsoft.Json.Linq;
using ScottPlot;
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Markup;
using System.Text.Json.Nodes;
namespace Elwig.Models {
public class Graph : ICloneable {
@ -30,7 +26,7 @@ namespace Elwig.Models {
DataY = DataGen.Zeros(MaxX - MinX + 1);
}
public Graph(string type, int num, JToken graphData, string contracts, int minX, int maxX) {
public Graph(string type, int num, JsonObject graphData, string contracts, int minX, int maxX) {
Type = type;
Num = num;
Contracts = contracts;
@ -52,8 +48,8 @@ namespace Elwig.Models {
DataY = dataY;
}
private void ParseGraphData(JToken graphData) {
var GraphPoints = graphData.Children().OfType<JProperty>().ToDictionary(p => int.Parse(p.Name[..^2]), p => (double)p.Value);
private void ParseGraphData(JsonObject graphData) {
var GraphPoints = graphData.ToDictionary(p => int.Parse(p.Key[..^2]), p => (double)p.Value?.AsValue());
if (GraphPoints.Keys.Count < 1) {
return;
@ -99,19 +95,19 @@ namespace Elwig.Models {
}
}
public JObject ToJson() {
JObject graph = new();
public JsonObject ToJson() {
JsonObject graph = new();
if (DataY[0] != DataY[1]) {
graph.Add(new JProperty(DataX[0] + Type.ToLower(), Math.Round(DataY[0], 4)));
graph.Add(new KeyValuePair<string, JsonNode?>(DataX[0] + Type.ToLower(), Math.Round(DataY[0], 4)));
}
for (int i = 1; i < DataX.Length - 1; i++) {
if (Math.Round(DataY[i] - DataY[i - 1], 4) != Math.Round(DataY[i + 1] - DataY[i], 4)) {
graph.Add(new JProperty(DataX[i] + Type.ToLower(), Math.Round(DataY[i], 4)));
graph.Add(new KeyValuePair<string, JsonNode?>(DataX[i] + Type.ToLower(), Math.Round(DataY[i], 4)));
}
}
if (DataY[^1] != DataY[^2]) {
graph.Add(new JProperty(DataX[^1] + Type.ToLower(), Math.Round(DataY[^1], 4)));
graph.Add(new KeyValuePair<string, JsonNode?>(DataX[^1] + Type.ToLower(), Math.Round(DataY[^1], 4)));
}
return graph;
}