diff options
| author | Shlomo Hecht <shlomo@twine-s.com> | 2018-05-02 17:36:54 +0300 |
|---|---|---|
| committer | Shlomo Hecht <shlomo@twine-s.com> | 2018-05-02 17:36:54 +0300 |
| commit | ee697f7a3350d0a97bddee4de3a2ae4f9d285052 (patch) | |
| tree | 2dc2e3bb811b0d89a3c4c51801c1572966fcee7c /Software/Visual_Studio/Tango.Core | |
| parent | 73c4b814f1f28170ae72723568189096413c3564 (diff) | |
| download | Tango-ee697f7a3350d0a97bddee4de3a2ae4f9d285052.tar.gz Tango-ee697f7a3350d0a97bddee4de3a2ae4f9d285052.zip | |
merge
Diffstat (limited to 'Software/Visual_Studio/Tango.Core')
5 files changed, 106 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.Core/ExtensionMethods/ObjectExtensions.cs b/Software/Visual_Studio/Tango.Core/ExtensionMethods/ObjectExtensions.cs index 87f410542..9d17bd198 100644 --- a/Software/Visual_Studio/Tango.Core/ExtensionMethods/ObjectExtensions.cs +++ b/Software/Visual_Studio/Tango.Core/ExtensionMethods/ObjectExtensions.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; +using Tango.Core.Json; using Tango.Serialization; /// <summary> @@ -93,4 +94,28 @@ public static class ObjectExtensions return JsonConvert.SerializeObject(obj, Formatting.Indented); } + + /// <summary> + /// Serializes the specified object to indented json string. + /// </summary> + /// <param name="obj">The object.</param> + /// <returns></returns> + public static String ToJsonString(this Object obj, params String[] ignoreProperties) + { + var settings = new JsonSerializerSettings() { ContractResolver = new DynamicContractResolver(ignoreProperties) }; + settings.Converters.Add(new StringEnumConverter { CamelCaseText = false }); + return JsonConvert.SerializeObject(obj, Formatting.Indented, settings); + } + + /// <summary> + /// Serializes the specified object to indented json html string. + /// </summary> + /// <param name="obj">The object.</param> + /// <returns></returns> + public static String ToHtmlJsonString(this Object obj, params String[] ignoreProperties) + { + var settings = new JsonSerializerSettings() { ContractResolver = new HtmlContractResolver(ignoreProperties) }; + settings.Converters.Add(new StringEnumConverter { CamelCaseText = false }); + return JsonConvert.SerializeObject(obj, Formatting.Indented, settings); + } } diff --git a/Software/Visual_Studio/Tango.Core/Helpers/EmbeddedResourceHelper.cs b/Software/Visual_Studio/Tango.Core/Helpers/EmbeddedResourceHelper.cs index d1d556296..bdf2cb684 100644 --- a/Software/Visual_Studio/Tango.Core/Helpers/EmbeddedResourceHelper.cs +++ b/Software/Visual_Studio/Tango.Core/Helpers/EmbeddedResourceHelper.cs @@ -15,5 +15,15 @@ namespace Tango.Core.Helpers Assembly asm = Assembly.GetCallingAssembly(); return asm.GetManifestResourceStream(relativePath); } + + public static String GetEmbeddedResourceText(String relativePath) + { + Assembly asm = Assembly.GetCallingAssembly(); + using (Stream st = asm.GetManifestResourceStream(relativePath)) + { + StreamReader reader = new StreamReader(st); + return reader.ReadToEnd(); + } + } } } diff --git a/Software/Visual_Studio/Tango.Core/Json/DynamicContractResolver.cs b/Software/Visual_Studio/Tango.Core/Json/DynamicContractResolver.cs new file mode 100644 index 000000000..293b6947f --- /dev/null +++ b/Software/Visual_Studio/Tango.Core/Json/DynamicContractResolver.cs @@ -0,0 +1,30 @@ +using Newtonsoft.Json.Serialization; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; +using System.Reflection; + +namespace Tango.Core.Json +{ + public class DynamicContractResolver : DefaultContractResolver + { + private List<String> _ignoreProperties; + + public DynamicContractResolver(params String[] ignoreProperties) + { + _ignoreProperties = ignoreProperties.ToList(); + } + + protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) + { + JsonProperty property = base.CreateProperty(member, memberSerialization); + + property.ShouldSerialize = (x) => !_ignoreProperties.Contains(property.PropertyName); + + return property; + } + } +} diff --git a/Software/Visual_Studio/Tango.Core/Json/HtmlContractResolver.cs b/Software/Visual_Studio/Tango.Core/Json/HtmlContractResolver.cs new file mode 100644 index 000000000..c4affd3ad --- /dev/null +++ b/Software/Visual_Studio/Tango.Core/Json/HtmlContractResolver.cs @@ -0,0 +1,39 @@ +using Newtonsoft.Json.Serialization; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; +using System.Reflection; + +namespace Tango.Core.Json +{ + public class HtmlContractResolver : DefaultContractResolver + { + private List<String> _ignoreProperties; + + public HtmlContractResolver(params String[] ignoreProperties) + { + if (ignoreProperties != null) + { + _ignoreProperties = ignoreProperties.ToList(); + } + else + { + _ignoreProperties = new List<string>(); + } + } + + protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) + { + JsonProperty property = base.CreateProperty(member, memberSerialization); + + property.ShouldSerialize = (x) => !_ignoreProperties.Contains(property.PropertyName); + + property.PropertyName = "<b>" + property.PropertyName.Replace("\"", "") + "<b/>"; + + return property; + } + } +} diff --git a/Software/Visual_Studio/Tango.Core/Tango.Core.csproj b/Software/Visual_Studio/Tango.Core/Tango.Core.csproj index a9196237b..648137190 100644 --- a/Software/Visual_Studio/Tango.Core/Tango.Core.csproj +++ b/Software/Visual_Studio/Tango.Core/Tango.Core.csproj @@ -91,6 +91,8 @@ <Compile Include="Helpers\PathHelper.cs" /> <Compile Include="Helpers\ThreadsHelper.cs" /> <Compile Include="IParameterized.cs" /> + <Compile Include="Json\HtmlContractResolver.cs" /> + <Compile Include="Json\DynamicContractResolver.cs" /> <Compile Include="ParameterIgnoreAttribute.cs" /> <Compile Include="ParameterItem.cs" /> <Compile Include="ParameterItemAttribute.cs" /> |
