/// <summary> /// Writes the JSON representation of the object. /// </summary> /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param> /// <param name="value">The value.</param> /// <param name="serializer">The calling serializer.</param> public override void WriteJson(JsonWriter writer, object value, JsonSerializerWriter internalWriter) { EnsureReflectionObject(value.GetType()); DefaultContractResolver resolver = internalWriter.Serializer.ContractResolver as DefaultContractResolver; string keyName = (string)_reflectionObject.GetValue(value, KeyPropertyName); object keyValue = _reflectionObject.GetValue(value, ValuePropertyName); Type keyValueType = (keyValue != null) ? keyValue.GetType() : null; writer.WriteStartObject(); writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(KeyPropertyName) : KeyPropertyName); writer.WriteValue(keyName); writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(TypePropertyName) : TypePropertyName); writer.WriteValue((keyValueType != null) ? keyValueType.FullName : null); writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(ValuePropertyName) : ValuePropertyName); if (keyValueType != null) { string valueJson; if (JsonSerializerWriter.TryConvertToString(keyValue, keyValueType, out valueJson)) { writer.WriteValue(valueJson); } else { writer.WriteValue(keyValue); } } else { writer.WriteNull(); } writer.WriteEndObject(); }