private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract)
        {
            contract.InvokeOnSerializing(value);

            string s;

            if (TryConvertToString(value, contract.UnderlyingType, out s))
            {
                writer.WriteValue(s);

#if !SILVERLIGHT && !PocketPC
                contract.InvokeOnSerialized(value);
#endif
                return;
            }

            SerializeStack.Add(value);
            writer.WriteStartObject();

            bool isReference = contract.IsReference ?? HasFlag(Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Objects);
            if (isReference)
            {
                writer.WritePropertyName(JsonTypeReflector.IdPropertyName);
                writer.WriteValue(Serializer.ReferenceResolver.GetReference(value));
            }
            if (HasFlag(Serializer.TypeNameHandling, TypeNameHandling.Objects))
            {
                WriteTypeProperty(writer, contract.UnderlyingType);
            }

            int initialDepth = writer.Top;

            foreach (JsonProperty property in contract.Properties)
            {
                try
                {
                    if (!property.Ignored && property.Readable)
                    {
                        WriteMemberInfoProperty(writer, value, property);
                    }
                }
                catch (Exception ex)
                {
                    if (IsErrorHandled(value, contract, property.Member.Name, ex))
                    {
                        HandleError(writer, initialDepth);
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            writer.WriteEndObject();
            SerializeStack.RemoveAt(SerializeStack.Count - 1);

            contract.InvokeOnSerialized(value);
        }
        private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract)
        {
            contract.InvokeOnSerializing(value);

#if !SILVERLIGHT && !PocketPC
            TypeConverter converter = TypeDescriptor.GetConverter(contract.UnderlyingType);

            // 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) || value is Type))
            {
                if (converter.CanConvertTo(typeof(string)))
                {
                    writer.WriteValue(converter.ConvertToInvariantString(value));

                    contract.InvokeOnSerialized(value);
                    return;
                }
            }
#else
            if (value is Guid || value is Type || value is Uri)
            {
                writer.WriteValue(value.ToString());
                return;
            }
#endif

            SerializeStack.Add(value);
            writer.WriteStartObject();

            bool isReference = contract.IsReference ?? HasFlag(_serializer.PreserveReferencesHandling, PreserveReferencesHandling.Objects);
            if (isReference)
            {
                writer.WritePropertyName(JsonTypeReflector.IdPropertyName);
                writer.WriteValue(_serializer.ReferenceResolver.GetReference(value));
            }
            if (HasFlag(_serializer.TypeNameHandling, TypeNameHandling.Objects))
            {
                WriteTypeProperty(writer, contract.UnderlyingType);
            }

            foreach (JsonProperty property in contract.Properties)
            {
                if (!property.Ignored && property.Readable)
                {
                    WriteMemberInfoProperty(writer, value, property);
                }
            }

            writer.WriteEndObject();
            SerializeStack.RemoveAt(SerializeStack.Count - 1);

            contract.InvokeOnSerialized(value);
        }
示例#3
0
        private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
        {
            contract.InvokeOnSerializing(value, this.Serializer.Context);
            this._serializeStack.Add(value);
            this.WriteObjectStart(writer, value, (JsonContract)contract, member, collectionContract, containerProperty);
            int top = writer.Top;

            foreach (JsonProperty jsonProperty in (Collection <JsonProperty>)contract.Properties)
            {
                try
                {
                    JsonContract memberContract;
                    object       memberValue;
                    if (this.CalculatePropertyValues(writer, value, (JsonContainerContract)contract, member, jsonProperty, out memberContract, out memberValue))
                    {
                        writer.WritePropertyName(jsonProperty.PropertyName);
                        this.SerializeValue(writer, memberValue, memberContract, jsonProperty, (JsonContainerContract)contract, member);
                    }
                }
                catch (Exception ex)
                {
                    if (this.IsErrorHandled(value, (JsonContract)contract, (object)jsonProperty.PropertyName, writer.ContainerPath, ex))
                    {
                        this.HandleError(writer, top);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            writer.WriteEndObject();
            this._serializeStack.RemoveAt(this._serializeStack.Count - 1);
            contract.InvokeOnSerialized(value, this.Serializer.Context);
        }
示例#4
0
        private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract, JsonProperty member, JsonContract collectionValueContract)
        {
            contract.InvokeOnSerializing(value, Serializer.Context);

            _serializeStack.Add(value);
            writer.WriteStartObject();

            bool isReference = contract.IsReference ?? HasFlag(Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Objects);

            if (isReference)
            {
                writer.WritePropertyName(JsonTypeReflector.IdPropertyName);
                writer.WriteValue(Serializer.ReferenceResolver.GetReference(this, value));
            }
            if (ShouldWriteType(TypeNameHandling.Objects, contract, member, collectionValueContract))
            {
                WriteTypeProperty(writer, contract.UnderlyingType);
            }

            int initialDepth = writer.Top;

            foreach (JsonProperty property in contract.Properties)
            {
                try
                {
                    if (!property.Ignored && property.Readable && ShouldSerialize(property, value) && IsSpecified(property, value))
                    {
                        if (property.PropertyContract == null)
                        {
                            property.PropertyContract = Serializer.ContractResolver.ResolveContract(property.PropertyType);
                        }

                        object       memberValue    = property.ValueProvider.GetValue(value);
                        JsonContract memberContract = (property.PropertyContract.UnderlyingType.IsSealed) ? property.PropertyContract : GetContractSafe(memberValue);

                        WriteMemberInfoProperty(writer, memberValue, property, memberContract);
                    }
                }
                catch (Exception ex)
                {
                    if (IsErrorHandled(value, contract, property.PropertyName, writer.ContainerPath, ex))
                    {
                        HandleError(writer, initialDepth);
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            writer.WriteEndObject();
            _serializeStack.RemoveAt(_serializeStack.Count - 1);

            contract.InvokeOnSerialized(value, Serializer.Context);
        }
        private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract, JsonProperty member, JsonContract collectionValueContract)
        {
            contract.InvokeOnSerializing(value, Serializer.Context);

            SerializeStack.Add(value);
            writer.WriteStartObject();

            bool isReference = contract.IsReference ?? HasFlag(Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Objects);

            if (isReference)
            {
                writer.WritePropertyName(JsonTypeReflector.IdPropertyName);
                writer.WriteValue(Serializer.ReferenceResolver.GetReference(this, value));
            }
            if (ShouldWriteType(TypeNameHandling.Objects, contract, member, collectionValueContract))
            {
                WriteTypeProperty(writer, contract.UnderlyingType);
            }

            //	int initialDepth = writer.Top;

            foreach (JsonProperty property in contract.Properties)
            {
                try
                {
                    if (!property.Ignored && property.Readable && ShouldSerialize(property, value) && IsSpecified(property, value))
                    {
                        object       memberValue    = property.ValueProvider.GetValue(value);
                        JsonContract memberContract = GetContractSafe(memberValue);
                        if (property.PropertyName == "collider" || property.PropertyName == "rigidbody")
                        {
                            continue;
                        }
                        WriteMemberInfoProperty(writer, memberValue, property, memberContract);
                    }
                }
                catch (Exception ex)
                {
                    Debug.LogException(ex);

                    /*	if (IsErrorHandled(value, contract, property.PropertyName, ex))
                     *              HandleError(writer, initialDepth);
                     *      else
                     *              throw;*/
                }
            }

            writer.WriteEndObject();
            SerializeStack.RemoveAt(SerializeStack.Count - 1);

            contract.InvokeOnSerialized(value, Serializer.Context);
        }
示例#6
0
        private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonContract collectionValueContract)
        {
            contract.InvokeOnSerializing(value, Serializer.Context);

            _serializeStack.Add(value);

            WriteObjectStart(writer, value, contract, member, collectionContract, collectionValueContract);

            int initialDepth = writer.Top;

            foreach (JsonProperty property in contract.Properties)
            {
                try
                {
                    if (!property.Ignored && property.Readable && ShouldSerialize(property, value) && IsSpecified(property, value))
                    {
                        if (property.PropertyContract == null)
                        {
                            property.PropertyContract = Serializer.ContractResolver.ResolveContract(property.PropertyType);
                        }

                        object       memberValue    = property.ValueProvider.GetValue(value);
                        JsonContract memberContract = (property.PropertyContract.UnderlyingType.IsSealed()) ? property.PropertyContract : GetContractSafe(memberValue);

                        if (ShouldWriteProperty(memberValue, property))
                        {
                            WriteMemberInfoProperty(writer, memberValue, property, memberContract, contract);
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (IsErrorHandled(value, contract, property.PropertyName, writer.ContainerPath, ex))
                    {
                        HandleError(writer, initialDepth);
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            writer.WriteEndObject();

            _serializeStack.RemoveAt(_serializeStack.Count - 1);

            contract.InvokeOnSerialized(value, Serializer.Context);
        }
        private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract, JsonProperty member, JsonContract collectionValueContract)
        {
            contract.InvokeOnSerializing(value, base.Serializer.Context);
            this.SerializeStack.Add(value);
            writer.WriteStartObject();
            bool?isReference = contract.IsReference;
            bool flag        = (!isReference.get_HasValue()) ? this.HasFlag(base.Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Objects) : isReference.get_Value();

            if (flag)
            {
                writer.WritePropertyName("$id");
                writer.WriteValue(base.Serializer.ReferenceResolver.GetReference(this, value));
            }
            if (this.ShouldWriteType(TypeNameHandling.Objects, contract, member, collectionValueContract))
            {
                this.WriteTypeProperty(writer, contract.UnderlyingType);
            }
            int top = writer.Top;

            using (IEnumerator <JsonProperty> enumerator = contract.Properties.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    JsonProperty current = enumerator.get_Current();
                    try
                    {
                        if (!current.Ignored && current.Readable && this.ShouldSerialize(current, value) && this.IsSpecified(current, value))
                        {
                            object       value2       = current.ValueProvider.GetValue(value);
                            JsonContract contractSafe = this.GetContractSafe(value2);
                            this.WriteMemberInfoProperty(writer, value2, current, contractSafe);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (!base.IsErrorHandled(value, contract, current.PropertyName, ex))
                        {
                            throw;
                        }
                        this.HandleError(writer, top);
                    }
                }
            }
            writer.WriteEndObject();
            this.SerializeStack.RemoveAt(this.SerializeStack.get_Count() - 1);
            contract.InvokeOnSerialized(value, base.Serializer.Context);
        }
        private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract, JsonProperty member, JsonContract collectionValueContract)
        {
            contract.InvokeOnSerializing(value, base.Serializer.Context);
            this.SerializeStack.Add(value);
            writer.WriteStartObject();
            bool?isReference = contract.IsReference;

            if ((!isReference.HasValue ? this.HasFlag(base.Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Objects) : isReference.Value))
            {
                writer.WritePropertyName("$id");
                writer.WriteValue(base.Serializer.ReferenceResolver.GetReference(this, value));
            }
            if (this.ShouldWriteType(TypeNameHandling.Objects, contract, member, collectionValueContract))
            {
                this.WriteTypeProperty(writer, contract.UnderlyingType);
            }
            int top = writer.Top;

            foreach (JsonProperty property in contract.Properties)
            {
                try
                {
                    if (!property.Ignored && property.Readable && this.ShouldSerialize(property, value) && this.IsSpecified(property, value))
                    {
                        object obj = property.ValueProvider.GetValue(value);
                        this.WriteMemberInfoProperty(writer, obj, property, this.GetContractSafe(obj));
                    }
                }
                catch (Exception exception)
                {
                    if (!base.IsErrorHandled(value, contract, property.PropertyName, exception))
                    {
                        throw;
                    }
                    else
                    {
                        this.HandleError(writer, top);
                    }
                }
            }
            writer.WriteEndObject();
            this.SerializeStack.RemoveAt(this.SerializeStack.Count - 1);
            contract.InvokeOnSerialized(value, base.Serializer.Context);
        }
        private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
        {
            contract.InvokeOnSerializing(value, Serializer.Context);

            _serializeStack.Add(value);

            WriteObjectStart(writer, value, contract, member, collectionContract, containerProperty);

            int initialDepth = writer.Top;

            foreach (JsonProperty property in contract.Properties)
            {
                try
                {
                    object       memberValue;
                    JsonContract memberContract;

                    if (!CalculatePropertyValues(writer, value, contract, member, property, out memberContract, out memberValue))
                    {
                        continue;
                    }

                    writer.WritePropertyName(property.PropertyName);
                    SerializeValue(writer, memberValue, memberContract, property, contract, member);
                }
                catch (Exception ex)
                {
                    if (IsErrorHandled(value, contract, property.PropertyName, writer.ContainerPath, ex))
                    {
                        HandleError(writer, initialDepth);
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            writer.WriteEndObject();

            _serializeStack.RemoveAt(_serializeStack.Count - 1);

            contract.InvokeOnSerialized(value, Serializer.Context);
        }
示例#10
0
        private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract, JsonProperty member, JsonContract collectionValueContract)
        {
            contract.InvokeOnSerializing(value, base.Serializer.Context);
            SerializeStack.Add(value);
            writer.WriteStartObject();
            if (contract.IsReference ?? HasFlag(base.Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Objects))
            {
                writer.WritePropertyName("$id");
                writer.WriteValue(base.Serializer.ReferenceResolver.GetReference(this, value));
            }
            if (ShouldWriteType(TypeNameHandling.Objects, contract, member, collectionValueContract))
            {
                WriteTypeProperty(writer, contract.UnderlyingType);
            }
            int top = writer.Top;

            foreach (JsonProperty property in contract.Properties)
            {
                try
                {
                    if (!property.Ignored && property.Readable && ShouldSerialize(property, value) && IsSpecified(property, value))
                    {
                        object       value2       = property.ValueProvider.GetValue(value);
                        JsonContract contractSafe = GetContractSafe(value2);
                        WriteMemberInfoProperty(writer, value2, property, contractSafe);
                    }
                }
                catch (Exception ex)
                {
                    if (IsErrorHandled(value, contract, property.PropertyName, ex))
                    {
                        HandleError(writer, top);
                        continue;
                    }
                    throw;
                }
            }
            writer.WriteEndObject();
            SerializeStack.RemoveAt(SerializeStack.Count - 1);
            contract.InvokeOnSerialized(value, base.Serializer.Context);
        }
    private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract)
    {
      contract.InvokeOnSerializing(value);

#if !SILVERLIGHT && !PocketPC
      TypeConverter converter = TypeDescriptor.GetConverter(contract.UnderlyingType);

      // 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) || value is Type))
      {
        if (converter.CanConvertTo(typeof (string)))
        {
          writer.WriteValue(converter.ConvertToInvariantString(value));

          contract.InvokeOnSerialized(value);
          return;
        }
      }
#else
      if (value is Guid || value is Type || value is Uri)
      {
        writer.WriteValue(value.ToString());
        return;
      }
#endif

      SerializeStack.Add(value);
      writer.WriteStartObject();

      bool isReference = contract.IsReference ?? HasFlag(_serializer.PreserveReferencesHandling, PreserveReferencesHandling.Objects);
      if (isReference)
      {
        writer.WritePropertyName(JsonTypeReflector.IdPropertyName);
        writer.WriteValue(_serializer.ReferenceResolver.GetReference(value));
      }
      if (HasFlag(_serializer.TypeNameHandling, TypeNameHandling.Objects))
      {
        WriteTypeProperty(writer, contract.UnderlyingType);
      }

      foreach (JsonProperty property in contract.Properties)
      {
        if (!property.Ignored && property.Readable)
          WriteMemberInfoProperty(writer, value, property);
      }

      writer.WriteEndObject();
      SerializeStack.RemoveAt(SerializeStack.Count - 1);

      contract.InvokeOnSerialized(value);
    }
    private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
    {
      contract.InvokeOnSerializing(value, Serializer.Context);

      _serializeStack.Add(value);

      WriteObjectStart(writer, value, contract, member, collectionContract, containerProperty);

      int initialDepth = writer.Top;

      foreach (JsonProperty property in contract.Properties)
      {
        try
        {
          if (!property.Ignored && property.Readable && ShouldSerialize(property, value) && IsSpecified(property, value))
          {
            if (property.PropertyContract == null)
              property.PropertyContract = Serializer.ContractResolver.ResolveContract(property.PropertyType);

            object memberValue = property.ValueProvider.GetValue(value);
            JsonContract memberContract = (property.PropertyContract.UnderlyingType.IsSealed()) ? property.PropertyContract : GetContractSafe(memberValue);

            if (ShouldWriteProperty(memberValue, property))
            {
              string propertyName = property.PropertyName;

              if (ShouldWriteReference(memberValue, property, memberContract, contract, member))
              {
                writer.WritePropertyName(propertyName);
                WriteReference(writer, memberValue);
                continue;
              }

              if (!CheckForCircularReference(writer, memberValue, property, memberContract, contract, member))
                continue;

              if (memberValue == null)
              {
                Required resolvedRequired = property._required ?? contract.ItemRequired ?? Required.Default;
                if (resolvedRequired == Required.Always)
                  throw JsonSerializationException.Create(null, writer.ContainerPath, "Cannot write a null value for property '{0}'. Property requires a value.".FormatWith(CultureInfo.InvariantCulture, property.PropertyName), null);
              }

              writer.WritePropertyName(propertyName);
              SerializeValue(writer, memberValue, memberContract, property, contract, member);
            }
          }
        }
        catch (Exception ex)
        {
          if (IsErrorHandled(value, contract, property.PropertyName, writer.ContainerPath, ex))
            HandleError(writer, initialDepth);
          else
            throw;
        }
      }

      writer.WriteEndObject();

      _serializeStack.RemoveAt(_serializeStack.Count - 1);

      contract.InvokeOnSerialized(value, Serializer.Context);
    }
        private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract)
        {
            contract.InvokeOnSerializing(value);

              string s;
              if (TryConvertToString(value, contract.UnderlyingType, out s))
              {
            writer.WriteValue(s);

            #if !SILVERLIGHT && !PocketPC
            contract.InvokeOnSerialized(value);
            #endif
            return;
              }

              SerializeStack.Add(value);
              writer.WriteStartObject();

              bool isReference = contract.IsReference ?? HasFlag(Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Objects);
              if (isReference)
              {
            writer.WritePropertyName(JsonTypeReflector.IdPropertyName);
            writer.WriteValue(Serializer.ReferenceResolver.GetReference(value));
              }
              if (HasFlag(Serializer.TypeNameHandling, TypeNameHandling.Objects))
              {
            WriteTypeProperty(writer, contract.UnderlyingType);
              }

              int initialDepth = writer.Top;

              foreach (JsonProperty property in contract.Properties)
              {
            try
            {
              if (!property.Ignored && property.Readable)
            WriteMemberInfoProperty(writer, value, property);
            }
            catch (Exception ex)
            {
              if (IsErrorHandled(value, contract, property.Member.Name, ex))
            HandleError(writer, initialDepth);
              else
            throw;
            }
              }

              writer.WriteEndObject();
              SerializeStack.RemoveAt(SerializeStack.Count - 1);

              contract.InvokeOnSerialized(value);
        }
    private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract)
    {
      contract.InvokeOnSerializing(value, Serializer.Context);

      _serializeStack.Add(value);

      WriteObjectStart(writer, value, contract, member, collectionContract);

      int initialDepth = writer.Top;

      foreach (JsonProperty property in contract.Properties)
      {
        try
        {
          if (!property.Ignored && property.Readable && ShouldSerialize(property, value) && IsSpecified(property, value))
          {
            if (property.PropertyContract == null)
              property.PropertyContract = Serializer.ContractResolver.ResolveContract(property.PropertyType);

            object memberValue = property.ValueProvider.GetValue(value);
            JsonContract memberContract = (property.PropertyContract.UnderlyingType.IsSealed()) ? property.PropertyContract : GetContractSafe(memberValue);

            if (ShouldWriteProperty(memberValue, property))
            {
              WriteMemberInfoProperty(writer, memberValue, property, memberContract, contract);
            }
          }
        }
        catch (Exception ex)
        {
          if (IsErrorHandled(value, contract, property.PropertyName, writer.ContainerPath, ex))
            HandleError(writer, initialDepth);
          else
            throw;
        }
      }

      writer.WriteEndObject();

      _serializeStack.RemoveAt(_serializeStack.Count - 1);

      contract.InvokeOnSerialized(value, Serializer.Context);
    }
示例#15
0
 private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
 {
   contract.InvokeOnSerializing(value, this.Serializer.Context);
   this._serializeStack.Add(value);
   this.WriteObjectStart(writer, value, (JsonContract) contract, member, collectionContract, containerProperty);
   int top = writer.Top;
   foreach (JsonProperty jsonProperty in (Collection<JsonProperty>) contract.Properties)
   {
     try
     {
       JsonContract memberContract;
       object memberValue;
       if (this.CalculatePropertyValues(writer, value, (JsonContainerContract) contract, member, jsonProperty, out memberContract, out memberValue))
       {
         writer.WritePropertyName(jsonProperty.PropertyName);
         this.SerializeValue(writer, memberValue, memberContract, jsonProperty, (JsonContainerContract) contract, member);
       }
     }
     catch (Exception ex)
     {
       if (this.IsErrorHandled(value, (JsonContract) contract, (object) jsonProperty.PropertyName, writer.ContainerPath, ex))
         this.HandleError(writer, top);
       else
         throw;
     }
   }
   writer.WriteEndObject();
   this._serializeStack.RemoveAt(this._serializeStack.Count - 1);
   contract.InvokeOnSerialized(value, this.Serializer.Context);
 }
		private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract, JsonProperty member, JsonContract collectionValueContract)
		{
			contract.InvokeOnSerializing(value, Serializer.Context);

			SerializeStack.Add(value);
			writer.WriteStartObject();

			bool isReference = contract.IsReference ?? HasFlag(Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Objects);
			if (isReference)
			{
				writer.WritePropertyName(JsonTypeReflector.IdPropertyName);
				writer.WriteValue(Serializer.ReferenceResolver.GetReference(this, value));
			}
			if (ShouldWriteType(TypeNameHandling.Objects, contract, member, collectionValueContract))
			{
				WriteTypeProperty(writer, contract.UnderlyingType);
			}

			int initialDepth = writer.Top;

			foreach (JsonProperty property in contract.Properties)
			{
				try
				{
					if (!property.Ignored && property.Readable && ShouldSerialize(property, value) && IsSpecified(property, value))
					{
						object memberValue = property.ValueProvider.GetValue(value);
						JsonContract memberContract = GetContractSafe(memberValue);

						WriteMemberInfoProperty(writer, memberValue, property, memberContract);
					}
				}
				catch (Exception ex)
				{
					if (IsErrorHandled(value, contract, property.PropertyName, ex))
						HandleError(writer, initialDepth);
					else
						throw;
				}
			}

			writer.WriteEndObject();
			SerializeStack.RemoveAt(SerializeStack.Count - 1);

			contract.InvokeOnSerialized(value, Serializer.Context);
		}
    private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
    {
      contract.InvokeOnSerializing(value, Serializer.Context);

      _serializeStack.Add(value);

      WriteObjectStart(writer, value, contract, member, collectionContract, containerProperty);

      int initialDepth = writer.Top;

      foreach (JsonProperty property in contract.Properties)
      {
        try
        {
          object memberValue;
          JsonContract memberContract;

          if (!CalculatePropertyValues(writer, value, contract, member, property, out memberContract, out memberValue))
            continue;

          writer.WritePropertyName(property.PropertyName);
          SerializeValue(writer, memberValue, memberContract, property, contract, member);
        }
        catch (Exception ex)
        {
          if (IsErrorHandled(value, contract, property.PropertyName, writer.ContainerPath, ex))
            HandleError(writer, initialDepth);
          else
            throw;
        }
      }

      writer.WriteEndObject();

      _serializeStack.RemoveAt(_serializeStack.Count - 1);

      contract.InvokeOnSerialized(value, Serializer.Context);
    }
示例#18
0
        private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
        {
            contract.InvokeOnSerializing(value, Serializer.Context);

            _serializeStack.Add(value);

            WriteObjectStart(writer, value, contract, member, collectionContract, containerProperty);

            int initialDepth = writer.Top;

            foreach (JsonProperty property in contract.Properties)
            {
                try
                {
                    if (!property.Ignored && property.Readable && ShouldSerialize(property, value) && IsSpecified(property, value))
                    {
                        if (property.PropertyContract == null)
                        {
                            property.PropertyContract = Serializer.ContractResolver.ResolveContract(property.PropertyType);
                        }

                        object       memberValue    = property.ValueProvider.GetValue(value);
                        JsonContract memberContract = (property.PropertyContract.UnderlyingType.IsSealed()) ? property.PropertyContract : GetContractSafe(memberValue);

                        if (ShouldWriteProperty(memberValue, property))
                        {
                            string propertyName = property.PropertyName;

                            if (ShouldWriteReference(memberValue, property, memberContract, contract, member))
                            {
                                writer.WritePropertyName(propertyName);
                                WriteReference(writer, memberValue);
                                continue;
                            }

                            if (!CheckForCircularReference(writer, memberValue, property, memberContract, contract, member))
                            {
                                continue;
                            }

                            if (memberValue == null)
                            {
                                Required resolvedRequired = property._required ?? contract.ItemRequired ?? Required.Default;
                                if (resolvedRequired == Required.Always)
                                {
                                    throw JsonSerializationException.Create(null, writer.ContainerPath, "Cannot write a null value for property '{0}'. Property requires a value.".FormatWith(CultureInfo.InvariantCulture, property.PropertyName), null);
                                }
                            }

                            writer.WritePropertyName(propertyName);
                            SerializeValue(writer, memberValue, memberContract, property, contract, member);
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (IsErrorHandled(value, contract, property.PropertyName, writer.ContainerPath, ex))
                    {
                        HandleError(writer, initialDepth);
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            writer.WriteEndObject();

            _serializeStack.RemoveAt(_serializeStack.Count - 1);

            contract.InvokeOnSerialized(value, Serializer.Context);
        }