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();
                }
            }
        }
    }

  • 相关阅读:
    做自己的ORMapping Framework ---- 第三讲 关于Attribute
    Hugepages,hugetlb_shm_group与ORA-27125
    65个面试回答技巧
    在线修改Schema
    percona innobackupex 遇到 connect to MySQL server as DBD::mysql module is not installed 问题
    MYSQL中的日期转换
    SHOW INNODB STATUS 探秘
    11g手动打补丁
    11g OCM自动打补丁
    How to create Oracle ASM devices using device-mapper multipath devices in Red Hat Enterprise Linux 6
  • 原文地址:https://www.cnblogs.com/zeroone/p/2477092.html
Copyright © 2011-2022 走看看