Esempio n. 1
0
 /// <summary>
 /// 从 Json 文本中解析出类型 <typeparamref name="T"/> 的对象,<typeparamref name="T"/> 可以是匿名类型。
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="json">表示对象的 Json 文本</param>
 /// <param name="anyObj">可序列化的匿名类型。</param>
 /// <returns>对象。</returns>
 public T Deserialize <T>(string json, T anyObj)
 {
     using (var context = new JsonConvertContext())
     {
         return(Deserialize <T>(json));
     }
 }
Esempio n. 2
0
        /// <summary>
        /// 将对象转换为使用文本并写入到流中。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <param name="writer"></param>
        public void Serialize <T>(T value, JsonWriter writer)
        {
            var ser = new JsonSerialize(this, writer, Option);

            using (var context = new JsonConvertContext())
            {
                ser.Serialize(value);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 从流中读取文本,解析出类型 <paramref name="type"/> 的对象。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="reader"></param>
        /// <returns></returns>
        public T Deserialize <T>(JsonReader reader)
        {
            var deser = new JsonDeserialize(this, reader, Option);

            using (var context = new JsonConvertContext())
            {
                return(deser.Deserialize <T>());
            }
        }
Esempio n. 4
0
        public T Deserialize <T>(string json)
        {
            if (string.IsNullOrEmpty(json))
            {
                return(default(T));
            }

            using (var sr = new StringReader(json))
                using (var reader = new JsonReader(sr))
                    using (var deser = new JsonDeserialize(this, reader, Option))
                        using (var context = new JsonConvertContext())
                        {
                            return(deser.Deserialize <T>());
                        }
        }
Esempio n. 5
0
        public object Deserialize(string json, Type type)
        {
            if (string.IsNullOrEmpty(json))
            {
                return(null);
            }

            using (var sr = new StringReader(json))
                using (var reader = new JsonReader(sr))
                    using (var deser = new JsonDeserialize(this, reader, Option))
                        using (var context = new JsonConvertContext())
                        {
                            return(deser.Deserialize(type));
                        }
        }
Esempio n. 6
0
        public string Serialize <T>(T value)
        {
            using (var sw = new StringWriter(CultureInfo.InvariantCulture))
                using (var writer = new JsonWriter(sw))
                    using (var ser = new JsonSerialize(this, writer, Option))
                        using (var context = new JsonConvertContext())
                        {
                            if (Option.Indent)
                            {
                                writer.Indent = 4;
                            }

                            ser.Serialize(value);
                            return(sw.ToString());
                        }
        }