-Added ITraceWriter

-Fixed .NET 2.0 build using DateTimeOffset
This commit is contained in:
James Newton-King
2012-11-09 17:10:32 +13:00
parent 12b60f2fca
commit 89883dc57b
36 changed files with 870 additions and 117 deletions
@@ -30,6 +30,7 @@ using System.ComponentModel;
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
using System.Dynamic;
#endif
using System.Diagnostics;
using System.Globalization;
using System.Security;
using Newtonsoft.Json.Linq;
@@ -229,21 +230,25 @@ namespace Newtonsoft.Json.Serialization
if (_serializeStack.IndexOf(value) != -1)
{
string message = "Self referencing loop detected";
if (property != null)
message += " for property '{0}'".FormatWith(CultureInfo.InvariantCulture, property.PropertyName);
message += " with type '{0}'.".FormatWith(CultureInfo.InvariantCulture, value.GetType());
switch (referenceLoopHandling.GetValueOrDefault(Serializer.ReferenceLoopHandling))
{
case ReferenceLoopHandling.Error:
string message = "Self referencing loop detected";
if (property != null)
message += " for property '{0}'".FormatWith(CultureInfo.InvariantCulture, property.PropertyName);
message += " with type '{0}'.".FormatWith(CultureInfo.InvariantCulture, value.GetType());
throw JsonSerializationException.Create(null, writer.ContainerPath, message, null);
case ReferenceLoopHandling.Ignore:
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Verbose)
TraceWriter.Trace(TraceLevel.Verbose, JsonPosition.FormatMessage(null, writer.Path, message + ". Skipping serializing self referenced value."), null);
return false;
case ReferenceLoopHandling.Serialize:
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Verbose)
TraceWriter.Trace(TraceLevel.Verbose, JsonPosition.FormatMessage(null, writer.Path, message + ". Serializing self referenced value."), null);
return true;
default:
throw new InvalidOperationException("Unexpected ReferenceLoopHandling value: '{0}'".FormatWith(CultureInfo.InvariantCulture, Serializer.ReferenceLoopHandling));
}
}
@@ -252,9 +257,14 @@ namespace Newtonsoft.Json.Serialization
private void WriteReference(JsonWriter writer, object value)
{
string reference = GetReference(writer, value);
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info)
TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(null, writer.Path, "Writing object reference to Id '{0}' for {1}.".FormatWith(CultureInfo.InvariantCulture, reference, value.GetType())), null);
writer.WriteStartObject();
writer.WritePropertyName(JsonTypeReflector.RefPropertyName);
writer.WriteValue(GetReference(writer, value));
writer.WriteValue(reference);
writer.WriteEndObject();
}
@@ -263,6 +273,7 @@ namespace Newtonsoft.Json.Serialization
try
{
string reference = Serializer.ReferenceResolver.GetReference(this, value);
return reference;
}
catch (Exception ex)
@@ -290,6 +301,7 @@ namespace Newtonsoft.Json.Serialization
#else
s = converter.ConvertToString(value);
#endif
return true;
}
}
@@ -315,18 +327,34 @@ namespace Newtonsoft.Json.Serialization
private void SerializeString(JsonWriter writer, object value, JsonStringContract contract)
{
contract.InvokeOnSerializing(value, Serializer.Context);
OnSerializing(writer, contract, value);
string s;
TryConvertToString(value, contract.UnderlyingType, out s);
writer.WriteValue(s);
OnSerialized(writer, contract, value);
}
private void OnSerializing(JsonWriter writer, JsonContract contract, object value)
{
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info)
TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(null, writer.Path, "Started serializing {0}".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType)), null);
contract.InvokeOnSerializing(value, Serializer.Context);
}
private void OnSerialized(JsonWriter writer, JsonContract contract, object value)
{
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info)
TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(null, writer.Path, "Finished serializing {0}".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType)), null);
contract.InvokeOnSerialized(value, Serializer.Context);
}
private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
{
contract.InvokeOnSerializing(value, Serializer.Context);
OnSerializing(writer, contract, value);
_serializeStack.Add(value);
@@ -349,7 +377,7 @@ namespace Newtonsoft.Json.Serialization
}
catch (Exception ex)
{
if (IsErrorHandled(value, contract, property.PropertyName, writer.ContainerPath, ex))
if (IsErrorHandled(value, contract, property.PropertyName, null, writer.ContainerPath, ex))
HandleError(writer, initialDepth);
else
throw;
@@ -360,7 +388,7 @@ namespace Newtonsoft.Json.Serialization
_serializeStack.RemoveAt(_serializeStack.Count - 1);
contract.InvokeOnSerialized(value, Serializer.Context);
OnSerialized(writer, contract, value);
}
private bool CalculatePropertyValues(JsonWriter writer, object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, out JsonContract memberContract, out object memberValue)
@@ -409,8 +437,7 @@ namespace Newtonsoft.Json.Serialization
bool isReference = ResolveIsReference(contract, member, collectionContract, containerProperty) ?? HasFlag(Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Objects);
if (isReference)
{
writer.WritePropertyName(JsonTypeReflector.IdPropertyName);
writer.WriteValue(GetReference(writer, value));
WriteReferenceIdProperty(writer, contract.UnderlyingType, value);
}
if (ShouldWriteType(TypeNameHandling.Objects, contract, member, collectionContract, containerProperty))
{
@@ -418,10 +445,26 @@ namespace Newtonsoft.Json.Serialization
}
}
private void WriteReferenceIdProperty(JsonWriter writer, Type type, object value)
{
string reference = GetReference(writer, value);
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Verbose)
TraceWriter.Trace(TraceLevel.Verbose, JsonPosition.FormatMessage(null, writer.Path, "Writing object reference Id '{0}' for {1}.".FormatWith(CultureInfo.InvariantCulture, reference, type)), null);
writer.WritePropertyName(JsonTypeReflector.IdPropertyName);
writer.WriteValue(reference);
}
private void WriteTypeProperty(JsonWriter writer, Type type)
{
string typeName = ReflectionUtils.GetTypeName(type, Serializer.TypeNameAssemblyFormat, Serializer.Binder);
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Verbose)
TraceWriter.Trace(TraceLevel.Verbose, JsonPosition.FormatMessage(null, writer.Path, "Writing type name '{0}' for {1}.".FormatWith(CultureInfo.InvariantCulture, typeName, type)), null);
writer.WritePropertyName(JsonTypeReflector.TypePropertyName);
writer.WriteValue(ReflectionUtils.GetTypeName(type, Serializer.TypeNameAssemblyFormat, Serializer.Binder));
writer.WriteValue(typeName);
}
private bool HasFlag(DefaultValueHandling value, DefaultValueHandling flag)
@@ -452,15 +495,21 @@ namespace Newtonsoft.Json.Serialization
_serializeStack.Add(value);
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info)
TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(null, writer.Path, "Started serializing {0} with converter {1}.".FormatWith(CultureInfo.InvariantCulture, value.GetType(), converter.GetType())), null);
converter.WriteJson(writer, value, GetInternalSerializer());
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info)
TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(null, writer.Path, "Finished serializing {0} with converter {1}.".FormatWith(CultureInfo.InvariantCulture, value.GetType(), converter.GetType())), null);
_serializeStack.RemoveAt(_serializeStack.Count - 1);
}
}
private void SerializeList(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
{
contract.InvokeOnSerializing(values.UnderlyingCollection, Serializer.Context);
OnSerializing(writer, contract, values.UnderlyingCollection);
_serializeStack.Add(values.UnderlyingCollection);
@@ -492,7 +541,7 @@ namespace Newtonsoft.Json.Serialization
}
catch (Exception ex)
{
if (IsErrorHandled(values.UnderlyingCollection, contract, index, writer.ContainerPath, ex))
if (IsErrorHandled(values.UnderlyingCollection, contract, index, null, writer.ContainerPath, ex))
HandleError(writer, initialDepth);
else
throw;
@@ -510,12 +559,12 @@ namespace Newtonsoft.Json.Serialization
_serializeStack.RemoveAt(_serializeStack.Count - 1);
contract.InvokeOnSerialized(values.UnderlyingCollection, Serializer.Context);
OnSerialized(writer, contract, values.UnderlyingCollection);
}
private void SerializeMultidimensionalArray(JsonWriter writer, Array values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
{
contract.InvokeOnSerializing(values, Serializer.Context);
OnSerializing(writer, contract, values);
_serializeStack.Add(values);
@@ -528,7 +577,7 @@ namespace Newtonsoft.Json.Serialization
_serializeStack.RemoveAt(_serializeStack.Count - 1);
contract.InvokeOnSerialized(values, Serializer.Context);
OnSerialized(writer, contract, values);
}
private void SerializeMultidimensionalArray(JsonWriter writer, Array values, JsonArrayContract contract, JsonProperty member, int initialDepth, int[] indices)
@@ -569,7 +618,7 @@ namespace Newtonsoft.Json.Serialization
}
catch (Exception ex)
{
if (IsErrorHandled(values, contract, i, writer.ContainerPath, ex))
if (IsErrorHandled(values, contract, i, null, writer.ContainerPath, ex))
HandleError(writer, initialDepth + 1);
else
throw;
@@ -596,8 +645,7 @@ namespace Newtonsoft.Json.Serialization
if (isReference)
{
writer.WritePropertyName(JsonTypeReflector.IdPropertyName);
writer.WriteValue(GetReference(writer, values));
WriteReferenceIdProperty(writer, contract.UnderlyingType, values);
}
if (includeTypeDetails)
{
@@ -624,7 +672,7 @@ namespace Newtonsoft.Json.Serialization
To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true.".FormatWith(CultureInfo.InvariantCulture, value.GetType()), null);
}
contract.InvokeOnSerializing(value, Serializer.Context);
OnSerializing(writer, contract, value);
_serializeStack.Add(value);
WriteObjectStart(writer, value, contract, member, collectionContract, containerProperty);
@@ -641,14 +689,14 @@ To fix this error either change the environment to be fully trusted, change the
writer.WriteEndObject();
_serializeStack.RemoveAt(_serializeStack.Count - 1);
contract.InvokeOnSerialized(value, Serializer.Context);
OnSerialized(writer, contract, value);
}
#endif
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
private void SerializeDynamic(JsonWriter writer, IDynamicMetaObjectProvider value, JsonDynamicContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
{
contract.InvokeOnSerializing(value, Serializer.Context);
OnSerializing(writer, contract, value);
_serializeStack.Add(value);
WriteObjectStart(writer, value, contract, member, collectionContract, containerProperty);
@@ -673,7 +721,7 @@ To fix this error either change the environment to be fully trusted, change the
}
catch (Exception ex)
{
if (IsErrorHandled(value, contract, property.PropertyName, writer.ContainerPath, ex))
if (IsErrorHandled(value, contract, property.PropertyName, null, writer.ContainerPath, ex))
HandleError(writer, initialDepth);
else
throw;
@@ -697,7 +745,7 @@ To fix this error either change the environment to be fully trusted, change the
}
catch (Exception ex)
{
if (IsErrorHandled(value, contract, memberName, writer.ContainerPath, ex))
if (IsErrorHandled(value, contract, memberName, null, writer.ContainerPath, ex))
HandleError(writer, initialDepth);
else
throw;
@@ -708,7 +756,7 @@ To fix this error either change the environment to be fully trusted, change the
writer.WriteEndObject();
_serializeStack.RemoveAt(_serializeStack.Count - 1);
contract.InvokeOnSerialized(value, Serializer.Context);
OnSerialized(writer, contract, value);
}
#endif
@@ -743,8 +791,7 @@ To fix this error either change the environment to be fully trusted, change the
private void SerializeDictionary(JsonWriter writer, IWrappedDictionary values, JsonDictionaryContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
{
contract.InvokeOnSerializing(values.UnderlyingDictionary, Serializer.Context);
OnSerializing(writer, contract, values.UnderlyingDictionary);
_serializeStack.Add(values.UnderlyingDictionary);
WriteObjectStart(writer, values.UnderlyingDictionary, contract, member, collectionContract, containerProperty);
@@ -787,7 +834,7 @@ To fix this error either change the environment to be fully trusted, change the
}
catch (Exception ex)
{
if (IsErrorHandled(values.UnderlyingDictionary, contract, propertyName, writer.ContainerPath, ex))
if (IsErrorHandled(values.UnderlyingDictionary, contract, propertyName, null, writer.ContainerPath, ex))
HandleError(writer, initialDepth);
else
throw;
@@ -798,7 +845,7 @@ To fix this error either change the environment to be fully trusted, change the
_serializeStack.RemoveAt(_serializeStack.Count - 1);
contract.InvokeOnSerialized(values.UnderlyingDictionary, Serializer.Context);
OnSerialized(writer, contract, values.UnderlyingDictionary);
}
private string GetPropertyName(DictionaryEntry entry)