public static String ToJson(this Object value) { var jsonBuilder = new StringBuilder(); var serializer = new JsonSerializer(); var writer = new JsonTextWriter(new StringWriter(jsonBuilder)); serializer.Serialize(writer, value); return jsonBuilder.ToString(); }
/// <summary> /// Creates a new <see cref="JsonSerializer"/> instance using the specified <see cref="JsonSerializerSettings"/> objects. /// </summary> /// <param name="settings">The settings.</param> /// <returns></returns> public static JsonSerializer Create(JsonSerializerSettings settings) { JsonSerializer jsonSerializer = new JsonSerializer(); if (settings != null) { if (!CollectionUtils.IsNullOrEmpty(settings.Converters)) jsonSerializer.Converters.AddRange(settings.Converters); jsonSerializer.ReferenceLoopHandling = settings.ReferenceLoopHandling; jsonSerializer.MissingMemberHandling = settings.MissingMemberHandling; jsonSerializer.ObjectCreationHandling = settings.ObjectCreationHandling; jsonSerializer.NullValueHandling = settings.NullValueHandling; jsonSerializer.DefaultValueHandling = settings.DefaultValueHandling; jsonSerializer.MappingResolver = settings.MappingResolver; } return jsonSerializer; }