-Updated serializing help page

-Fixed help generation warnings
-Added AsJEnumerable
-Changed extension methods to return IJEnumerable where appropriate
-Extension methods better handle converting JValues
This commit is contained in:
JamesNK
2008-08-23 12:46:28 +00:00
parent c7d99fbd3f
commit e1cd4a5cff
9 changed files with 239 additions and 58 deletions
+30 -15
View File
@@ -93,24 +93,32 @@
<p>A collection of JsonConverters that will be used during serialization and
deserialization.</p>
<h3>Serialization Attributes</h3>
<p>There are a number of optional attributes that can be placed on a class to control how it is serialized.</p>
<p>There are a number of attributes that can be placed on a class to control how it is serialized.</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;">JsonObject</span>(<span style="color: #2b91af;">MemberSerialization</span>.OptIn)]</pre>
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">Person</span></pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: #2b91af;">JsonProperty</span>]</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Name { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: #2b91af;">JsonProperty</span>]</pre>
<pre style="margin: 0px;">&nbsp; <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;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// not serialized</span></pre>
<pre style="margin: 0px;">&nbsp; <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;">}</pre>
</div>
<div style="font-family: Courier New; font-size: 10pt; color: black;">
<pre style="margin: 0px;">[<span style="color: #2b91af;">JsonObject</span>(<span style="color: #2b91af;">MemberSerialization</span>.OptIn)]</pre>
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">Person</span></pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// "John Smith"</span></pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: #2b91af;">JsonProperty</span>]</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Name { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// "2000-12-15T22:11:03"</span></pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: #2b91af;">JsonProperty</span>]</pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: #2b91af;">JsonConverter</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">IsoDateTimeConverter</span>))]</pre>
<pre style="margin: 0px;">&nbsp; <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;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// new Date(976918263055)</span></pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: #2b91af;">JsonProperty</span>]</pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: #2b91af;">JsonConverter</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">JavaScriptDateTimeConverter</span>))]</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">DateTime</span> LastModified { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// not serialized</span></pre>
<pre style="margin: 0px;">&nbsp; <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;">}</pre>
</div>
</div></div>
@@ -120,6 +128,13 @@
<p>Allows the name of the serialized member to be customized. The attribute also indicates that a member should be serialized when member serialization is set to opt-in.</p>
<h5>JsonIgnoreAttribute</h5>
<p>Excludes a field or property from serialization.</p>
<h5>JsonConverterAttribute</h5>
<p>The JsonConverterAttribute specifies which JsonSerializer is used to convert an object.</p>
<p>The attribute can be placed on a class or a member. When placed on a class the JsonConverter
specified by the attribute will be the default way of serializing that class. When the attribute is on a field
or property then the specified JsonConverter will always be used to serialize that value.</p>
<p>The priority of which JsonConverter is used is member attribute then class attribute and
finally any converters passed to the JsonSerializer.</p>
<h3>JsonConverters</h3>
<p>JsonConverters allows JSON to be manually be written during serialization and read during deserialization. This is useful for particularly complex JSON structures or for when you want to change how a type is serialized.</p>
<p>To create your own custom converter inherit from the JsonConverter class. Json.NET also comes with a number of JsonConverters:</p>
@@ -195,12 +195,20 @@ namespace Newtonsoft.Json.Tests
[JsonObject(MemberSerialization.OptIn)]
public class Person
{
// "John Smith"
[JsonProperty]
public string Name { get; set; }
// "2000-12-15T22:11:03"
[JsonProperty]
[JsonConverter(typeof(IsoDateTimeConverter))]
public DateTime BirthDate { get; set; }
// new Date(976918263055)
[JsonProperty]
[JsonConverter(typeof(JavaScriptDateTimeConverter))]
public DateTime LastModified { get; set; }
// not serialized
public string Department { get; set; }
}
@@ -514,6 +514,10 @@ keyword such as type of business.""
Assert.AreEqual(2, o["channel"]["item"].Children()["title"].Count());
Assert.AreEqual(0, o["channel"]["item"].Children()["monkey"].Count());
Assert.AreEqual("Json.NET 1.3 + New license + Now on CodePlex", (string)o["channel"]["item"][0]["title"]);
CollectionAssert.AreEqual(new string[] { "Json.NET 1.3 + New license + Now on CodePlex", "LINQ to JSON beta" }, o["channel"]["item"].Children().Values<string>("title").ToArray());
}
[Test]
@@ -665,5 +669,101 @@ keyword such as type of business.""
Assert.IsInstanceOfType(typeof(JArray), a);
Assert.AreEqual(5, a.Count());
}
[Test]
public void AsJEnumerable()
{
JObject o = null;
IJEnumerable<JToken> enumerable = null;
enumerable = o.AsJEnumerable();
Assert.IsNull(enumerable);
o =
new JObject(
new JProperty("Test1", new DateTime(2000, 10, 15, 5, 5, 5, DateTimeKind.Utc)),
new JProperty("Test2", new DateTimeOffset(2000, 10, 15, 5, 5, 5, new TimeSpan(11, 11, 0))),
new JProperty("Test3", "Test3Value"),
new JProperty("Test4", null)
);
enumerable = o.AsJEnumerable();
Assert.IsNotNull(enumerable);
Assert.AreEqual(o, enumerable);
DateTime d = enumerable["Test1"].Value<DateTime>();
Assert.AreEqual(new DateTime(2000, 10, 15, 5, 5, 5, DateTimeKind.Utc), d);
}
[Test]
public void ChildrenExtension()
{
string json = @"[
{
""title"": ""James Newton-King"",
""link"": ""http://james.newtonking.com"",
""description"": ""James Newton-King's blog."",
""item"": [
{
""title"": ""Json.NET 1.3 + New license + Now on CodePlex"",
""description"": ""Annoucing the release of Json.NET 1.3, the MIT license and the source being available on CodePlex"",
""link"": ""http://james.newtonking.com/projects/json-net.aspx"",
""category"": [
""Json.NET"",
""CodePlex""
]
},
{
""title"": ""LINQ to JSON beta"",
""description"": ""Annoucing LINQ to JSON"",
""link"": ""http://james.newtonking.com/projects/json-net.aspx"",
""category"": [
""Json.NET"",
""LINQ""
]
}
]
},
{
""title"": ""James Newton-King"",
""link"": ""http://james.newtonking.com"",
""description"": ""James Newton-King's blog."",
""item"": [
{
""title"": ""Json.NET 1.3 + New license + Now on CodePlex"",
""description"": ""Annoucing the release of Json.NET 1.3, the MIT license and the source being available on CodePlex"",
""link"": ""http://james.newtonking.com/projects/json-net.aspx"",
""category"": [
""Json.NET"",
""CodePlex""
]
},
{
""title"": ""LINQ to JSON beta"",
""description"": ""Annoucing LINQ to JSON"",
""link"": ""http://james.newtonking.com/projects/json-net.aspx"",
""category"": [
""Json.NET"",
""LINQ""
]
}
]
}
]";
JArray o = JArray.Parse(json);
Assert.AreEqual(4, o.Children()["item"].Children()["title"].Count());
CollectionAssert.AreEqual(new string[]
{
"Json.NET 1.3 + New license + Now on CodePlex",
"LINQ to JSON beta",
"Json.NET 1.3 + New license + Now on CodePlex",
"LINQ to JSON beta"
},
o.Children()["item"].Children()["title"].Values<string>().ToArray());
}
}
}
+1 -1
View File
@@ -795,7 +795,7 @@ namespace Newtonsoft.Json
}
/// <summary>
/// Changes the <see cref="P:CurrentState"/> to Closed.
/// Changes the state to closed.
/// </summary>
public override void Close()
{
+2 -2
View File
@@ -72,7 +72,7 @@ namespace Newtonsoft.Json
}
/// <summary>
/// Specifies formatting options for the <see cref="JsonWriter"/>.
/// Specifies formatting options for the <see cref="JsonTextWriter"/>.
/// </summary>
public enum Formatting
{
@@ -81,7 +81,7 @@ namespace Newtonsoft.Json
/// </summary>
None,
/// <summary>
/// Causes child objects to be indented according to the <see cref="P:JsonWriter.Indentation"/> and <see cref="P:JsonWriter.IndentChar"/> settings.
/// Causes child objects to be indented according to the <see cref="JsonTextWriter.Indentation"/> and <see cref="JsonTextWriter.IndentChar"/> settings.
/// </summary>
Indented
}
+83 -33
View File
@@ -44,11 +44,11 @@ namespace Newtonsoft.Json.Linq
/// <typeparam name="T">The type of the objects in source, constrained to <see cref="JToken"/>.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the ancestors of every node in the source collection.</returns>
public static IEnumerable<JToken> Ancestors<T>(this IEnumerable<T> source) where T : JToken
public static IJEnumerable<JToken> Ancestors<T>(this IEnumerable<T> source) where T : JToken
{
ValidationUtils.ArgumentNotNull(source, "source");
return source.SelectMany(j => j.Ancestors());
return source.SelectMany(j => j.Ancestors()).AsJEnumerable();
}
//TODO
@@ -65,11 +65,11 @@ namespace Newtonsoft.Json.Linq
/// <typeparam name="T">The type of the objects in source, constrained to <see cref="JContainer"/>.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the descendants of every node in the source collection.</returns>
public static IEnumerable<JToken> Descendants<T>(this IEnumerable<T> source) where T : JContainer
public static IJEnumerable<JToken> Descendants<T>(this IEnumerable<T> source) where T : JContainer
{
ValidationUtils.ArgumentNotNull(source, "source");
return source.SelectMany(j => j.Descendants());
return source.SelectMany(j => j.Descendants()).AsJEnumerable();
}
//TODO
@@ -85,11 +85,11 @@ namespace Newtonsoft.Json.Linq
/// </summary>
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JObject"/> that contains the source collection.</param>
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JProperty"/> that contains the properties of every object in the source collection.</returns>
public static IEnumerable<JProperty> Properties(this IEnumerable<JObject> source)
public static IJEnumerable<JProperty> Properties(this IEnumerable<JObject> source)
{
ValidationUtils.ArgumentNotNull(source, "source");
return source.SelectMany(d => d.Properties());
return source.SelectMany(d => d.Properties()).AsJEnumerable();
}
/// <summary>
@@ -98,9 +98,9 @@ namespace Newtonsoft.Json.Linq
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
/// <param name="key">The token key.</param>
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the values of every node in the source collection with the given key.</returns>
public static IEnumerable<JToken> Values(this IEnumerable<JToken> source, object key)
public static IJEnumerable<JToken> Values(this IEnumerable<JToken> source, object key)
{
return Values<JToken, JToken>(source, key);
return Values<JToken, JToken>(source, key).AsJEnumerable();
}
/// <summary>
@@ -108,9 +108,9 @@ namespace Newtonsoft.Json.Linq
/// </summary>
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the values of every node in the source collection.</returns>
public static IEnumerable<JToken> Values(this IEnumerable<JToken> source)
public static IJEnumerable<JToken> Values(this IEnumerable<JToken> source)
{
return Values<JToken, JToken>(source, null);
return source.Values(null);
}
/// <summary>
@@ -136,10 +136,35 @@ namespace Newtonsoft.Json.Linq
return Values<JToken, U>(source, null);
}
//public static IEnumerable<U> Values<U>(this IEnumerable<JToken> source, object key)
//{
// return Values<JToken, U>(source, null);
//}
/// <summary>
/// Converts the value.
/// </summary>
/// <typeparam name="U">The type to convert the value to.</typeparam>
/// <param name="value">A <see cref="JToken"/> cast as a <see cref="IEnumerable{T}"/> of <see cref="JToken"/>.</param>
/// <returns>A converted value.</returns>
public static U Value<U>(this IEnumerable<JToken> value)
{
return value.Value<JToken, U>();
}
/// <summary>
/// Converts the value.
/// </summary>
/// <typeparam name="T">The source collection type.</typeparam>
/// <typeparam name="U">The type to convert the value to.</typeparam>
/// <param name="value">A <see cref="JToken"/> cast as a <see cref="IEnumerable{T}"/> of <see cref="JToken"/>.</param>
/// <returns>A converted value.</returns>
public static U Value<T, U>(this IEnumerable<T> value) where T : JToken
{
ValidationUtils.ArgumentNotNull(value, "source");
JToken token = value as JToken;
if (token == null)
throw new ArgumentException("Source value must be a JToken.");
return token.Convert<JToken, U>();
}
internal static IEnumerable<U> Values<T, U>(this IEnumerable<T> source, object key) where T : JToken
{
@@ -149,9 +174,16 @@ namespace Newtonsoft.Json.Linq
{
if (key == null)
{
foreach (JToken t in token.Children())
if (token is JValue)
{
yield return t.Convert<JToken, U>(); ;
yield return Convert<JValue, U>((JValue)token);
}
else
{
foreach (JToken t in token.Children())
{
yield return t.Convert<JToken, U>(); ;
}
}
}
else
@@ -163,17 +195,6 @@ namespace Newtonsoft.Json.Linq
}
yield break;
//IEnumerable<JToken> allChildren = source.SelectMany(d => d.Children());
//IEnumerable<JToken> childrenValues = (key != null) ? allChildren.Select(p => p[key]) : allChildren.SelectMany(p => p.Children());
//List<U> s = childrenValues.Convert<JToken, U>().ToList();
//return childrenValues.Convert<JToken, U>();
// return source.SelectMany(d => d.Properties())
//.Where(p => string.Compare(p.Name, name, StringComparison.Ordinal) == 0)
//.Select(p => p.Value)
//.Convert<JToken, T>();
}
//TODO
@@ -189,19 +210,22 @@ namespace Newtonsoft.Json.Linq
/// <summary>
/// Returns a collection of child tokens of every array in the source collection.
/// </summary>
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JArray"/> that contains the source collection.</param>
/// <typeparam name="T">The source collection type.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the values of every node in the source collection.</returns>
public static IEnumerable<JToken> Children(this IEnumerable<JArray> source)
public static IJEnumerable<JToken> Children<T>(this IEnumerable<T> source) where T : JToken
{
return Children<JToken>(source);
return Children<T, JToken>(source).AsJEnumerable();
}
/// <summary>
/// Returns a collection of converted child tokens of every array in the source collection.
/// </summary>
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JArray"/> that contains the source collection.</param>
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the converted values of every node in the source collection.</returns>
public static IEnumerable<U> Children<U>(this IEnumerable<JArray> source)
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
/// <typeparam name="U">The type to convert the values to.</typeparam>
/// <typeparam name="T">The source collection type.</typeparam>
/// <returns>An <see cref="IEnumerable{T}"/> that contains the converted values of every node in the source collection.</returns>
public static IEnumerable<U> Children<T, U>(this IEnumerable<T> source) where T : JToken
{
ValidationUtils.ArgumentNotNull(source, "source");
@@ -249,5 +273,31 @@ namespace Newtonsoft.Json.Linq
//TODO
//public static void Remove<T>(this IEnumerable<T> source) where T : JContainer;
/// <summary>
/// Returns the input typed as <see cref="IJEnumerable{T}"/>.
/// </summary>
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
/// <returns>The input typed as <see cref="IJEnumerable{T}"/>.</returns>
public static IJEnumerable<JToken> AsJEnumerable(this IEnumerable<JToken> source)
{
return source.AsJEnumerable<JToken>();
}
/// <summary>
/// Returns the input typed as <see cref="IJEnumerable{T}"/>.
/// </summary>
/// <typeparam name="T">The source collection type.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
/// <returns>The input typed as <see cref="IJEnumerable{T}"/>.</returns>
public static IJEnumerable<T> AsJEnumerable<T>(this IEnumerable<T> source) where T : JToken
{
if (source == null)
return null;
else if (source is IJEnumerable<T>)
return (IJEnumerable<T>)source;
else
return new JEnumerable<T>(source);
}
}
}
+1 -1
View File
@@ -162,7 +162,7 @@ namespace Newtonsoft.Json.Linq
/// <summary>
/// Returns a collection of the child values of this token, in document order.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="T">The type to convert the values to.</typeparam>
/// <returns>
/// A <see cref="IEnumerable{T}"/> containing the child values of this <see cref="JToken"/>, in document order.
/// </returns>
+3 -3
View File
@@ -281,10 +281,10 @@ namespace Newtonsoft.Json.Linq
}
/// <summary>
/// Removes the specified property name.
/// Removes the property with the specified name.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
/// <returns></returns>
/// <returns>true if item was successfully removed; otherwise, false.</returns>
public bool Remove(string propertyName)
{
JProperty property = Property(propertyName);
@@ -300,7 +300,7 @@ namespace Newtonsoft.Json.Linq
/// </summary>
/// <param name="propertyName">Name of the property.</param>
/// <param name="value">The value.</param>
/// <returns></returns>
/// <returns>true if a value was successfully retrieved; otherwise, false.</returns>
public bool TryGetValue(string propertyName, out JToken value)
{
JProperty property = Property(propertyName);
+11 -3
View File
@@ -246,8 +246,9 @@ namespace Newtonsoft.Json.Linq
}
/// <summary>
/// Returns a collection of the child tokens of this token, in document order.
/// Returns a collection of the child tokens of this token, in document order, filtered by the specified type.
/// </summary>
/// <typeparam name="T">The type to filter the child tokens on.</typeparam>
/// <returns>A <see cref="JEnumerable{T}"/> containing the child tokens of this <see cref="JToken"/>, in document order.</returns>
public JEnumerable<T> Children<T>() where T : JToken
{
@@ -257,6 +258,7 @@ namespace Newtonsoft.Json.Linq
/// <summary>
/// Returns a collection of the child values of this token, in document order.
/// </summary>
/// <typeparam name="T">The type to convert the values to.</typeparam>
/// <returns>A <see cref="IEnumerable{T}"/> containing the child values of this <see cref="JToken"/>, in document order.</returns>
public virtual IEnumerable<T> Values<T>()
{
@@ -712,7 +714,13 @@ namespace Newtonsoft.Json.Linq
}
#endregion
IEnumerator IEnumerable.GetEnumerator()
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>
/// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
/// </returns>
public IEnumerator GetEnumerator()
{
return ((IEnumerable<JToken>)this).GetEnumerator();
}
@@ -726,7 +734,7 @@ namespace Newtonsoft.Json.Linq
IJEnumerable<JToken> IJEnumerable<JToken>.this[object key]
{
get { return Children()[key]; }
get { return this[key]; }
}
}
}