zoukankan      html  css  js  c++  java
  • json extionsion

    using System.Collections.Generic;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;

    namespace Discovery.Utilities.Format
    {
    public static class StringExtension
    {
    public static string ToJson(this object obj)
    {
    if (obj == null)
    {
    return "";
    }
    return JsonConvert.SerializeObject(obj, Formatting.Indented, new CustomDateTimeConverter());
    }

    public static string ToJsonNoIndented(this object obj)
    {
    if (obj == null)
    {
    return "";
    }
    return JsonConvert.SerializeObject(obj, Formatting.None, new CustomDateTimeConverter());
    }

    /// <summary></summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public static string ToJsonNoIndentedNoNull(this object obj)
    {
    if (obj == null)
    {
    return "";
    }
    var jSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, Converters = new List<JsonConverter>() { new CustomDateTimeConverter() } };
    return JsonConvert.SerializeObject(obj, Formatting.None, jSetting);
    }

    public static object DeserializeObject(this string value)
    {
    return JsonConvert.DeserializeObject(value);
    }

    /// <summary>
    /// if null return "", if less than count return source string, if longer than count cut to count.
    /// </summary>
    /// <param name="str"></param>
    /// <param name="length">length</param>
    /// <returns></returns>
    public static string LimitTo(this string str, int length)
    {
    if (string.IsNullOrEmpty(str))
    {
    return str;
    }
    if (str.Length > length)
    {
    return str.Substring(0, length);
    }
    return str;
    }
    }

    public class CustomDateTimeConverter : IsoDateTimeConverter
    {
    public CustomDateTimeConverter()
    {
    base.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
    }
    }
    }

  • 相关阅读:
    流水线操作verilog
    16x16移位相加乘法器verilog实现
    Nios II对flash进行读写(DE2)
    initial使用的要点
    边沿检测电路设计verilog
    DDoS攻防战 (四):CC攻击防御系统部署
    DDoS攻防战(三):ip黑白名单防火墙frdev的原理与实现
    DDoS攻防战 (二) :CC攻击工具实现与防御理论
    DDoS攻防战 (一) : 概述
    IP流量重放与pcap文件格式解析
  • 原文地址:https://www.cnblogs.com/netact/p/5708231.html
Copyright © 2011-2022 走看看