-Added default serializer settings
This commit is contained in:
@@ -50,6 +50,166 @@ namespace Newtonsoft.Json.Tests
|
||||
[TestFixture]
|
||||
public class JsonConvertTest : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void DefaultSettings()
|
||||
{
|
||||
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
|
||||
{
|
||||
Formatting = Formatting.Indented
|
||||
};
|
||||
|
||||
string json = JsonConvert.SerializeObject(new { test = new[] { 1, 2, 3 } });
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""test"": [
|
||||
1,
|
||||
2,
|
||||
3
|
||||
]
|
||||
}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DefaultSettings_Override()
|
||||
{
|
||||
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
|
||||
{
|
||||
Formatting = Formatting.Indented
|
||||
};
|
||||
|
||||
string json = JsonConvert.SerializeObject(new { test = new[] { 1, 2, 3 } }, new JsonSerializerSettings
|
||||
{
|
||||
Formatting = Formatting.None
|
||||
});
|
||||
|
||||
Assert.AreEqual(@"{""test"":[1,2,3]}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DefaultSettings_Override_JsonConverterOrder()
|
||||
{
|
||||
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
|
||||
{
|
||||
Formatting = Formatting.Indented,
|
||||
Converters = { new IsoDateTimeConverter { DateTimeFormat = "yyyy" } }
|
||||
};
|
||||
|
||||
string json = JsonConvert.SerializeObject(new[] { new DateTime(2000, 12, 12, 4, 2, 4, DateTimeKind.Utc) }, new JsonSerializerSettings
|
||||
{
|
||||
Formatting = Formatting.None,
|
||||
Converters =
|
||||
{
|
||||
// should take precedence
|
||||
new JavaScriptDateTimeConverter(),
|
||||
new IsoDateTimeConverter { DateTimeFormat = "dd" }
|
||||
}
|
||||
});
|
||||
|
||||
Assert.AreEqual(@"[new Date(976593724000)]", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DefaultSettings_Create()
|
||||
{
|
||||
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
|
||||
{
|
||||
Formatting = Formatting.Indented
|
||||
};
|
||||
|
||||
IList<int> l = new List<int> { 1, 2, 3 };
|
||||
|
||||
StringWriter sw = new StringWriter();
|
||||
JsonSerializer serializer = JsonSerializer.CreateDefault();
|
||||
serializer.Serialize(sw, l);
|
||||
|
||||
Assert.AreEqual(@"[
|
||||
1,
|
||||
2,
|
||||
3
|
||||
]", sw.ToString());
|
||||
|
||||
sw = new StringWriter();
|
||||
serializer.Formatting = Formatting.None;
|
||||
serializer.Serialize(sw, l);
|
||||
|
||||
Assert.AreEqual(@"[1,2,3]", sw.ToString());
|
||||
|
||||
sw = new StringWriter();
|
||||
serializer = new JsonSerializer();
|
||||
serializer.Serialize(sw, l);
|
||||
|
||||
Assert.AreEqual(@"[1,2,3]", sw.ToString());
|
||||
|
||||
sw = new StringWriter();
|
||||
serializer = JsonSerializer.Create();
|
||||
serializer.Serialize(sw, l);
|
||||
|
||||
Assert.AreEqual(@"[1,2,3]", sw.ToString());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DefaultSettings_CreateWithSettings()
|
||||
{
|
||||
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
|
||||
{
|
||||
Formatting = Formatting.Indented
|
||||
};
|
||||
|
||||
IList<int> l = new List<int> { 1, 2, 3 };
|
||||
|
||||
StringWriter sw = new StringWriter();
|
||||
JsonSerializer serializer = JsonSerializer.CreateDefault(new JsonSerializerSettings
|
||||
{
|
||||
Converters = { new IntConverter() }
|
||||
});
|
||||
serializer.Serialize(sw, l);
|
||||
|
||||
Assert.AreEqual(@"[
|
||||
2,
|
||||
4,
|
||||
6
|
||||
]", sw.ToString());
|
||||
|
||||
sw = new StringWriter();
|
||||
serializer.Converters.Clear();
|
||||
serializer.Serialize(sw, l);
|
||||
|
||||
Assert.AreEqual(@"[
|
||||
1,
|
||||
2,
|
||||
3
|
||||
]", sw.ToString());
|
||||
|
||||
sw = new StringWriter();
|
||||
serializer = JsonSerializer.Create(new JsonSerializerSettings { Formatting = Formatting.Indented });
|
||||
serializer.Serialize(sw, l);
|
||||
|
||||
Assert.AreEqual(@"[
|
||||
1,
|
||||
2,
|
||||
3
|
||||
]", sw.ToString());
|
||||
}
|
||||
|
||||
public class IntConverter : JsonConverter
|
||||
{
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
int i = (int)value;
|
||||
writer.WriteValue(i * 2);
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return objectType == typeof(int);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeObject_EmptyString()
|
||||
{
|
||||
|
||||
@@ -75,4 +75,4 @@ using System.Security;
|
||||
// You can specify all the values or you can default the Revision and Build Numbers
|
||||
// by using the '*' as shown below:
|
||||
[assembly: AssemblyVersion("4.5.0.0")]
|
||||
[assembly: AssemblyFileVersion("5.0.4.16025")]
|
||||
[assembly: AssemblyFileVersion("5.0.4.16028")]
|
||||
|
||||
@@ -41,6 +41,7 @@ using NUnit.Framework;
|
||||
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
|
||||
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
|
||||
using TestMethod = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
|
||||
using Setup = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitialize;
|
||||
#endif
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using System.Collections;
|
||||
@@ -117,13 +118,14 @@ namespace Newtonsoft.Json.Tests
|
||||
return bytes;
|
||||
}
|
||||
|
||||
#if !NETFX_CORE
|
||||
[SetUp]
|
||||
protected void TestSetup()
|
||||
{
|
||||
//CultureInfo turkey = CultureInfo.CreateSpecificCulture("tr");
|
||||
//Thread.CurrentThread.CurrentCulture = turkey;
|
||||
//Thread.CurrentThread.CurrentUICulture = turkey;
|
||||
|
||||
JsonConvert.DefaultSettings = null;
|
||||
}
|
||||
|
||||
protected void WriteEscapedJson(string json)
|
||||
@@ -135,7 +137,6 @@ namespace Newtonsoft.Json.Tests
|
||||
{
|
||||
return @"@""" + json.Replace(@"""", @"""""") + @"""";
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if NETFX_CORE
|
||||
|
||||
@@ -36,6 +36,7 @@ using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using System.Xml;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using System.Text;
|
||||
#if !NET20 && (!SILVERLIGHT || WINDOWS_PHONE) && !PORTABLE40
|
||||
using System.Xml.Linq;
|
||||
@@ -51,6 +52,15 @@ namespace Newtonsoft.Json
|
||||
/// </example>
|
||||
public static class JsonConvert
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets a function that creates default <see cref="JsonSerializerSettings"/>.
|
||||
/// Default settings are automatically used by serialization methods on <see cref="JsonConvert"/>,
|
||||
/// and <see cref="JToken.ToObject{T}()"/> and <see cref="JToken.FromObject(object)"/> on <see cref="JToken"/>.
|
||||
/// To serialize without using any default settings create a <see cref="JsonSerializer"/> with
|
||||
/// <see cref="JsonSerializer.Create()"/>.
|
||||
/// </summary>
|
||||
public static Func<JsonSerializerSettings> DefaultSettings { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Represents JavaScript's boolean value true as a string. This field is read-only.
|
||||
/// </summary>
|
||||
@@ -595,7 +605,7 @@ namespace Newtonsoft.Json
|
||||
/// </returns>
|
||||
public static string SerializeObject(object value, Type type, Formatting formatting, JsonSerializerSettings settings)
|
||||
{
|
||||
JsonSerializer jsonSerializer = JsonSerializer.Create(settings);
|
||||
JsonSerializer jsonSerializer = JsonSerializer.CreateDefault(settings);
|
||||
|
||||
StringBuilder sb = new StringBuilder(256);
|
||||
StringWriter sw = new StringWriter(sb, CultureInfo.InvariantCulture);
|
||||
@@ -793,7 +803,7 @@ namespace Newtonsoft.Json
|
||||
ValidationUtils.ArgumentNotNull(value, "value");
|
||||
|
||||
StringReader sr = new StringReader(value);
|
||||
JsonSerializer jsonSerializer = JsonSerializer.Create(settings);
|
||||
JsonSerializer jsonSerializer = JsonSerializer.CreateDefault(settings);
|
||||
|
||||
// by default DeserializeObject should check for additional content
|
||||
if (!jsonSerializer.IsCheckAdditionalContentSet())
|
||||
@@ -886,7 +896,7 @@ namespace Newtonsoft.Json
|
||||
public static void PopulateObject(string value, object target, JsonSerializerSettings settings)
|
||||
{
|
||||
StringReader sr = new StringReader(value);
|
||||
JsonSerializer jsonSerializer = JsonSerializer.Create(settings);
|
||||
JsonSerializer jsonSerializer = JsonSerializer.CreateDefault(settings);
|
||||
|
||||
using (JsonReader jsonReader = new JsonTextReader(sr))
|
||||
{
|
||||
|
||||
@@ -442,62 +442,147 @@ namespace Newtonsoft.Json
|
||||
_contractResolver = DefaultContractResolver.Instance;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="JsonSerializer"/> instance.
|
||||
/// The <see cref="JsonSerializer"/> will not use default settings.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// A new <see cref="JsonSerializer"/> instance.
|
||||
/// The <see cref="JsonSerializer"/> will not use default settings.
|
||||
/// </returns>
|
||||
public static JsonSerializer Create()
|
||||
{
|
||||
return new JsonSerializer();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="JsonSerializer"/> instance using the specified <see cref="JsonSerializerSettings"/>.
|
||||
/// The <see cref="JsonSerializer"/> will not use default settings.
|
||||
/// </summary>
|
||||
/// <param name="settings">The settings to be applied to the <see cref="JsonSerializer"/>.</param>
|
||||
/// <returns>A new <see cref="JsonSerializer"/> instance using the specified <see cref="JsonSerializerSettings"/>.</returns>
|
||||
/// <returns>
|
||||
/// A new <see cref="JsonSerializer"/> instance using the specified <see cref="JsonSerializerSettings"/>.
|
||||
/// The <see cref="JsonSerializer"/> will not use default settings.
|
||||
/// </returns>
|
||||
public static JsonSerializer Create(JsonSerializerSettings settings)
|
||||
{
|
||||
JsonSerializer jsonSerializer = new JsonSerializer();
|
||||
JsonSerializer serializer = Create();
|
||||
|
||||
if (settings != null)
|
||||
ApplySerializerSettings(serializer, settings);
|
||||
|
||||
return serializer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="JsonSerializer"/> instance.
|
||||
/// The <see cref="JsonSerializer"/> will use default settings.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// A new <see cref="JsonSerializer"/> instance.
|
||||
/// The <see cref="JsonSerializer"/> will use default settings.
|
||||
/// </returns>
|
||||
public static JsonSerializer CreateDefault()
|
||||
{
|
||||
Func<JsonSerializerSettings> defaultSettingsCreator = JsonConvert.DefaultSettings;
|
||||
JsonSerializerSettings defaultSettings = (defaultSettingsCreator != null) ? defaultSettingsCreator() : null;
|
||||
|
||||
return Create(defaultSettings);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="JsonSerializer"/> instance using the specified <see cref="JsonSerializerSettings"/>.
|
||||
/// The <see cref="JsonSerializer"/> will use default settings.
|
||||
/// </summary>
|
||||
/// <param name="settings">The settings to be applied to the <see cref="JsonSerializer"/>.</param>
|
||||
/// <returns>
|
||||
/// A new <see cref="JsonSerializer"/> instance using the specified <see cref="JsonSerializerSettings"/>.
|
||||
/// The <see cref="JsonSerializer"/> will use default settings.
|
||||
/// </returns>
|
||||
public static JsonSerializer CreateDefault(JsonSerializerSettings settings)
|
||||
{
|
||||
JsonSerializer serializer = CreateDefault();
|
||||
if (settings != null)
|
||||
ApplySerializerSettings(serializer, settings);
|
||||
|
||||
return serializer;
|
||||
}
|
||||
|
||||
private static void ApplySerializerSettings(JsonSerializer serializer, JsonSerializerSettings settings)
|
||||
{
|
||||
if (!CollectionUtils.IsNullOrEmpty(settings.Converters))
|
||||
{
|
||||
if (!CollectionUtils.IsNullOrEmpty(settings.Converters))
|
||||
jsonSerializer.Converters.AddRange(settings.Converters);
|
||||
|
||||
// serializer specific
|
||||
jsonSerializer.TypeNameHandling = settings.TypeNameHandling;
|
||||
jsonSerializer.TypeNameAssemblyFormat = settings.TypeNameAssemblyFormat;
|
||||
jsonSerializer.PreserveReferencesHandling = settings.PreserveReferencesHandling;
|
||||
jsonSerializer.ReferenceLoopHandling = settings.ReferenceLoopHandling;
|
||||
jsonSerializer.MissingMemberHandling = settings.MissingMemberHandling;
|
||||
jsonSerializer.ObjectCreationHandling = settings.ObjectCreationHandling;
|
||||
jsonSerializer.NullValueHandling = settings.NullValueHandling;
|
||||
jsonSerializer.DefaultValueHandling = settings.DefaultValueHandling;
|
||||
jsonSerializer.ConstructorHandling = settings.ConstructorHandling;
|
||||
jsonSerializer.Context = settings.Context;
|
||||
jsonSerializer._checkAdditionalContent = settings._checkAdditionalContent;
|
||||
|
||||
// reader/writer specific
|
||||
// unset values won't override reader/writer set values
|
||||
jsonSerializer._formatting = settings._formatting;
|
||||
jsonSerializer._dateFormatHandling = settings._dateFormatHandling;
|
||||
jsonSerializer._dateTimeZoneHandling = settings._dateTimeZoneHandling;
|
||||
jsonSerializer._dateParseHandling = settings._dateParseHandling;
|
||||
jsonSerializer._dateFormatString = settings._dateFormatString;
|
||||
jsonSerializer._dateFormatStringSet = settings._dateFormatStringSet;
|
||||
jsonSerializer._floatFormatHandling = settings._floatFormatHandling;
|
||||
jsonSerializer._floatParseHandling = settings._floatParseHandling;
|
||||
jsonSerializer._stringEscapeHandling = settings._stringEscapeHandling;
|
||||
jsonSerializer._culture = settings._culture;
|
||||
jsonSerializer._maxDepth = settings._maxDepth;
|
||||
jsonSerializer._maxDepthSet = settings._maxDepthSet;
|
||||
|
||||
if (settings.Error != null)
|
||||
jsonSerializer.Error += settings.Error;
|
||||
|
||||
if (settings.ContractResolver != null)
|
||||
jsonSerializer.ContractResolver = settings.ContractResolver;
|
||||
if (settings.ReferenceResolver != null)
|
||||
jsonSerializer.ReferenceResolver = settings.ReferenceResolver;
|
||||
if (settings.TraceWriter != null)
|
||||
jsonSerializer.TraceWriter = settings.TraceWriter;
|
||||
if (settings.Binder != null)
|
||||
jsonSerializer.Binder = settings.Binder;
|
||||
// insert settings converters at the beginning so they take precedence
|
||||
for (int i = 0; i < settings.Converters.Count; i++)
|
||||
{
|
||||
serializer.Converters.Insert(i, settings.Converters[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return jsonSerializer;
|
||||
// serializer specific
|
||||
if (settings._typeNameHandling != null)
|
||||
serializer.TypeNameHandling = settings.TypeNameHandling;
|
||||
if (settings._typeNameAssemblyFormat != null)
|
||||
serializer.TypeNameAssemblyFormat = settings.TypeNameAssemblyFormat;
|
||||
if (settings._preserveReferencesHandling != null)
|
||||
serializer.PreserveReferencesHandling = settings.PreserveReferencesHandling;
|
||||
if (settings._referenceLoopHandling != null)
|
||||
serializer.ReferenceLoopHandling = settings.ReferenceLoopHandling;
|
||||
if (settings._missingMemberHandling != null)
|
||||
serializer.MissingMemberHandling = settings.MissingMemberHandling;
|
||||
if (settings._objectCreationHandling != null)
|
||||
serializer.ObjectCreationHandling = settings.ObjectCreationHandling;
|
||||
if (settings._nullValueHandling != null)
|
||||
serializer.NullValueHandling = settings.NullValueHandling;
|
||||
if (settings._defaultValueHandling != null)
|
||||
serializer.DefaultValueHandling = settings.DefaultValueHandling;
|
||||
if (settings._constructorHandling != null)
|
||||
serializer.ConstructorHandling = settings.ConstructorHandling;
|
||||
if (settings._context != null)
|
||||
serializer.Context = settings.Context;
|
||||
if (settings._checkAdditionalContent != null)
|
||||
serializer._checkAdditionalContent = settings._checkAdditionalContent;
|
||||
|
||||
if (settings.Error != null)
|
||||
serializer.Error += settings.Error;
|
||||
|
||||
if (settings.ContractResolver != null)
|
||||
serializer.ContractResolver = settings.ContractResolver;
|
||||
if (settings.ReferenceResolver != null)
|
||||
serializer.ReferenceResolver = settings.ReferenceResolver;
|
||||
if (settings.TraceWriter != null)
|
||||
serializer.TraceWriter = settings.TraceWriter;
|
||||
if (settings.Binder != null)
|
||||
serializer.Binder = settings.Binder;
|
||||
|
||||
// reader/writer specific
|
||||
// unset values won't override reader/writer set values
|
||||
if (settings._formatting != null)
|
||||
serializer._formatting = settings._formatting;
|
||||
if (settings._dateFormatHandling != null)
|
||||
serializer._dateFormatHandling = settings._dateFormatHandling;
|
||||
if (settings._dateTimeZoneHandling != null)
|
||||
serializer._dateTimeZoneHandling = settings._dateTimeZoneHandling;
|
||||
if (settings._dateParseHandling != null)
|
||||
serializer._dateParseHandling = settings._dateParseHandling;
|
||||
if (settings._dateFormatStringSet)
|
||||
{
|
||||
serializer._dateFormatString = settings._dateFormatString;
|
||||
serializer._dateFormatStringSet = settings._dateFormatStringSet;
|
||||
}
|
||||
if (settings._floatFormatHandling != null)
|
||||
serializer._floatFormatHandling = settings._floatFormatHandling;
|
||||
if (settings._floatParseHandling != null)
|
||||
serializer._floatParseHandling = settings._floatParseHandling;
|
||||
if (settings._stringEscapeHandling != null)
|
||||
serializer._stringEscapeHandling = settings._stringEscapeHandling;
|
||||
if (settings._culture != null)
|
||||
serializer._culture = settings._culture;
|
||||
if (settings._maxDepthSet)
|
||||
{
|
||||
serializer._maxDepth = settings._maxDepth;
|
||||
serializer._maxDepthSet = settings._maxDepthSet;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -55,6 +55,7 @@ namespace Newtonsoft.Json
|
||||
internal const FloatParseHandling DefaultFloatParseHandling = FloatParseHandling.Double;
|
||||
internal const FloatFormatHandling DefaultFloatFormatHandling = FloatFormatHandling.String;
|
||||
internal const StringEscapeHandling DefaultStringEscapeHandling = StringEscapeHandling.Default;
|
||||
internal const FormatterAssemblyStyle DefaultFormatterAssemblyStyle = FormatterAssemblyStyle.Simple;
|
||||
internal static readonly CultureInfo DefaultCulture;
|
||||
internal const bool DefaultCheckAdditionalContent = false;
|
||||
internal const string DefaultDateFormatString = @"yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK";
|
||||
@@ -72,36 +73,66 @@ namespace Newtonsoft.Json
|
||||
internal bool _maxDepthSet;
|
||||
internal string _dateFormatString;
|
||||
internal bool _dateFormatStringSet;
|
||||
internal FormatterAssemblyStyle? _typeNameAssemblyFormat;
|
||||
internal DefaultValueHandling? _defaultValueHandling;
|
||||
internal PreserveReferencesHandling? _preserveReferencesHandling;
|
||||
internal NullValueHandling? _nullValueHandling;
|
||||
internal ObjectCreationHandling? _objectCreationHandling;
|
||||
internal MissingMemberHandling? _missingMemberHandling;
|
||||
internal ReferenceLoopHandling? _referenceLoopHandling;
|
||||
internal StreamingContext? _context;
|
||||
internal ConstructorHandling? _constructorHandling;
|
||||
internal TypeNameHandling? _typeNameHandling;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets how reference loops (e.g. a class referencing itself) is handled.
|
||||
/// </summary>
|
||||
/// <value>Reference loop handling.</value>
|
||||
public ReferenceLoopHandling ReferenceLoopHandling { get; set; }
|
||||
public ReferenceLoopHandling ReferenceLoopHandling
|
||||
{
|
||||
get { return _referenceLoopHandling ?? DefaultReferenceLoopHandling; }
|
||||
set { _referenceLoopHandling = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets how missing members (e.g. JSON contains a property that isn't a member on the object) are handled during deserialization.
|
||||
/// </summary>
|
||||
/// <value>Missing member handling.</value>
|
||||
public MissingMemberHandling MissingMemberHandling { get; set; }
|
||||
public MissingMemberHandling MissingMemberHandling
|
||||
{
|
||||
get { return _missingMemberHandling ?? DefaultMissingMemberHandling; }
|
||||
set { _missingMemberHandling = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets how objects are created during deserialization.
|
||||
/// </summary>
|
||||
/// <value>The object creation handling.</value>
|
||||
public ObjectCreationHandling ObjectCreationHandling { get; set; }
|
||||
public ObjectCreationHandling ObjectCreationHandling
|
||||
{
|
||||
get { return _objectCreationHandling ?? DefaultObjectCreationHandling; }
|
||||
set { _objectCreationHandling = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets how null values are handled during serialization and deserialization.
|
||||
/// </summary>
|
||||
/// <value>Null value handling.</value>
|
||||
public NullValueHandling NullValueHandling { get; set; }
|
||||
public NullValueHandling NullValueHandling
|
||||
{
|
||||
get { return _nullValueHandling ?? DefaultNullValueHandling; }
|
||||
set { _nullValueHandling = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets how null default are handled during serialization and deserialization.
|
||||
/// </summary>
|
||||
/// <value>The default value handling.</value>
|
||||
public DefaultValueHandling DefaultValueHandling { get; set; }
|
||||
public DefaultValueHandling DefaultValueHandling
|
||||
{
|
||||
get { return _defaultValueHandling ?? DefaultDefaultValueHandling; }
|
||||
set { _defaultValueHandling = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a collection <see cref="JsonConverter"/> that will be used during serialization.
|
||||
@@ -113,25 +144,41 @@ namespace Newtonsoft.Json
|
||||
/// Gets or sets how object references are preserved by the serializer.
|
||||
/// </summary>
|
||||
/// <value>The preserve references handling.</value>
|
||||
public PreserveReferencesHandling PreserveReferencesHandling { get; set; }
|
||||
public PreserveReferencesHandling PreserveReferencesHandling
|
||||
{
|
||||
get { return _preserveReferencesHandling ?? DefaultPreserveReferencesHandling; }
|
||||
set { _preserveReferencesHandling = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets how type name writing and reading is handled by the serializer.
|
||||
/// </summary>
|
||||
/// <value>The type name handling.</value>
|
||||
public TypeNameHandling TypeNameHandling { get; set; }
|
||||
public TypeNameHandling TypeNameHandling
|
||||
{
|
||||
get { return _typeNameHandling ?? DefaultTypeNameHandling; }
|
||||
set { _typeNameHandling = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets how a type name assembly is written and resolved by the serializer.
|
||||
/// </summary>
|
||||
/// <value>The type name assembly format.</value>
|
||||
public FormatterAssemblyStyle TypeNameAssemblyFormat { get; set; }
|
||||
public FormatterAssemblyStyle TypeNameAssemblyFormat
|
||||
{
|
||||
get { return _typeNameAssemblyFormat ?? DefaultFormatterAssemblyStyle; }
|
||||
set { _typeNameAssemblyFormat = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets how constructors are used during deserialization.
|
||||
/// </summary>
|
||||
/// <value>The constructor handling.</value>
|
||||
public ConstructorHandling ConstructorHandling { get; set; }
|
||||
public ConstructorHandling ConstructorHandling
|
||||
{
|
||||
get { return _constructorHandling ?? DefaultConstructorHandling; }
|
||||
set { _constructorHandling = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the contract resolver used by the serializer when
|
||||
@@ -168,7 +215,11 @@ namespace Newtonsoft.Json
|
||||
/// Gets or sets the <see cref="StreamingContext"/> used by the serializer when invoking serialization callback methods.
|
||||
/// </summary>
|
||||
/// <value>The context.</value>
|
||||
public StreamingContext Context { get; set; }
|
||||
public StreamingContext Context
|
||||
{
|
||||
get { return _context ?? DefaultContext; }
|
||||
set { _context = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get or set how <see cref="DateTime"/> and <see cref="DateTimeOffset"/> values are formatting when writing JSON text.
|
||||
@@ -296,16 +347,6 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
public JsonSerializerSettings()
|
||||
{
|
||||
ReferenceLoopHandling = DefaultReferenceLoopHandling;
|
||||
MissingMemberHandling = DefaultMissingMemberHandling;
|
||||
ObjectCreationHandling = DefaultObjectCreationHandling;
|
||||
NullValueHandling = DefaultNullValueHandling;
|
||||
DefaultValueHandling = DefaultDefaultValueHandling;
|
||||
PreserveReferencesHandling = DefaultPreserveReferencesHandling;
|
||||
TypeNameHandling = DefaultTypeNameHandling;
|
||||
TypeNameAssemblyFormat = DefaultTypeNameAssemblyFormat;
|
||||
Context = DefaultContext;
|
||||
|
||||
Converters = new List<JsonConverter>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,7 +160,7 @@ namespace Newtonsoft.Json.Linq
|
||||
/// <returns>A <see cref="JArray"/> with the values of the specified object</returns>
|
||||
public static new JArray FromObject(object o)
|
||||
{
|
||||
return FromObject(o, new JsonSerializer());
|
||||
return FromObject(o, JsonSerializer.CreateDefault());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -342,7 +342,7 @@ namespace Newtonsoft.Json.Linq
|
||||
/// <returns>A <see cref="JObject"/> with the values of the specified object</returns>
|
||||
public static new JObject FromObject(object o)
|
||||
{
|
||||
return FromObject(o, new JsonSerializer());
|
||||
return FromObject(o, JsonSerializer.CreateDefault());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1572,7 +1572,7 @@ namespace Newtonsoft.Json.Linq
|
||||
/// <returns>A <see cref="JToken"/> with the value of the specified object</returns>
|
||||
public static JToken FromObject(object o)
|
||||
{
|
||||
return FromObjectInternal(o, new JsonSerializer());
|
||||
return FromObjectInternal(o, JsonSerializer.CreateDefault());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1602,11 +1602,6 @@ namespace Newtonsoft.Json.Linq
|
||||
/// <param name="objectType">The object type that the token will be deserialized to.</param>
|
||||
/// <returns>The new object created from the JSON value.</returns>
|
||||
public object ToObject(Type objectType)
|
||||
{
|
||||
return ToObject(objectType, false);
|
||||
}
|
||||
|
||||
private object ToObject(Type objectType, bool isNullable)
|
||||
{
|
||||
PrimitiveTypeCode typeCode = ConvertUtils.GetTypeCode(objectType);
|
||||
|
||||
@@ -1694,7 +1689,7 @@ namespace Newtonsoft.Json.Linq
|
||||
#endif
|
||||
}
|
||||
|
||||
return ToObject(objectType, new JsonSerializer());
|
||||
return ToObject(objectType, JsonSerializer.CreateDefault());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -617,7 +617,7 @@ namespace Newtonsoft.Json.Linq
|
||||
JsonConverter matchingConverter = JsonSerializer.GetMatchingConverter(converters, _value.GetType());
|
||||
if (matchingConverter != null)
|
||||
{
|
||||
matchingConverter.WriteJson(writer, _value, new JsonSerializer());
|
||||
matchingConverter.WriteJson(writer, _value, JsonSerializer.CreateDefault());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,6 +92,6 @@ using System.Security;
|
||||
// You can specify all the values or you can default the Revision and Build Numbers
|
||||
// by using the '*' as shown below:
|
||||
[assembly: AssemblyVersion("4.5.0.0")]
|
||||
[assembly: AssemblyFileVersion("5.0.4.16025")]
|
||||
[assembly: AssemblyFileVersion("5.0.4.16028")]
|
||||
|
||||
[assembly: CLSCompliant(true)]
|
||||
|
||||
Reference in New Issue
Block a user