本文整理汇总了C#中Newtonsoft.Json.Serialization.JsonObjectContract.InvokeOnSerializing方法的典型用法代码示例。如果您正苦于以下问题:C# JsonObjectContract.InvokeOnSerializing方法的具体用法?C# JsonObjectContract.InvokeOnSerializing怎么用?C# JsonObjectContract.InvokeOnSerializing使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Newtonsoft.Json.Serialization.JsonObjectContract
的用法示例。
在下文中一共展示了JsonObjectContract.InvokeOnSerializing方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SerializeObject
private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract, JsonProperty member, JsonContract collectionValueContract)
{
contract.InvokeOnSerializing(value, Serializer.Context);
SerializeStack.Add(value);
writer.WriteStartObject();
bool isReference = contract.IsReference ?? HasFlag(Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Objects);
if (isReference)
{
writer.WritePropertyName(JsonTypeReflector.IdPropertyName);
writer.WriteValue(Serializer.ReferenceResolver.GetReference(this, value));
}
if (ShouldWriteType(TypeNameHandling.Objects, contract, member, collectionValueContract))
{
WriteTypeProperty(writer, contract.UnderlyingType);
}
int initialDepth = writer.Top;
foreach (JsonProperty property in contract.Properties)
{
try
{
if (!property.Ignored && property.Readable && ShouldSerialize(property, value) && IsSpecified(property, value))
{
object memberValue = property.ValueProvider.GetValue(value);
JsonContract memberContract = GetContractSafe(memberValue);
WriteMemberInfoProperty(writer, memberValue, property, memberContract);
}
}
catch (Exception ex)
{
if (IsErrorHandled(value, contract, property.PropertyName, ex))
HandleError(writer, initialDepth);
else
throw;
}
}
writer.WriteEndObject();
SerializeStack.RemoveAt(SerializeStack.Count - 1);
contract.InvokeOnSerialized(value, Serializer.Context);
}
示例2: SerializeObject
private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
{
contract.InvokeOnSerializing(value, Serializer.Context);
_serializeStack.Add(value);
WriteObjectStart(writer, value, contract, member, collectionContract, containerProperty);
int initialDepth = writer.Top;
foreach (JsonProperty property in contract.Properties)
{
try
{
object memberValue;
JsonContract memberContract;
if (!CalculatePropertyValues(writer, value, contract, member, property, out memberContract, out memberValue))
continue;
writer.WritePropertyName(property.PropertyName);
SerializeValue(writer, memberValue, memberContract, property, contract, member);
}
catch (Exception ex)
{
if (IsErrorHandled(value, contract, property.PropertyName, writer.ContainerPath, ex))
HandleError(writer, initialDepth);
else
throw;
}
}
writer.WriteEndObject();
_serializeStack.RemoveAt(_serializeStack.Count - 1);
contract.InvokeOnSerialized(value, Serializer.Context);
}
示例3: SerializeObject
private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract)
{
contract.InvokeOnSerializing(value);
#if !SILVERLIGHT && !PocketPC
TypeConverter converter = TypeDescriptor.GetConverter(contract.UnderlyingType);
// 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));
contract.InvokeOnSerialized(value);
return;
}
}
#else
if (value is Guid || value is Type || value is Uri)
{
writer.WriteValue(value.ToString());
return;
}
#endif
SerializeStack.Add(value);
writer.WriteStartObject();
bool isReference = contract.IsReference ?? HasFlag(_serializer.PreserveReferencesHandling, PreserveReferencesHandling.Objects);
if (isReference)
{
writer.WritePropertyName(JsonTypeReflector.IdPropertyName);
writer.WriteValue(_serializer.ReferenceResolver.GetReference(value));
}
if (HasFlag(_serializer.TypeNameHandling, TypeNameHandling.Objects))
{
WriteTypeProperty(writer, contract.UnderlyingType);
}
foreach (JsonProperty property in contract.Properties)
{
if (!property.Ignored && property.Readable)
WriteMemberInfoProperty(writer, value, property);
}
writer.WriteEndObject();
SerializeStack.RemoveAt(SerializeStack.Count - 1);
contract.InvokeOnSerialized(value);
}
示例4: SerializeObject
private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
{
contract.InvokeOnSerializing(value, Serializer.Context);
_serializeStack.Add(value);
WriteObjectStart(writer, value, contract, member, collectionContract, containerProperty);
int initialDepth = writer.Top;
foreach (JsonProperty property in contract.Properties)
{
try
{
if (!property.Ignored && property.Readable && ShouldSerialize(property, value) && IsSpecified(property, value))
{
if (property.PropertyContract == null)
property.PropertyContract = Serializer.ContractResolver.ResolveContract(property.PropertyType);
object memberValue = property.ValueProvider.GetValue(value);
JsonContract memberContract = (property.PropertyContract.UnderlyingType.IsSealed()) ? property.PropertyContract : GetContractSafe(memberValue);
if (ShouldWriteProperty(memberValue, property))
{
string propertyName = property.PropertyName;
if (ShouldWriteReference(memberValue, property, memberContract, contract, member))
{
writer.WritePropertyName(propertyName);
WriteReference(writer, memberValue);
continue;
}
if (!CheckForCircularReference(writer, memberValue, property, memberContract, contract, member))
continue;
if (memberValue == null)
{
Required resolvedRequired = property._required ?? contract.ItemRequired ?? Required.Default;
if (resolvedRequired == Required.Always)
throw JsonSerializationException.Create(null, writer.ContainerPath, "Cannot write a null value for property '{0}'. Property requires a value.".FormatWith(CultureInfo.InvariantCulture, property.PropertyName), null);
}
writer.WritePropertyName(propertyName);
SerializeValue(writer, memberValue, memberContract, property, contract, member);
}
}
}
catch (Exception ex)
{
if (IsErrorHandled(value, contract, property.PropertyName, writer.ContainerPath, ex))
HandleError(writer, initialDepth);
else
throw;
}
}
writer.WriteEndObject();
_serializeStack.RemoveAt(_serializeStack.Count - 1);
contract.InvokeOnSerialized(value, Serializer.Context);
}
示例5: SerializeObject
private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract)
{
contract.InvokeOnSerializing(value);
string s;
if (TryConvertToString(value, contract.UnderlyingType, out s))
{
writer.WriteValue(s);
#if !SILVERLIGHT && !PocketPC
contract.InvokeOnSerialized(value);
#endif
return;
}
SerializeStack.Add(value);
writer.WriteStartObject();
bool isReference = contract.IsReference ?? HasFlag(Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Objects);
if (isReference)
{
writer.WritePropertyName(JsonTypeReflector.IdPropertyName);
writer.WriteValue(Serializer.ReferenceResolver.GetReference(value));
}
if (HasFlag(Serializer.TypeNameHandling, TypeNameHandling.Objects))
{
WriteTypeProperty(writer, contract.UnderlyingType);
}
int initialDepth = writer.Top;
foreach (JsonProperty property in contract.Properties)
{
try
{
if (!property.Ignored && property.Readable)
WriteMemberInfoProperty(writer, value, property);
}
catch (Exception ex)
{
if (IsErrorHandled(value, contract, property.Member.Name, ex))
HandleError(writer, initialDepth);
else
throw;
}
}
writer.WriteEndObject();
SerializeStack.RemoveAt(SerializeStack.Count - 1);
contract.InvokeOnSerialized(value);
}
示例6: SerializeObject
private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract)
{
contract.InvokeOnSerializing(value, Serializer.Context);
_serializeStack.Add(value);
WriteObjectStart(writer, value, contract, member, collectionContract);
int initialDepth = writer.Top;
foreach (JsonProperty property in contract.Properties)
{
try
{
if (!property.Ignored && property.Readable && ShouldSerialize(property, value) && IsSpecified(property, value))
{
if (property.PropertyContract == null)
property.PropertyContract = Serializer.ContractResolver.ResolveContract(property.PropertyType);
object memberValue = property.ValueProvider.GetValue(value);
JsonContract memberContract = (property.PropertyContract.UnderlyingType.IsSealed()) ? property.PropertyContract : GetContractSafe(memberValue);
if (ShouldWriteProperty(memberValue, property))
{
WriteMemberInfoProperty(writer, memberValue, property, memberContract, contract);
}
}
}
catch (Exception ex)
{
if (IsErrorHandled(value, contract, property.PropertyName, writer.ContainerPath, ex))
HandleError(writer, initialDepth);
else
throw;
}
}
writer.WriteEndObject();
_serializeStack.RemoveAt(_serializeStack.Count - 1);
contract.InvokeOnSerialized(value, Serializer.Context);
}
示例7: SerializeObject
private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
{
contract.InvokeOnSerializing(value, this.Serializer.Context);
this._serializeStack.Add(value);
this.WriteObjectStart(writer, value, (JsonContract) contract, member, collectionContract, containerProperty);
int top = writer.Top;
foreach (JsonProperty jsonProperty in (Collection<JsonProperty>) contract.Properties)
{
try
{
JsonContract memberContract;
object memberValue;
if (this.CalculatePropertyValues(writer, value, (JsonContainerContract) contract, member, jsonProperty, out memberContract, out memberValue))
{
writer.WritePropertyName(jsonProperty.PropertyName);
this.SerializeValue(writer, memberValue, memberContract, jsonProperty, (JsonContainerContract) contract, member);
}
}
catch (Exception ex)
{
if (this.IsErrorHandled(value, (JsonContract) contract, (object) jsonProperty.PropertyName, writer.ContainerPath, ex))
this.HandleError(writer, top);
else
throw;
}
}
writer.WriteEndObject();
this._serializeStack.RemoveAt(this._serializeStack.Count - 1);
contract.InvokeOnSerialized(value, this.Serializer.Context);
}