-More documentation
-Changed CustomCreationConverter to throw NotImplementedException when serializing
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>CustomCreationConverter</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">CustomCreationConverter</span></div>
|
||||
|
||||
<div id="content">
|
||||
<span style="color: DarkGray"> </span>
|
||||
|
||||
<p>
|
||||
The CustomCreationConverter is a JsonConverter that provides a way to customize how an object is created
|
||||
during JSON deserialization.
|
||||
Once the object has been created it will then have values populated onto it by the serializer.
|
||||
</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;">interface</span> <span style="color: #2b91af;">IPerson</span></pre>
|
||||
<pre style="margin: 0px;">{</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">string</span> FirstName { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">string</span> LastName { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: #2b91af;">DateTime</span> BirthDate { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;">}</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">Employee</span> : <span style="color: #2b91af;">IPerson</span></pre>
|
||||
<pre style="margin: 0px;">{</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> FirstName { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> LastName { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: #2b91af;">DateTime</span> BirthDate { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> Department { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> JobTitle { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;">}</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">PersonConverter</span> : <span style="color: #2b91af;">CustomCreationConverter</span><<span style="color: #2b91af;">IPerson</span>></pre>
|
||||
<pre style="margin: 0px;">{</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: #2b91af;">IPerson</span> Create(<span style="color: #2b91af;">Type</span> objectType)</pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">return</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">Employee</span>();</pre>
|
||||
<pre style="margin: 0px;"> }</pre>
|
||||
<pre style="margin: 0px;">}</pre>
|
||||
</div>
|
||||
</div></div>
|
||||
|
||||
<p>
|
||||
This is an extremely simple example. A more complicated scenario could involve an object factory or service locator
|
||||
which resolves the object at runtime.
|
||||
</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: green;">//[</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "FirstName": "Maurice",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "LastName": "Moss",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "BirthDate": "\/Date(252291661000)\/",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Department": "IT",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "JobTitle": "Support"</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "FirstName": "Jen",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "LastName": "Barber",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "BirthDate": "\/Date(258771661000)\/",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Department": "IT",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "JobTitle": "Manager"</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// }</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">//]</span></pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">List</span><<span style="color: #2b91af;">IPerson</span>> people = <span style="color: #2b91af;">JsonConvert</span>.DeserializeObject<<span style="color: #2b91af;">List</span><<span style="color: #2b91af;">IPerson</span>>>(json, <span style="color: blue;">new</span> <span style="color: #2b91af;">PersonConverter</span>());</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">IPerson</span> person = people[0];</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(person.GetType());</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// Newtonsoft.Json.Tests.Employee</span></pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(person.FirstName);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// Maurice</span></pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Employee</span> employee = (<span style="color: #2b91af;">Employee</span>)person;</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(employee.JobTitle);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// Support</span></pre>
|
||||
</div>
|
||||
</div></div>
|
||||
|
||||
<div id="footer"></div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -11,19 +11,18 @@
|
||||
<div id="content">
|
||||
<span style="color: DarkGray"></span>
|
||||
<p>
|
||||
The Json.NET library makes working with JavaScript and JSON formatted data in .NET
|
||||
simple. Quickly read and write JSON using the JsonReader and JsonWriter or serialize
|
||||
Json.NET makes working with JSON formatted data in .NET
|
||||
simple. Quickly read and write JSON using LINQ to JSON or serialize
|
||||
your .NET objects with a single method call using the JsonSerializer.</p>
|
||||
<h3>
|
||||
Features</h3>
|
||||
<ul>
|
||||
<li>LINQ to JSON</li>
|
||||
<li>Lightning fast JsonReader and JsonWriter</li>
|
||||
<li>The JsonSerializer for quickly converting your .NET objects to JSON and back again</li>
|
||||
<li>Json.NET can optionally produce well formatted, indented JSON for debugging or display</li>
|
||||
<li>Attributes like JsonIgnore and JsonProperty can be added to a class to customize
|
||||
how a class is serialized</li>
|
||||
<li>Ability to convert JSON to and from XML</li>
|
||||
<li>Convert JSON to and from XML</li>
|
||||
</ul>
|
||||
<h3>
|
||||
History</h3>
|
||||
@@ -49,9 +48,9 @@
|
||||
<h3>
|
||||
Donate</h3>
|
||||
<p>
|
||||
Json.NET is a free open source project that is developed in personal time.</p>
|
||||
Json.NET is a free open source project that I have developed in my personal time.</p>
|
||||
<p>
|
||||
You can show your appreciation for Json.NET and support future development by donating.</p>
|
||||
You can show your support for Json.NET by donating. I really appreciate your feedback and support.</p>
|
||||
<p><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=james%40newtonking%2ecom&item_name=Supporting%20Json%2eNET&no_shipping=0&no_note=1&tax=0¤cy_code=USD&lc=US&bn=PP%2dDonationsBF&charset=UTF%2d8" target="_blank">
|
||||
<img src="donate.gif" alt="Donate" style="border-width:0;" />
|
||||
</a></p>
|
||||
|
||||
+64
-3
@@ -149,15 +149,20 @@
|
||||
<h3>Querying JSON</h3>
|
||||
|
||||
|
||||
<p>The properties methods that are the most useful when querying JSON objects are <a href="./html/Overload_Newtonsoft_Json_Linq_JToken_Children.htm">Children</a>, <a href="./html/M_Newtonsoft_Json_Linq_JToken_Values__1.htm">Values</a> and the <a href="./html/P_Newtonsoft_Json_Linq_JToken_Item.htm">property index</a>.</p>
|
||||
<p>Children returns all the children of that object. Values is similar to Children but it allows you to specify a type you would like the values to be converted into. Finally the property index is used to get a specific child, either by index position for JSON arrays or property name for JSON objects.</p>
|
||||
<p>The properties methods that are the most useful when querying JSON objects are
|
||||
the <a href="./html/Overload_Newtonsoft_Json_Linq_JToken_Children.htm">Children</a>
|
||||
method and the <a href="./html/P_Newtonsoft_Json_Linq_JToken_Item.htm">property index</a>.</p>
|
||||
<p>Children returns all the children of that object. If it is a JObject it will
|
||||
return a collection of properties to work with and if it is a JArray you will
|
||||
get a collection of the array's values.</p>
|
||||
<p>The property index is used to get a specific child, either by index position for JSON arrays or property name for JSON objects.</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;">var</span> postTitles =</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">from</span> p <span style="color: blue;">in</span> rss[<span style="color: #a31515;">"channel"</span>][<span style="color: #a31515;">"item"</span>].Children()</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">select</span> p.Value<<span style="color: blue;">string</span>>(<span style="color: #a31515;">"title"</span>);</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">select</span> (<span style="color: blue;">string</span>)p[<span style="color: #a31515;">"title"</span>];</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> item <span style="color: blue;">in</span> postTitles)</pre>
|
||||
<pre style="margin: 0px;">{</pre>
|
||||
@@ -183,6 +188,62 @@
|
||||
<pre style="margin: 0px;"><span style="color: green;">//CodePlex - Count: 1</span></pre>
|
||||
</div>
|
||||
|
||||
</div></div>
|
||||
|
||||
<p>LINQ to JSON can also be used to manually convert from JSON to a .NET object.</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;">Shortie</span></pre>
|
||||
<pre style="margin: 0px;">{</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> Original { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> Shortened { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> Short { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: #2b91af;">ShortieException</span> Error { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;">}</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ShortieException</span></pre>
|
||||
<pre style="margin: 0px;">{</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">int</span> Code { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> ErrorMessage { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;">}</pre>
|
||||
</div>
|
||||
</div></div>
|
||||
|
||||
<p>Manually serializing and deserializing between .NET objects is most useful when working with JSON that doesn't closely match your .NET objects.</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;">string</span> jsonText = <span style="color: #a31515;">@"{</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""short"":{</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""original"":""http://www.foo.com/"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""short"":""krehqk"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""error"":{</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""code"":0,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""msg"":""No action taken""}</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;">}"</span>;</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">JObject</span> json = <span style="color: #2b91af;">JObject</span>.Parse(jsonText);</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Shortie</span> shortie = <span style="color: blue;">new</span> <span style="color: #2b91af;">Shortie</span></pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> Original = (<span style="color: blue;">string</span>)json[<span style="color: #a31515;">"short"</span>][<span style="color: #a31515;">"original"</span>],</pre>
|
||||
<pre style="margin: 0px;"> Short = (<span style="color: blue;">string</span>)json[<span style="color: #a31515;">"short"</span>][<span style="color: #a31515;">"short"</span>],</pre>
|
||||
<pre style="margin: 0px;"> Error = <span style="color: blue;">new</span> <span style="color: #2b91af;">ShortieException</span></pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> Code = (<span style="color: blue;">int</span>)json[<span style="color: #a31515;">"short"</span>][<span style="color: #a31515;">"error"</span>][<span style="color: #a31515;">"code"</span>],</pre>
|
||||
<pre style="margin: 0px;"> ErrorMessage = (<span style="color: blue;">string</span>)json[<span style="color: #a31515;">"short"</span>][<span style="color: #a31515;">"error"</span>][<span style="color: #a31515;">"msg"</span>]</pre>
|
||||
<pre style="margin: 0px;"> }</pre>
|
||||
<pre style="margin: 0px;"> };</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(shortie.Original);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// http://www.foo.com/</span></pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(shortie.Error.ErrorMessage);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// No action taken</span></pre>
|
||||
</div>
|
||||
|
||||
</div></div>
|
||||
|
||||
<div id="footer"></div>
|
||||
|
||||
@@ -73,8 +73,8 @@
|
||||
<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;"><span style="color: #2b91af;">Person</span> p = (<span style="color: #2b91af;">Person</span>)serializer.Deserialize(<span style="color: blue;">new</span> <span style="color: #2b91af;">JTokenReader</span>(o), <span style="color: blue;">typeof</span>(<span style="color: #2b91af;">Person</span>));</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// John Smith</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(p.Name);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// John Smith</span></pre>
|
||||
</div>
|
||||
|
||||
</div></div>
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
<contentItem sourcePath=".\donate.gif" destPath="" excludeItems="False" />
|
||||
<contentItem sourcePath=".\AdvanceSerializingJSON.html" destPath="" excludeItems="False" />
|
||||
<contentItem sourcePath=".\DatesInJSON.html" destPath="" excludeItems="False" />
|
||||
<contentItem sourcePath=".\CustomCreationConverter.html" destPath="" excludeItems="False" />
|
||||
</additionalContent>
|
||||
<ProjectSummary />
|
||||
<MissingTags>Summary, Parameter, Returns, AutoDocumentCtors, Namespace, TypeParameter</MissingTags>
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
<siteMapNode title="Serializing and deserializing JSON" url=".\SerializingJSON.html">
|
||||
<siteMapNode title="Advanced JSON serialization" url=".\AdvanceSerializingJSON.html" />
|
||||
<siteMapNode title="Serializing dates in JSON" url=".\DatesInJSON.html" />
|
||||
<siteMapNode title="CustomCreationConverter" url=".\CustomCreationConverter.html" />
|
||||
</siteMapNode>
|
||||
<siteMapNode title="LINQ to JSON" url=".\LINQtoJSON.html" />
|
||||
<siteMapNode title="Reading and writing JSON" url=".\ReadingWritingJSON.html" />
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Converters
|
||||
{
|
||||
public class CustomCreationConverterTests : TestFixtureBase
|
||||
{
|
||||
public interface IPerson
|
||||
{
|
||||
string FirstName { get; set; }
|
||||
string LastName { get; set; }
|
||||
DateTime BirthDate { get; set; }
|
||||
}
|
||||
|
||||
public class Employee : IPerson
|
||||
{
|
||||
public string FirstName { get; set; }
|
||||
public string LastName { get; set; }
|
||||
public DateTime BirthDate { get; set; }
|
||||
|
||||
public string Department { get; set; }
|
||||
public string JobTitle { get; set; }
|
||||
}
|
||||
|
||||
public class PersonConverter : CustomCreationConverter<IPerson>
|
||||
{
|
||||
public override IPerson Create(Type objectType)
|
||||
{
|
||||
return new Employee();
|
||||
}
|
||||
}
|
||||
|
||||
public void DeserializeObject()
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(new List<Employee>
|
||||
{
|
||||
new Employee
|
||||
{
|
||||
BirthDate = new DateTime(1977, 12, 30, 1, 1, 1, DateTimeKind.Utc),
|
||||
FirstName = "Maurice",
|
||||
LastName = "Moss",
|
||||
Department = "IT",
|
||||
JobTitle = "Support"
|
||||
},
|
||||
new Employee
|
||||
{
|
||||
BirthDate = new DateTime(1978, 3, 15, 1, 1, 1, DateTimeKind.Utc),
|
||||
FirstName = "Jen",
|
||||
LastName = "Barber",
|
||||
Department = "IT",
|
||||
JobTitle = "Manager"
|
||||
}
|
||||
}, Formatting.Indented);
|
||||
|
||||
//[
|
||||
// {
|
||||
// "FirstName": "Maurice",
|
||||
// "LastName": "Moss",
|
||||
// "BirthDate": "\/Date(252291661000)\/",
|
||||
// "Department": "IT",
|
||||
// "JobTitle": "Support"
|
||||
// },
|
||||
// {
|
||||
// "FirstName": "Jen",
|
||||
// "LastName": "Barber",
|
||||
// "BirthDate": "\/Date(258771661000)\/",
|
||||
// "Department": "IT",
|
||||
// "JobTitle": "Manager"
|
||||
// }
|
||||
//]
|
||||
|
||||
List<IPerson> people = JsonConvert.DeserializeObject<List<IPerson>>(json, new PersonConverter());
|
||||
|
||||
IPerson person = people[0];
|
||||
|
||||
Console.WriteLine(person.GetType());
|
||||
// Newtonsoft.Json.Tests.Employee
|
||||
|
||||
Console.WriteLine(person.FirstName);
|
||||
// Maurice
|
||||
|
||||
Employee employee = (Employee)person;
|
||||
|
||||
Console.WriteLine(employee.JobTitle);
|
||||
// Support
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -480,6 +480,12 @@ Parameter name: arrayIndex")]
|
||||
}
|
||||
};
|
||||
|
||||
Console.WriteLine(shortie.Original);
|
||||
// http://www.foo.com/
|
||||
|
||||
Console.WriteLine(shortie.Error.ErrorMessage);
|
||||
// No action taken
|
||||
|
||||
Assert.AreEqual("http://www.foo.com/", shortie.Original);
|
||||
Assert.AreEqual("krehqk", shortie.Short);
|
||||
Assert.AreEqual(null, shortie.Shortened);
|
||||
|
||||
@@ -84,6 +84,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Converters\BinaryConverterTests.cs" />
|
||||
<Compile Include="Converters\CustomCreationConverterTests.cs" />
|
||||
<Compile Include="Converters\StringEnumConverterTests.cs" />
|
||||
<Compile Include="JsonValidatingReaderTests.cs" />
|
||||
<Compile Include="LinqToSql\Department.cs" />
|
||||
|
||||
@@ -6,33 +6,17 @@ using System.Xml.Serialization;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.TestObjects
|
||||
{
|
||||
[XmlRoot("short")]
|
||||
[JsonObject(MemberSerialization.OptIn)]
|
||||
public class Shortie
|
||||
{
|
||||
[JsonProperty("original")]
|
||||
[XmlElement("original")]
|
||||
public String Original { get; set; }
|
||||
|
||||
[JsonProperty("shortened")]
|
||||
[XmlElement("shortened")]
|
||||
public String Shortened { get; set; }
|
||||
|
||||
[JsonProperty("short")]
|
||||
[XmlElement("short")]
|
||||
public String Short { get; set; }
|
||||
|
||||
public string Original { get; set; }
|
||||
public string Shortened { get; set; }
|
||||
public string Short { get; set; }
|
||||
public ShortieException Error { get; set; }
|
||||
}
|
||||
|
||||
public class ShortieException
|
||||
{
|
||||
[JsonProperty("code")]
|
||||
[XmlElement("code")]
|
||||
public int Code { get; set; }
|
||||
|
||||
[JsonProperty("msg")]
|
||||
[XmlElement("msg")]
|
||||
public String ErrorMessage { get; set; }
|
||||
public string ErrorMessage { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Doc", "Doc", "{8F9E13D9-418
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
Doc\AdvanceSerializingJSON.html = Doc\AdvanceSerializingJSON.html
|
||||
Doc\custom.css = Doc\custom.css
|
||||
Doc\CustomCreationConverter.html = Doc\CustomCreationConverter.html
|
||||
Doc\DatesInJSON.html = Doc\DatesInJSON.html
|
||||
Doc\doc.shfb = Doc\doc.shfb
|
||||
Doc\doc.sitemap = Doc\doc.sitemap
|
||||
|
||||
@@ -44,7 +44,7 @@ namespace Newtonsoft.Json.Converters
|
||||
/// <param name="serializer">The calling serializer.</param>
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
serializer.Serialize(writer, value);
|
||||
throw new NotSupportedException("CustomCreationConverter should only be used while deserializing.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user