-Line endings
This commit is contained in:
@@ -1,33 +1,33 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Serialization Error Handling</title>
|
||||
<link href="styles.css" rel="stylesheet" type="text/css" />
|
||||
<link href="custom.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<div id="control">
|
||||
<span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
|
||||
<span class="topicTitle">Serialization Error Handling</span></div>
|
||||
|
||||
<div id="content">
|
||||
<span style="color: DarkGray"> </span>
|
||||
|
||||
<p>Json.NET supports error handling during serialization and deserialization. Error handling lets
|
||||
you catch an error and choose whether to handle it and continue with serialization or let the error
|
||||
bubble up and be thrown in your application.</p>
|
||||
<p>Error handling is defined through two methods:
|
||||
the Error event on JsonSerializer and the OnErrorAttribute.</p>
|
||||
<h3>Error Event</h3>
|
||||
|
||||
<p>
|
||||
The <a href="./html/E_Newtonsoft_Json_JsonSerializer_Error.htm">Error</a> event is an event handler found on <a href="./html/T_Newtonsoft_Json_JsonSerializer.htm">JsonSerializer</a>. The error event is raised whenever an
|
||||
exception is thrown while serializing or deserialing JSON. Like all settings found on JsonSerializer
|
||||
it can also be set on <a href="./html/T_Newtonsoft_Json_JsonSerializer.htm">JsonSerializerSettings</a> and passed to the serialization methods on JsonConvert.</p>
|
||||
|
||||
<div class="overflowpanel"> <div class="code">
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Serialization Error Handling</title>
|
||||
<link href="styles.css" rel="stylesheet" type="text/css" />
|
||||
<link href="custom.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<div id="control">
|
||||
<span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
|
||||
<span class="topicTitle">Serialization Error Handling</span></div>
|
||||
|
||||
<div id="content">
|
||||
<span style="color: DarkGray"> </span>
|
||||
|
||||
<p>Json.NET supports error handling during serialization and deserialization. Error handling lets
|
||||
you catch an error and choose whether to handle it and continue with serialization or let the error
|
||||
bubble up and be thrown in your application.</p>
|
||||
<p>Error handling is defined through two methods:
|
||||
the Error event on JsonSerializer and the OnErrorAttribute.</p>
|
||||
<h3>Error Event</h3>
|
||||
|
||||
<p>
|
||||
The <a href="./html/E_Newtonsoft_Json_JsonSerializer_Error.htm">Error</a> event is an event handler found on <a href="./html/T_Newtonsoft_Json_JsonSerializer.htm">JsonSerializer</a>. The error event is raised whenever an
|
||||
exception is thrown while serializing or deserialing JSON. Like all settings found on JsonSerializer
|
||||
it can also be set on <a href="./html/T_Newtonsoft_Json_JsonSerializer.htm">JsonSerializerSettings</a> and passed to the serialization methods on JsonConvert.</p>
|
||||
|
||||
<div class="overflowpanel"> <div class="code">
|
||||
<div style="font-family: Courier New; font-size: 10pt; color: black;">
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">List</span><<span style="color: blue;">string</span>> errors = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span><<span style="color: blue;">string</span>>();</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
@@ -71,15 +71,15 @@
|
||||
for the null value since the list doesn't allow nullable DateTimes. The event handler has logged these messages
|
||||
and Json.NET has continued on deserializing the JSON because the errors were marked as handled.
|
||||
</p>
|
||||
<p>
|
||||
One thing to note with error handling in Json.NET is that an unhandled error will bubble up and raise the event
|
||||
on each of its parents, e.g. an unhandled error when serializing a collection of objects will be raised twice,
|
||||
once against the object and then again on the collection. This will let you handle an error either where it
|
||||
occurred or on one of its parents.
|
||||
</p>
|
||||
|
||||
|
||||
<div class="overflowpanel"> <div class="code">
|
||||
<p>
|
||||
One thing to note with error handling in Json.NET is that an unhandled error will bubble up and raise the event
|
||||
on each of its parents, e.g. an unhandled error when serializing a collection of objects will be raised twice,
|
||||
once against the object and then again on the collection. This will let you handle an error either where it
|
||||
occurred or on one of its parents.
|
||||
</p>
|
||||
|
||||
|
||||
<div class="overflowpanel"> <div class="code">
|
||||
<div style="font-family: Courier New; font-size: 10pt; color: black;">
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">JsonSerializer</span> serializer = <span style="color: blue;">new</span> <span style="color: #2b91af;">JsonSerializer</span>();</pre>
|
||||
<pre style="margin: 0px;">serializer.Error += <span style="color: blue;">delegate</span>(<span style="color: blue;">object</span> sender, <span style="color: #2b91af;">ErrorEventArgs</span> args)</pre>
|
||||
@@ -89,21 +89,21 @@
|
||||
<pre style="margin: 0px;"> errors.Add(args.ErrorContext.Error.Message);</pre>
|
||||
<pre style="margin: 0px;"> };</pre>
|
||||
</div>
|
||||
</div></div>
|
||||
|
||||
<p>If you aren't immediately handling an error and only want to perform an action against it once then
|
||||
you can check to see whether the <a href="./html/T_Newtonsoft_Json_Serialization_ErrorEventArgs.htm">ErrorEventArg</a>'s CurrentObject is equal to the OriginalObject.
|
||||
OriginalObject is the object that threw the error and CurrentObject is the object that the event is being raised
|
||||
against. They will only equal the first time the event is raised against the OriginalObject.</p>
|
||||
|
||||
<h3>OnErrorAttribute</h3>
|
||||
|
||||
<p>
|
||||
The <a href="./html/T_Newtonsoft_Json_Serialization_OnErrorAttribute.htm">OnErrorAttribute</a> works much like the other <a href="SerializationCallbacks.html">.NET serialization attributes</a> that Json.NET supports.
|
||||
To use it you simply place the attribute on a method which takes the correct parameters: a StreamingContext and a ErrorContext.
|
||||
The name of the method doesn't matter. </p>
|
||||
<div class="overflowpanel"> <div class="code">
|
||||
|
||||
</div></div>
|
||||
|
||||
<p>If you aren't immediately handling an error and only want to perform an action against it once then
|
||||
you can check to see whether the <a href="./html/T_Newtonsoft_Json_Serialization_ErrorEventArgs.htm">ErrorEventArg</a>'s CurrentObject is equal to the OriginalObject.
|
||||
OriginalObject is the object that threw the error and CurrentObject is the object that the event is being raised
|
||||
against. They will only equal the first time the event is raised against the OriginalObject.</p>
|
||||
|
||||
<h3>OnErrorAttribute</h3>
|
||||
|
||||
<p>
|
||||
The <a href="./html/T_Newtonsoft_Json_Serialization_OnErrorAttribute.htm">OnErrorAttribute</a> works much like the other <a href="SerializationCallbacks.html">.NET serialization attributes</a> that Json.NET supports.
|
||||
To use it you simply place the attribute on a method which takes the correct parameters: a StreamingContext and a ErrorContext.
|
||||
The name of the method doesn't matter. </p>
|
||||
<div class="overflowpanel"> <div class="code">
|
||||
|
||||
<div style="font-family: Courier New; font-size: 10pt; color: black;">
|
||||
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">PersonError</span></pre>
|
||||
<pre style="margin: 0px;">{</pre>
|
||||
@@ -131,14 +131,14 @@ against. They will only equal the first time the event is raised against the Ori
|
||||
<pre style="margin: 0px;"> }</pre>
|
||||
<pre style="margin: 0px;">}</pre>
|
||||
</div>
|
||||
</div></div>
|
||||
<p>
|
||||
In this example accessing the the Roles property will throw an exception when no roles have
|
||||
been set. The HandleError method will set the error when serializing Roles as handled and allow Json.NET to continue
|
||||
serializing the class.
|
||||
</p>
|
||||
|
||||
<div class="overflowpanel"> <div class="code">
|
||||
</div></div>
|
||||
<p>
|
||||
In this example accessing the the Roles property will throw an exception when no roles have
|
||||
been set. The HandleError method will set the error when serializing Roles as handled and allow Json.NET to continue
|
||||
serializing the class.
|
||||
</p>
|
||||
|
||||
<div class="overflowpanel"> <div class="code">
|
||||
<div style="font-family: Courier New; font-size: 10pt; color: black;">
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">PersonError</span> person = <span style="color: blue;">new</span> <span style="color: #2b91af;">PersonError</span></pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
@@ -158,15 +158,15 @@ against. They will only equal the first time the event is raised against the Ori
|
||||
<pre style="margin: 0px;"><span style="color: green;">//}</span></pre>
|
||||
</div>
|
||||
</div></div>
|
||||
|
||||
|
||||
<div id="footer">
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
|
||||
|
||||
<div id="footer">
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Reference in New Issue
Block a user