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

  • 相关阅读:
    C#学习笔记之——一些应用
    C#学习笔记之——面向对象编程
    C#学习笔记之——一些练习(包含了一些out的使用,string的使用,StringBuilder的使用,类的属性,最大公约数的求法,还有英雄,武器类的设置)
    C#学习笔记之——数据类型,引用参数,输出参数,数组参数,命名参数,可选参数
    C#学习笔记之——类、对象
    离散实践1
    计算机书籍
    2013年12月大学英语六级作文预测:挑战与改变
    TCP协议详解
    Uip学习简介及网址
  • 原文地址:https://www.cnblogs.com/zeroone/p/2477092.html
Copyright © 2011-2022 走看看