Ejemplo n.º 1
0
		private void SerializeDictionary(JsonWriter writer, IDictionary values)
		{
			writer.WriteStartObject();

			foreach (DictionaryEntry entry in values)
			{
				writer.WritePropertyName(entry.Key.ToString());
				SerializeValue(writer, entry.Value);
			}

			writer.WriteEndObject();
		}
Ejemplo n.º 2
0
		private void SerializeCollection(JsonWriter writer, ICollection values)
		{
			object[] collectionValues = new object[values.Count];
			values.CopyTo(collectionValues, 0);

			SerializeList(writer, collectionValues);
		}
Ejemplo n.º 3
0
		private void SerializeList(JsonWriter writer, IList values)
		{
			writer.WriteStartArray();

			for (int i = 0; i < values.Count; i++)
			{
				SerializeValue(writer, values[i]);
			}

			writer.WriteEndArray();
		}
Ejemplo n.º 4
0
		private void SerializeObject(JsonWriter writer, object value)
		{
			Type objectType = value.GetType();

			TypeConverter converter = TypeDescriptor.GetConverter(objectType);

			// use the objectType's TypeConverter if it has one and can convert to a string
			if (converter != null && !(converter is ComponentConverter) && converter.GetType() != typeof(TypeConverter))
			{
				if (converter.CanConvertTo(typeof(string)))
				{
					writer.WriteValue(converter.ConvertToInvariantString(value));
					return;
				}
			}

			writer.SerializeStack.Add(value);

			writer.WriteStartObject();

			MemberMappingCollection memberMappings = GetMemberMappings(objectType);

			foreach (MemberMapping memberMapping in memberMappings)
			{
				if (!memberMapping.Ignored && memberMapping.Readable)
					WriteMemberInfoProperty(writer, value, memberMapping.Member, memberMapping.MappingName);
			}

			writer.WriteEndObject();

			writer.SerializeStack.Remove(value);
		}
Ejemplo n.º 5
0
		private void WriteMemberInfoProperty(JsonWriter writer, object value, MemberInfo member, string propertyName)
		{
			if (!ReflectionUtils.IsIndexedProperty(member))
			{
				object memberValue = ReflectionUtils.GetMemberValue(member, value);

				if (writer.SerializeStack.IndexOf(memberValue) != -1)
				{
					switch (_referenceLoopHandling)
					{
						case ReferenceLoopHandling.Error:
							throw new JsonSerializationException("Self referencing loop");
						case ReferenceLoopHandling.Ignore:
							// return from method
							return;
						case ReferenceLoopHandling.Serialize:
							// continue
							break;
						default:
							throw new InvalidOperationException(string.Format("Unexpected ReferenceLoopHandling value: '{0}'", _referenceLoopHandling));
					}
				}

				writer.WritePropertyName(propertyName != null ? propertyName : member.Name);
				SerializeValue(writer, memberValue);
			}
		}
Ejemplo n.º 6
0
		private void SerializeValue(JsonWriter writer, object value)
		{
			JsonConverter converter;

			if (value == null)
			{
				writer.WriteNull();
			}
			else if (HasMatchingConverter(value.GetType(), out converter))
			{
				converter.WriteJson(writer, value);
			}
			else if (value is IConvertible)
			{
				IConvertible convertible = value as IConvertible;

				switch (convertible.GetTypeCode())
				{
					case TypeCode.String:
						writer.WriteValue((string)convertible);
						break;
					case TypeCode.Char:
						writer.WriteValue((char)convertible);
						break;
					case TypeCode.Boolean:
						writer.WriteValue((bool)convertible);
						break;
					case TypeCode.SByte:
						writer.WriteValue((sbyte)convertible);
						break;
					case TypeCode.Int16:
						writer.WriteValue((short)convertible);
						break;
					case TypeCode.UInt16:
						writer.WriteValue((ushort)convertible);
						break;
					case TypeCode.Int32:
						writer.WriteValue((int)convertible);
						break;
					case TypeCode.Byte:
						writer.WriteValue((byte)convertible);
						break;
					case TypeCode.UInt32:
						writer.WriteValue((uint)convertible);
						break;
					case TypeCode.Int64:
						writer.WriteValue((long)convertible);
						break;
					case TypeCode.UInt64:
						writer.WriteValue((ulong)convertible);
						break;
					case TypeCode.Single:
						writer.WriteValue((float)convertible);
						break;
					case TypeCode.Double:
						writer.WriteValue((double)convertible);
						break;
					case TypeCode.DateTime:
						writer.WriteValue((DateTime)convertible);
						break;
					case TypeCode.Decimal:
						writer.WriteValue((decimal)convertible);
						break;
					default:
						SerializeObject(writer, value);
						break;
				}
			}
			else if (value is IList)
			{
				SerializeList(writer, (IList)value);
			}
			else if (value is IDictionary)
			{
				SerializeDictionary(writer, (IDictionary)value);
			}
			else if (value is ICollection)
			{
				SerializeCollection(writer, (ICollection)value);
			}
			else if (value is Identifier)
			{
				writer.WriteRaw(value.ToString());
			}
			else
			{
				SerializeObject(writer, value);
			}
		}
Ejemplo n.º 7
0
		/// <summary>
		/// Serializes the specified <see cref="Object"/> and writes the Json structure
		/// to a <c>Stream</c> using the specified <see cref="JsonWriter"/>. 
		/// </summary>
		/// <param name="jsonWriter">The <see cref="JsonWriter"/> used to write the Json structure.</param>
		/// <param name="value">The <see cref="Object"/> to serialize.</param>
		public void Serialize(JsonWriter jsonWriter, object value)
		{
			if (jsonWriter == null)
				throw new ArgumentNullException("jsonWriter");

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

			SerializeValue(jsonWriter, value);
		}
Ejemplo n.º 8
0
		public virtual void WriteJson(JsonWriter writer, object value)
		{
			JsonSerializer serializer = new JsonSerializer();

			serializer.Serialize(writer, value);
		}