From 0ccd342bbeece121b660c20c6b8a5d0decd8ed46 Mon Sep 17 00:00:00 2001 From: Shreyas Zare Date: Sat, 24 Dec 2022 13:07:15 +0530 Subject: [PATCH] WebServiceDashboardApi: removed newtonsoft. --- DnsServerCore/WebServiceDashboardApi.cs | 154 +++++++++--------------- 1 file changed, 57 insertions(+), 97 deletions(-) diff --git a/DnsServerCore/WebServiceDashboardApi.cs b/DnsServerCore/WebServiceDashboardApi.cs index f30d6d0a..33d4a153 100644 --- a/DnsServerCore/WebServiceDashboardApi.cs +++ b/DnsServerCore/WebServiceDashboardApi.cs @@ -18,11 +18,11 @@ along with this program. If not, see . */ using DnsServerCore.Dns; -using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Globalization; using System.Net; +using System.Text.Json; using System.Threading.Tasks; using TechnitiumLibrary.Net.Dns; using TechnitiumLibrary.Net.Dns.ResourceRecords; @@ -48,29 +48,22 @@ namespace DnsServerCore #region private - private static void WriteChartDataSet(JsonTextWriter jsonWriter, string label, string backgroundColor, string borderColor, List> statsPerInterval) + private static void WriteChartDataSet(Utf8JsonWriter jsonWriter, string label, string backgroundColor, string borderColor, List> statsPerInterval) { jsonWriter.WriteStartObject(); - jsonWriter.WritePropertyName("label"); - jsonWriter.WriteValue(label); - - jsonWriter.WritePropertyName("backgroundColor"); - jsonWriter.WriteValue(backgroundColor); - - jsonWriter.WritePropertyName("borderColor"); - jsonWriter.WriteValue(borderColor); - - jsonWriter.WritePropertyName("borderWidth"); - jsonWriter.WriteValue(2); - - jsonWriter.WritePropertyName("fill"); - jsonWriter.WriteValue(true); + jsonWriter.WriteString("label", label); + jsonWriter.WriteString("backgroundColor", backgroundColor); + jsonWriter.WriteString("borderColor", borderColor); + jsonWriter.WriteNumber("borderWidth", 2); + jsonWriter.WriteBoolean("fill", true); jsonWriter.WritePropertyName("data"); jsonWriter.WriteStartArray(); + foreach (KeyValuePair item in statsPerInterval) - jsonWriter.WriteValue(item.Value); + jsonWriter.WriteNumberValue(item.Value); + jsonWriter.WriteEndArray(); jsonWriter.WriteEndObject(); @@ -128,7 +121,7 @@ namespace DnsServerCore #region public - public async Task GetStats(HttpListenerRequest request, JsonTextWriter jsonWriter) + public async Task GetStats(HttpListenerRequest request, Utf8JsonWriter jsonWriter) { string strType = request.QueryString["type"]; if (string.IsNullOrEmpty(strType)) @@ -244,25 +237,13 @@ namespace DnsServerCore jsonWriter.WriteStartObject(); foreach (KeyValuePair item in stats) - { - jsonWriter.WritePropertyName(item.Key); - jsonWriter.WriteValue(item.Value); - } + jsonWriter.WriteNumber(item.Key, item.Value); - jsonWriter.WritePropertyName("zones"); - jsonWriter.WriteValue(_dnsWebService.DnsServer.AuthZoneManager.TotalZones); - - jsonWriter.WritePropertyName("cachedEntries"); - jsonWriter.WriteValue(_dnsWebService.DnsServer.CacheZoneManager.TotalEntries); - - jsonWriter.WritePropertyName("allowedZones"); - jsonWriter.WriteValue(_dnsWebService.DnsServer.AllowedZoneManager.TotalZonesAllowed); - - jsonWriter.WritePropertyName("blockedZones"); - jsonWriter.WriteValue(_dnsWebService.DnsServer.BlockedZoneManager.TotalZonesBlocked); - - jsonWriter.WritePropertyName("blockListZones"); - jsonWriter.WriteValue(_dnsWebService.DnsServer.BlockListZoneManager.TotalZonesBlocked); + jsonWriter.WriteNumber("zones", _dnsWebService.DnsServer.AuthZoneManager.TotalZones); + jsonWriter.WriteNumber("cachedEntries", _dnsWebService.DnsServer.CacheZoneManager.TotalEntries); + jsonWriter.WriteNumber("allowedZones", _dnsWebService.DnsServer.AllowedZoneManager.TotalZonesAllowed); + jsonWriter.WriteNumber("blockedZones", _dnsWebService.DnsServer.BlockedZoneManager.TotalZonesBlocked); + jsonWriter.WriteNumber("blockListZones", _dnsWebService.DnsServer.BlockListZoneManager.TotalZonesBlocked); jsonWriter.WriteEndObject(); } @@ -274,8 +255,7 @@ namespace DnsServerCore //label format { - jsonWriter.WritePropertyName("labelFormat"); - jsonWriter.WriteValue(labelFormat); + jsonWriter.WriteString("labelFormat", labelFormat); } //label @@ -286,7 +266,7 @@ namespace DnsServerCore jsonWriter.WriteStartArray(); foreach (KeyValuePair item in statsPerInterval) - jsonWriter.WriteValue(item.Key); + jsonWriter.WriteStringValue(item.Key); jsonWriter.WriteEndArray(); } @@ -332,19 +312,19 @@ namespace DnsServerCore switch (item.Key) { case "totalAuthoritative": - jsonWriter.WriteValue("Authoritative"); + jsonWriter.WriteStringValue("Authoritative"); break; case "totalRecursive": - jsonWriter.WriteValue("Recursive"); + jsonWriter.WriteStringValue("Recursive"); break; case "totalCached": - jsonWriter.WriteValue("Cached"); + jsonWriter.WriteStringValue("Cached"); break; case "totalBlocked": - jsonWriter.WriteValue("Blocked"); + jsonWriter.WriteStringValue("Blocked"); break; } } @@ -370,7 +350,7 @@ namespace DnsServerCore case "totalRecursive": case "totalCached": case "totalBlocked": - jsonWriter.WriteValue(item.Value); + jsonWriter.WriteNumberValue(item.Value); break; } } @@ -379,10 +359,10 @@ namespace DnsServerCore jsonWriter.WritePropertyName("backgroundColor"); jsonWriter.WriteStartArray(); - jsonWriter.WriteValue("rgba(150, 150, 0, 0.5)"); - jsonWriter.WriteValue("rgba(23, 162, 184, 0.5)"); - jsonWriter.WriteValue("rgba(111, 84, 153, 0.5)"); - jsonWriter.WriteValue("rgba(255, 165, 0, 0.5)"); + jsonWriter.WriteStringValue("rgba(150, 150, 0, 0.5)"); + jsonWriter.WriteStringValue("rgba(23, 162, 184, 0.5)"); + jsonWriter.WriteStringValue("rgba(111, 84, 153, 0.5)"); + jsonWriter.WriteStringValue("rgba(255, 165, 0, 0.5)"); jsonWriter.WriteEndArray(); jsonWriter.WriteEndObject(); @@ -406,7 +386,7 @@ namespace DnsServerCore jsonWriter.WriteStartArray(); foreach (KeyValuePair item in queryTypes) - jsonWriter.WriteValue(item.Key); + jsonWriter.WriteStringValue(item.Key); jsonWriter.WriteEndArray(); } @@ -420,22 +400,24 @@ namespace DnsServerCore jsonWriter.WritePropertyName("data"); jsonWriter.WriteStartArray(); + foreach (KeyValuePair item in queryTypes) - jsonWriter.WriteValue(item.Value); + jsonWriter.WriteNumberValue(item.Value); + jsonWriter.WriteEndArray(); jsonWriter.WritePropertyName("backgroundColor"); jsonWriter.WriteStartArray(); - jsonWriter.WriteValue("rgba(102, 153, 255, 0.5)"); - jsonWriter.WriteValue("rgba(92, 184, 92, 0.5)"); - jsonWriter.WriteValue("rgba(7, 7, 7, 0.5)"); - jsonWriter.WriteValue("rgba(91, 192, 222, 0.5)"); - jsonWriter.WriteValue("rgba(150, 150, 0, 0.5)"); - jsonWriter.WriteValue("rgba(23, 162, 184, 0.5)"); - jsonWriter.WriteValue("rgba(111, 84, 153, 0.5)"); - jsonWriter.WriteValue("rgba(255, 165, 0, 0.5)"); - jsonWriter.WriteValue("rgba(51, 122, 183, 0.5)"); - jsonWriter.WriteValue("rgba(150, 150, 150, 0.5)"); + jsonWriter.WriteStringValue("rgba(102, 153, 255, 0.5)"); + jsonWriter.WriteStringValue("rgba(92, 184, 92, 0.5)"); + jsonWriter.WriteStringValue("rgba(7, 7, 7, 0.5)"); + jsonWriter.WriteStringValue("rgba(91, 192, 222, 0.5)"); + jsonWriter.WriteStringValue("rgba(150, 150, 0, 0.5)"); + jsonWriter.WriteStringValue("rgba(23, 162, 184, 0.5)"); + jsonWriter.WriteStringValue("rgba(111, 84, 153, 0.5)"); + jsonWriter.WriteStringValue("rgba(255, 165, 0, 0.5)"); + jsonWriter.WriteStringValue("rgba(51, 122, 183, 0.5)"); + jsonWriter.WriteStringValue("rgba(150, 150, 150, 0.5)"); jsonWriter.WriteEndArray(); jsonWriter.WriteEndObject(); @@ -459,17 +441,12 @@ namespace DnsServerCore { jsonWriter.WriteStartObject(); - jsonWriter.WritePropertyName("name"); - jsonWriter.WriteValue(item.Key); + jsonWriter.WriteString("name", item.Key); if (clientIpMap.TryGetValue(item.Key, out string clientDomain) && !string.IsNullOrEmpty(clientDomain)) - { - jsonWriter.WritePropertyName("domain"); - jsonWriter.WriteValue(clientDomain); - } + jsonWriter.WriteString("domain", clientDomain); - jsonWriter.WritePropertyName("hits"); - jsonWriter.WriteValue(item.Value); + jsonWriter.WriteNumber("hits", item.Value); jsonWriter.WriteEndObject(); } @@ -488,11 +465,8 @@ namespace DnsServerCore { jsonWriter.WriteStartObject(); - jsonWriter.WritePropertyName("name"); - jsonWriter.WriteValue(item.Key); - - jsonWriter.WritePropertyName("hits"); - jsonWriter.WriteValue(item.Value); + jsonWriter.WriteString("name", item.Key); + jsonWriter.WriteNumber("hits", item.Value); jsonWriter.WriteEndObject(); } @@ -511,11 +485,8 @@ namespace DnsServerCore { jsonWriter.WriteStartObject(); - jsonWriter.WritePropertyName("name"); - jsonWriter.WriteValue(item.Key); - - jsonWriter.WritePropertyName("hits"); - jsonWriter.WriteValue(item.Value); + jsonWriter.WriteString("name", item.Key); + jsonWriter.WriteNumber("hits", item.Value); jsonWriter.WriteEndObject(); } @@ -524,7 +495,7 @@ namespace DnsServerCore } } - public async Task GetTopStats(HttpListenerRequest request, JsonTextWriter jsonWriter) + public async Task GetTopStats(HttpListenerRequest request, Utf8JsonWriter jsonWriter) { string strType = request.QueryString["type"]; if (string.IsNullOrEmpty(strType)) @@ -607,17 +578,12 @@ namespace DnsServerCore { jsonWriter.WriteStartObject(); - jsonWriter.WritePropertyName("name"); - jsonWriter.WriteValue(item.Key); + jsonWriter.WriteString("name", item.Key); if (clientIpMap.TryGetValue(item.Key, out string clientDomain) && !string.IsNullOrEmpty(clientDomain)) - { - jsonWriter.WritePropertyName("domain"); - jsonWriter.WriteValue(clientDomain); - } + jsonWriter.WriteString("domain", clientDomain); - jsonWriter.WritePropertyName("hits"); - jsonWriter.WriteValue(item.Value); + jsonWriter.WriteNumber("hits", item.Value); jsonWriter.WriteEndObject(); } @@ -635,11 +601,8 @@ namespace DnsServerCore { jsonWriter.WriteStartObject(); - jsonWriter.WritePropertyName("name"); - jsonWriter.WriteValue(item.Key); - - jsonWriter.WritePropertyName("hits"); - jsonWriter.WriteValue(item.Value); + jsonWriter.WriteString("name", item.Key); + jsonWriter.WriteNumber("hits", item.Value); jsonWriter.WriteEndObject(); } @@ -657,11 +620,8 @@ namespace DnsServerCore { jsonWriter.WriteStartObject(); - jsonWriter.WritePropertyName("name"); - jsonWriter.WriteValue(item.Key); - - jsonWriter.WritePropertyName("hits"); - jsonWriter.WriteValue(item.Value); + jsonWriter.WriteString("name", item.Key); + jsonWriter.WriteNumber("hits", item.Value); jsonWriter.WriteEndObject(); }