private void SerializeObject(JsonWriter writer, object value) { Type objectType = value.GetType(); #if !SILVERLIGHT && !PocketPC 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) || value is Type)) { if (converter.CanConvertTo(typeof(string))) { writer.WriteValue(converter.ConvertToInvariantString(value)); return; } } #else if (value is Guid || value is Type) { writer.WriteValue(value.ToString()); return; } #endif writer.SerializeStack.Add(value); writer.WriteStartObject(); JsonMemberMappingCollection memberMappings = GetMemberMappings(objectType); foreach (JsonMemberMapping memberMapping in memberMappings) { if (!memberMapping.Ignored && memberMapping.Readable) WriteMemberInfoProperty(writer, value, memberMapping); } writer.WriteEndObject(); writer.SerializeStack.RemoveAt(writer.SerializeStack.Count - 1); }
private void SerializeValue(JsonWriter writer, object value, JsonConverter memberConverter) { JsonConverter converter; if (value == null) { writer.WriteNull(); } else if (memberConverter != null) { memberConverter.WriteJson(writer, value); } else if (HasClassConverter(value.GetType(), out converter)) { converter.WriteJson(writer, value); } else if (HasMatchingConverter(value.GetType(), out converter)) { converter.WriteJson(writer, value); } else if (JsonConvert.IsJsonPrimitive(value)) { writer.WriteValue(value); } 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 IEnumerable) { SerializeEnumerable(writer, (IEnumerable)value); } else if (value is JsonRaw) { writer.WriteRawValue(((JsonRaw)value).Content); } else { SerializeObject(writer, value); } }