diff --git a/AutoWrapper.Server/Helpers/JsonSettings.cs b/AutoWrapper.Server/Helpers/JsonSettings.cs index 431d7cf..457330f 100644 --- a/AutoWrapper.Server/Helpers/JsonSettings.cs +++ b/AutoWrapper.Server/Helpers/JsonSettings.cs @@ -2,12 +2,12 @@ using Newtonsoft.Json; using System.Text.Json; - namespace AutoWrapper.Server.Helpers { public class JsonSettings { private const string DefaultResultProperty = "Result"; + public static JsonSerializerOptions DotNetJsonSettings(bool ignoreCase = true) { return new JsonSerializerOptions @@ -29,6 +29,25 @@ public static JsonSerializerSettings NewtonsoftJsonSettings(string newJsonPro return settings; } + public static JsonSerializerSettings NewtonsoftJsonSettings(string newJsonProperty, JsonSerializerSettings jsonSettings = null) + { + var jsonResolver = new CustomResultAttributeResolver(); + jsonResolver.RenameProperty(typeof(T), DefaultResultProperty, newJsonProperty); + + if (jsonSettings != null) + { + jsonSettings.ContractResolver = jsonResolver; + return jsonSettings; + } + else + { + return new JsonSerializerSettings + { + ContractResolver = jsonResolver + }; + } + } + public static JsonSerializerSettings NewtonsoftJsonSettings(string newJsonProperty) { var jsonResolver = new CustomResultAttributeResolver(); @@ -42,4 +61,4 @@ public static JsonSerializerSettings NewtonsoftJsonSettings(string newJsonProper return settings; } } -} +} \ No newline at end of file diff --git a/AutoWrapper.Server/Unwrapper.cs b/AutoWrapper.Server/Unwrapper.cs index 2aad707..6e65373 100644 --- a/AutoWrapper.Server/Unwrapper.cs +++ b/AutoWrapper.Server/Unwrapper.cs @@ -1,6 +1,6 @@ using AutoWrapper.Server.Helpers; -using NS = Newtonsoft.Json; using DN = System.Text.Json; +using NS = Newtonsoft.Json; namespace AutoWrapper.Server { @@ -8,6 +8,7 @@ public class WrapTo { public T Result { get; set; } } + public class Unwrapper { public static T Unwrap(string jsonString, string propertyToUnwrap = "", bool ignoreCase = true) @@ -25,5 +26,27 @@ public static T Unwrap(string jsonString, string propertyToUnwrap = "", bool } } + /// + /// Unwrap Api Response + /// + /// + /// json string to deserialize + /// + /// Newtonsoft SerializerSettings + /// return the deserialize value + public static T Unwrap(string jsonString, string propertyToUnwrap = "", NS.JsonSerializerSettings settings = null) + { + WrapTo data; + if (string.IsNullOrEmpty(propertyToUnwrap)) + { + data = NS.JsonConvert.DeserializeObject>(jsonString, settings); + return data.Result; + } + else + { + data = NS.JsonConvert.DeserializeObject>(jsonString, JsonSettings.NewtonsoftJsonSettings>(propertyToUnwrap, settings)); + return data.Result; + } + } } -} +} \ No newline at end of file