zoukankan      html  css  js  c++  java
  • JSON Helper

    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;
    using Newtonsoft.Json.Serialization;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;

    namespace Utilities
    {
    /// <summary>
    /// Customized Json Convert
    /// </summary>
    public static class JsonHelper
    {
    /// <summary>
    /// De-serialize object via JsonConvert.DeserializeObject.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="jsonString"></param>
    /// <returns>De serialized object</returns>
    public static T DeserializeObject<T>(this string jsonString)
    {
    return JsonConvert.DeserializeObject<T>(jsonString);
    }

    /// <summary>
    /// Json to Object
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="jsonString"></param>
    /// <returns></returns>
    public static T Parse<T>(string jsonString)
    {
    return (T)JsonConvert.DeserializeObject(jsonString, typeof(T));
    }
    /// <summary>
    /// Serialize object via JsonConvert.SerializeObject.
    /// If Serialization failed exception or null will be returned base on optional parameter "throwExceptionIfSerializationFailed"
    /// </summary>
    /// <param name="obj"></param>
    /// <param name="throwExceptionIfSerializationFailed"></param>
    public static string SerializeObject(this object obj, bool throwExceptionIfSerializationFailed = false)
    {
    try
    {
    return JsonConvert.SerializeObject(obj);
    }
    catch (Exception) when (!throwExceptionIfSerializationFailed)
    {
    return null;
    }
    }

    public static string SerializeWithDesensitization(object jsonObject)
    {
    if (jsonObject != null)
    {
    JsonSerializerSettings config = new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, Formatting = Formatting.Indented };
    config.Converters.Add(new StringEnumConverter());
    config.ContractResolver = new IgnoreSerializerContractResolver();
    return JsonConvert.SerializeObject(jsonObject, config);
    }
    else
    {
    return string.Empty;
    }
    }

    /// <summary>
    /// Object to Json
    /// </summary>
    /// <param name="jsonObject"></param>
    public static string Stringify(object jsonObject)
    {
    if (jsonObject != null)
    {
    JsonSerializerSettings config = new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, Formatting = Formatting.Indented };
    //Enum will be integer number instead of Enum name string after serialization
    //config.Converters.Add(new StringEnumConverter());
    return JsonConvert.SerializeObject(jsonObject, config);
    }
    else
    {
    return string.Empty;
    }
    }
    }

    public class IgnoreSerializerContractResolver : DefaultContractResolver
    {
    private readonly List<string> ignorePropertys = new List<string>() { "Password", "OldPassword", "NewPassword", "Passphrase", "PasswordBase64" };

    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
    {
    var property = base.CreateProperty(member, memberSerialization);

    if (IsIgnored(property.PropertyName))
    {
    property.ShouldSerialize = _ => false;
    property.Ignored = true;
    }
    return property;
    }

    private bool IsIgnored(string jsonPropertyName)
    {
    return ignorePropertys.Contains(jsonPropertyName, StringComparer.OrdinalIgnoreCase);
    }
    }
    }

  • 相关阅读:
    php7.4 降级 php7.1 的坑
    python 记录网页 生成pdf
    Mac 安装常用软件环境
    python 2.7 操作 excel
    007整数反转
    006Z字形变换
    005最长回文子串
    004寻找两个正序数组的中位数
    003无重复字符的最长子串
    002两数相加
  • 原文地址:https://www.cnblogs.com/researcher/p/14969087.html
Copyright © 2011-2022 走看看