From fab692c15ec1003bddfa250d408f05c79328a17b Mon Sep 17 00:00:00 2001 From: Don Oerkfitz Date: Thu, 20 May 2021 22:35:40 -0500 Subject: [PATCH] added protected to and from json methods --- OSI-API/Controllers/_EndpointBase.cs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/OSI-API/Controllers/_EndpointBase.cs b/OSI-API/Controllers/_EndpointBase.cs index 1ca6576..6444968 100644 --- a/OSI-API/Controllers/_EndpointBase.cs +++ b/OSI-API/Controllers/_EndpointBase.cs @@ -1,6 +1,8 @@ using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; using System.Threading.Tasks; +using System.Text.Json; +using System.Text.Json.Serialization; namespace OSI.API.Controllers { @@ -24,5 +26,30 @@ namespace OSI.API.Controllers // DELETE: api/Clients/5 [HttpDelete("{id}")] public abstract IActionResult Delete(int id); + + protected T FromJSON(string input) + { + JsonSerializerOptions options = new() + { + Converters = { + new JsonStringEnumConverter() + } + }; + + return JsonSerializer.Deserialize(input, options); + } + + protected string ToJSON(T input) + { + JsonSerializerOptions options = new() + { + WriteIndented = true, + Converters = { + new JsonStringEnumConverter() + } + }; + + return JsonSerializer.Serialize(input, options); + } } } \ No newline at end of file