zoukankan      html  css  js  c++  java
  • JsonSerializerHelper other 武胜

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.IO;
    using Newtonsoft.Json;

    namespace Serializable
    {
        public static class JsonSerializerHelper
        {
            public static TEntity JsonToEntity<TEntity>(string jsonStr)
            {
                using (StringReader sw = new StringReader(jsonStr))
                {
                    JsonSerializer serializer = new JsonSerializer
                    {
                        TypeNameHandling = TypeNameHandling.Objects,
                    };
                    using (JsonReader jw = new JsonTextReader(sw))
                    {
                        TEntity entity = serializer.Deserialize<TEntity>(jw);
                        return entity;
                    }

                }
            }

            public static string EntityToJson(object obj)
            {
                return EntityToJson(obj, null);
            }

            public static string EntityToJson(object obj, string[] exceptMemberName)
            {
                using (StringWriter sw = new StringWriter(System.Globalization.CultureInfo.InvariantCulture))
                {
                    JsonSerializer serializer = new JsonSerializer
                    {

                        NullValueHandling = NullValueHandling.Ignore,
                        ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                        ContractResolver = new NHibernateContractResolver(exceptMemberName),
                    };
                    using (JsonWriter jw = new JsonTextWriter(sw))
                    {
                        jw.Formatting = Formatting.Indented;
                        serializer.Serialize(jw, obj);
                    }
                    return sw.ToString();
                }
            }
        }
    }

  • 相关阅读:
    概率与数学期望初步
    $Luogu$ $P4316$ 绿豆蛙的归宿(附期望 $dp$ 的设计总结)
    $Luogu$ $P4427$ $[BJOI2018]$ 求和
    $SP3978$ $DISQUERY$ $-$ $Distance$ $Query$
    最近公共祖先模板(未完待续)
    $Luogu$ $P3052$ $[USACO12MAR]$ 摩天大楼里的奶牛 $Cows$ $in$ $a$ $Skyscraper$
    $Luogu$ $P2622$ 关灯问题 $mathrm{II}$
    [转载] $CF633F$ 题解
    [转载] $Luogu$ $P3933$ 题解
    2020高考回忆录(随便写写
  • 原文地址:https://www.cnblogs.com/zeroone/p/2477092.html
Copyright © 2011-2022 走看看