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

  • 相关阅读:
    find ./ -type d ! -name "."
    Linux入门-进程、计划任务
    Linux入门-用户管理
    Linux入门-shell使用技巧
    Linux入门-压缩、解压
    Linux入门-常用命令
    MySQL杂项(索引注意事项 快速导入导出数据 锁 字符集 慢查询)
    MySQL分区实验
    Lvs网络负载均衡 直接路由(dr)
    Lvs网络负载均衡 隧道(ip tunl)
  • 原文地址:https://www.cnblogs.com/zeroone/p/2477092.html
Copyright © 2011-2022 走看看