private void SerializeValue(JsonWriter writer, object value, JsonContract valueContract, JsonProperty member, JsonContract collectionValueContract)
        {
            JsonConverter converter = (member != null) ? member.Converter : null;

            if (value == null)
            {
                writer.WriteNull();
                return;
            }

            if ((converter != null ||
                 ((converter = valueContract.Converter) != null) ||
                 ((converter = this.Serializer.GetMatchingConverter(valueContract.UnderlyingType)) != null) ||
                 ((converter = valueContract.InternalConverter) != null)) &&
                converter.CanWrite)
            {
                this.SerializeConvertable(writer, converter, value, valueContract);
                return;
            }

            switch (valueContract.ContractType)
            {
            case JsonContractType.Object:
                this.SerializeObject(writer, value, (JsonObjectContract)valueContract, member, collectionValueContract);
                break;

            case JsonContractType.Array:
                JsonArrayContract arrayContract = (JsonArrayContract)valueContract;
                this.SerializeList(writer, arrayContract.CreateWrapper(value), arrayContract, member, collectionValueContract);
                break;

            case JsonContractType.Primitive:
                this.SerializePrimitive(writer, value, (JsonPrimitiveContract)valueContract, member, collectionValueContract);
                break;

            case JsonContractType.String:
                this.SerializeString(writer, value, (JsonStringContract)valueContract);
                break;

            case JsonContractType.Dictionary:
                JsonDictionaryContract dictionaryContract = (JsonDictionaryContract)valueContract;
                this.SerializeDictionary(writer, dictionaryContract.CreateWrapper(value), dictionaryContract, member, collectionValueContract);
                break;

                    #if !(NET35 || NET20 || WINDOWS_PHONE)
            case JsonContractType.Dynamic:
                this.SerializeDynamic(writer, (IDynamicMetaObjectProvider)value, (JsonDynamicContract)valueContract);
                break;
                    #endif
                    #if !SILVERLIGHT && !PocketPC
            case JsonContractType.Serializable:
                this.SerializeISerializable(writer, (ISerializable)value, (JsonISerializableContract)valueContract, member, collectionValueContract);
                break;
                    #endif
            case JsonContractType.Linq:
                ((JToken)value).WriteTo(writer, (this.Serializer.Converters != null) ? this.Serializer.Converters.ToArray() : null);
                break;
            }
        }
        private object CreateAndPopulateList(JsonReader reader, string reference, JsonArrayContract contract)
        {
            return CollectionUtils.CreateAndPopulateList(contract.CreatedType, (l, isTemporaryListReference) =>
            {
              if (reference != null && isTemporaryListReference)
            throw CreateSerializationException(reader, "Cannot preserve reference to array or readonly list: {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType));

            #if !PocketPC
              if (contract.OnSerializing != null && isTemporaryListReference)
            throw CreateSerializationException(reader, "Cannot call OnSerializing on an array or readonly list: {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType));
            #endif
              if (contract.OnError != null && isTemporaryListReference)
            throw CreateSerializationException(reader, "Cannot call OnError on an array or readonly list: {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType));

              PopulateList(contract.CreateWrapper(l), reader, reference, contract);
            });
        }