<commit_before>﻿/*
 * Authors:
 *   钟峰(<NAME>) <<EMAIL>>
 *
 * Copyright (C) 2013-2015 Zongsoft Corporation <http://www.zongsoft.com>
 *
 * This file is part of Zongsoft.CoreLibrary.
 *
 * Zongsoft.CoreLibrary is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Zongsoft.CoreLibrary is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Zongsoft.CoreLibrary; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

using System;
using System.IO;
using System.Reflection;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;

namespace Zongsoft.Runtime.Serialization
{
	public class Serializer : ISerializer
	{
		#region 静态字段
		private static ISerializer _text = new Serializer(new TextSerializationWriter());
		private static ISerializer _json = new Serializer(new JsonSerializationWriter());
		#endregion

		#region 事件定义
		public event EventHandler<SerializationEventArgs> Serializing;
		public event EventHandler<SerializationEventArgs> Serialized;
		#endregion

		#region 成员字段
		private SerializerSettings _settings;
		private ISerializationWriter _writer;
		#endregion

		#region 构造函数
		public Serializer(ISerializationWriter writer) : this(writer, null)
		{
		}

		public Serializer(ISerializationWriter writer, SerializerSettings settings)
		{
			if(writer == null)
				throw new ArgumentNullException("writer");

			_writer = writer;
			_settings = settings ?? new SerializerSettings();
		}
		#endregion

		#region 静态属性
		public static ISerializer Text
		{
			get
			{
				return _text;
			}
			set
			{
				if(value == null)
					throw new ArgumentNullException();

				_text = value;
			}
		}

		public static ISerializer Json
		{
			get
			{
				return _json;
			}
			set
			{
				if(value == null)
					throw new ArgumentNullException();

				_json = value;
			}
		}
		#endregion

		#region 公共属性
		public SerializerSettings Settings
		{
			get
			{
				return _settings;
			}
		}

		public ISerializationWriter Writer
		{
			get
			{
				return _writer;
			}
			set
			{
				if(value == null)
					throw new ArgumentNullException();

				_writer = value;
			}
		}
		#endregion

		#region 公共方法
		public T Deserialize<T>(Stream serializationStream)
		{
			return (T)this.Deserialize(serializationStream, typeof(Type));
		}

		public object Deserialize(Stream serializationStream)
		{
			throw new NotImplementedException();
		}

		public object Deserialize(Stream serializationStream, Type type)
		{
			if(serializationStream == null)
				throw new ArgumentNullException("serializationStream");

			if(type == null)
				throw new ArgumentNullException("type");

			throw new NotImplementedException();
		}

		public void Serialize(Stream serializationStream, object graph)
		{
			if(_writer == null)
				throw new InvalidOperationException("The value of property 'Writer' is null.");

			if(graph == null)
				return;
<commit_msg>将 Serializer 类增加对 ITextSerializer 接口的实现。<commit_after>﻿/*
 * Authors:
 *   钟峰(<NAME>) <<EMAIL>>
 *
 * Copyright (C) 2013-2015 Zongsoft Corporation <http://www.zongsoft.com>
 *
 * This file is part of Zongsoft.CoreLibrary.
 *
 * Zongsoft.CoreLibrary is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Zongsoft.CoreLibrary is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Zongsoft.CoreLibrary; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

using System;
using System.IO;
using System.Reflection;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;

namespace Zongsoft.Runtime.Serialization
{
	public class Serializer : ISerializer, ITextSerializer
	{
		#region 静态字段
		private static ITextSerializer _text = new Serializer(new TextSerializationWriter());
		private static ITextSerializer _json = new Serializer(new JsonSerializationWriter());
		#endregion

		#region 事件定义
		public event EventHandler<SerializationEventArgs> Serializing;
		public event EventHandler<SerializationEventArgs> Serialized;
		#endregion

		#region 成员字段
		private SerializerSettings _settings;
		private ISerializationWriter _writer;
		#endregion

		#region 构造函数
		public Serializer(ISerializationWriter writer) : this(writer, null)
		{
		}

		public Serializer(ISerializationWriter writer, SerializerSettings settings)
		{
			if(writer == null)
				throw new ArgumentNullException("writer");

			_writer = writer;
			_settings = settings ?? new SerializerSettings();
		}
		#endregion

		#region 静态属性
		public static ITextSerializer Text
		{
			get
			{
				return _text;
			}
			set
			{
				if(value == null)
					throw new ArgumentNullException();

				_text = value;
			}
		}

		public static ITextSerializer Json
		{
			get
			{
				return _json;
			}
			set
			{
				if(value == null)
					throw new ArgumentNullException();

				_json = value;
			}
		}
		#endregion

		#region 公共属性
		public SerializerSettings Settings
		{
			get
			{
				return _settings;
			}
		}

		public ISerializationWriter Writer
		{
			get
			{
				return _writer;
			}
			set
			{
				if(value == null)
					throw new ArgumentNullException();

				_writer = value;
			}
		}
		#endregion

		#region 公共方法
		public T Deserialize<T>(Stream serializationStream)
		{
			return (T)this.Deserialize(serializationStream, typeof(Type));
		}

		public object Deserialize(Stream serializationStream)
		{
			throw new NotImplementedException();
		}

		public object Deserialize(Stream serializationStream, Type type)
		{
			if(serializationStream == null)
				throw new ArgumentNullException("serializationStream");

			if(type == null)
				throw new ArgumentNullException("type");

			throw new NotImplementedException();
		}

		public object Deserialize(TextReader reader)
		{
			if(reader == null)
				throw new ArgumentNullException("reader");

			throw new NotImplementedException();
		}

		public object Deserialize(TextReader reader, Type type)
		{
			if(reader == null)
				throw new ArgumentNullException("reader");

			if(type == null)
				throw new ArgumentNullException("type");

			throw new NotImplementedException();
		}

		public T Deserialize<T>(TextReader reader)
		{
			return (T)this.Deserialize(reader, typeof(T));
		}

		public string Serialize(object graph)
		{
			if(graph == null)
				return null;

			using(var stream = new MemoryStream())
			{
				this.Serialize(stream, graph);

				stream.Position = 0;

				using(var reader = new StreamReader(stream, Encoding.UTF8))
				{
					return reader.ReadToEnd();
				}
			}
		}

		public void Serialize(TextWriter writer, object graph)
		{
			if(writer == null)
				throw new ArgumentNullException("writer");

			if(graph == null)
				return;

			using(var stream = new MemoryStream())
			{
				this.Serialize(stream, graph);

				stream.Position = 0;

				using(var reader = new StreamReader(stream, writer.Encoding))
				{
					string line = null;

					while((line = reader.ReadLine()) != null)
					{
						writer.WriteLine(line);
					}
				}
			}
		}

		public void Serialize(Stream serializationStream, object graph)
		{
			if(_writer == null)
				throw new InvalidOperationException("The value of property 'Writer' is null.");

			if(graph == null)
				return;

{'fraction_non_alphanumeric': 0.0846554582050675, 'fraction_numerical': 0.005327965901018234, 'mean_word_length': 3.565945945945946, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '117510', 'n_tokens_mistral': 2938, 'n_tokens_neox': 2466, 'n_words': 778}