diff --git a/Src/Doc/SerializingJSON.html b/Src/Doc/SerializingJSON.html
index 1a5a746e..6a1ee09e 100644
--- a/Src/Doc/SerializingJSON.html
+++ b/Src/Doc/SerializingJSON.html
@@ -93,24 +93,32 @@
A collection of JsonConverters that will be used during serialization and
deserialization.
Serialization Attributes
- There are a number of optional attributes that can be placed on a class to control how it is serialized.
+ There are a number of attributes that can be placed on a class to control how it is serialized.
-
-
[JsonObject(MemberSerialization.OptIn)]
-
public class Person
-
{
-
[JsonProperty]
-
public string Name { get; set; }
-
-
[JsonProperty]
-
public DateTime BirthDate { get; set; }
-
-
// not serialized
-
public string Department { get; set; }
-
}
-
+
+
[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; }
+
}
+
@@ -120,6 +128,13 @@
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.
JsonIgnoreAttribute
Excludes a field or property from serialization.
+ JsonConverterAttribute
+The JsonConverterAttribute specifies which JsonSerializer is used to convert an object.
+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.
+The priority of which JsonConverter is used is member attribute then class attribute and
+finally any converters passed to the JsonSerializer.
JsonConverters
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.
To create your own custom converter inherit from the JsonConverter class. Json.NET also comes with a number of JsonConverters:
diff --git a/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs
index d8e4a73a..ebb534e4 100644
--- a/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs
+++ b/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs
@@ -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; }
}
diff --git a/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs b/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs
index 365dfb54..8b52a5fe 100644
--- a/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs
+++ b/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs
@@ -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("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 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();
+
+ 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().ToArray());
+ }
+
}
}
\ No newline at end of file
diff --git a/Src/Newtonsoft.Json/JsonTextReader.cs b/Src/Newtonsoft.Json/JsonTextReader.cs
index 0d2aac88..5e6bf23b 100644
--- a/Src/Newtonsoft.Json/JsonTextReader.cs
+++ b/Src/Newtonsoft.Json/JsonTextReader.cs
@@ -795,7 +795,7 @@ namespace Newtonsoft.Json
}
///
- /// Changes the to Closed.
+ /// Changes the state to closed.
///
public override void Close()
{
diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs
index b727933a..552e5dbc 100644
--- a/Src/Newtonsoft.Json/JsonWriter.cs
+++ b/Src/Newtonsoft.Json/JsonWriter.cs
@@ -72,7 +72,7 @@ namespace Newtonsoft.Json
}
///
- /// Specifies formatting options for the .
+ /// Specifies formatting options for the .
///
public enum Formatting
{
@@ -81,7 +81,7 @@ namespace Newtonsoft.Json
///
None,
///
- /// Causes child objects to be indented according to the and settings.
+ /// Causes child objects to be indented according to the and settings.
///
Indented
}
diff --git a/Src/Newtonsoft.Json/Linq/Extensions.cs b/Src/Newtonsoft.Json/Linq/Extensions.cs
index 555be12f..11cd62ec 100644
--- a/Src/Newtonsoft.Json/Linq/Extensions.cs
+++ b/Src/Newtonsoft.Json/Linq/Extensions.cs
@@ -44,11 +44,11 @@ namespace Newtonsoft.Json.Linq
/// The type of the objects in source, constrained to .
/// An of that contains the source collection.
/// An of that contains the ancestors of every node in the source collection.
- public static IEnumerable Ancestors(this IEnumerable source) where T : JToken
+ public static IJEnumerable Ancestors(this IEnumerable 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
/// The type of the objects in source, constrained to .
/// An of that contains the source collection.
/// An of that contains the descendants of every node in the source collection.
- public static IEnumerable Descendants(this IEnumerable source) where T : JContainer
+ public static IJEnumerable Descendants(this IEnumerable 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
///
/// An of that contains the source collection.
/// An of that contains the properties of every object in the source collection.
- public static IEnumerable Properties(this IEnumerable source)
+ public static IJEnumerable Properties(this IEnumerable source)
{
ValidationUtils.ArgumentNotNull(source, "source");
- return source.SelectMany(d => d.Properties());
+ return source.SelectMany(d => d.Properties()).AsJEnumerable();
}
///
@@ -98,9 +98,9 @@ namespace Newtonsoft.Json.Linq
/// An of that contains the source collection.
/// The token key.
/// An of that contains the values of every node in the source collection with the given key.
- public static IEnumerable Values(this IEnumerable source, object key)
+ public static IJEnumerable Values(this IEnumerable source, object key)
{
- return Values(source, key);
+ return Values(source, key).AsJEnumerable();
}
///
@@ -108,9 +108,9 @@ namespace Newtonsoft.Json.Linq
///
/// An of that contains the source collection.
/// An of that contains the values of every node in the source collection.
- public static IEnumerable Values(this IEnumerable source)
+ public static IJEnumerable Values(this IEnumerable source)
{
- return Values(source, null);
+ return source.Values(null);
}
///
@@ -136,10 +136,35 @@ namespace Newtonsoft.Json.Linq
return Values(source, null);
}
- //public static IEnumerable Values(this IEnumerable source, object key)
- //{
- // return Values(source, null);
- //}
+ ///
+ /// Converts the value.
+ ///
+ /// The type to convert the value to.
+ /// A cast as a of .
+ /// A converted value.
+ public static U Value(this IEnumerable value)
+ {
+ return value.Value();
+ }
+
+ ///
+ /// Converts the value.
+ ///
+ /// The source collection type.
+ /// The type to convert the value to.
+ /// A cast as a of .
+ /// A converted value.
+ public static U Value(this IEnumerable 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();
+ }
+
internal static IEnumerable Values(this IEnumerable 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(); ;
+ yield return Convert((JValue)token);
+ }
+ else
+ {
+ foreach (JToken t in token.Children())
+ {
+ yield return t.Convert(); ;
+ }
}
}
else
@@ -163,17 +195,6 @@ namespace Newtonsoft.Json.Linq
}
yield break;
-
- //IEnumerable allChildren = source.SelectMany(d => d.Children());
- //IEnumerable childrenValues = (key != null) ? allChildren.Select(p => p[key]) : allChildren.SelectMany(p => p.Children());
-
- //List s = childrenValues.Convert().ToList();
- //return childrenValues.Convert();
-
- // return source.SelectMany(d => d.Properties())
- //.Where(p => string.Compare(p.Name, name, StringComparison.Ordinal) == 0)
- //.Select(p => p.Value)
- //.Convert();
}
//TODO
@@ -189,19 +210,22 @@ namespace Newtonsoft.Json.Linq
///
/// Returns a collection of child tokens of every array in the source collection.
///
- /// An of that contains the source collection.
+ /// The source collection type.
+ /// An of that contains the source collection.
/// An of that contains the values of every node in the source collection.
- public static IEnumerable Children(this IEnumerable source)
+ public static IJEnumerable Children(this IEnumerable source) where T : JToken
{
- return Children(source);
+ return Children(source).AsJEnumerable();
}
///
/// Returns a collection of converted child tokens of every array in the source collection.
///
- /// An of that contains the source collection.
- /// An of that contains the converted values of every node in the source collection.
- public static IEnumerable Children(this IEnumerable source)
+ /// An of that contains the source collection.
+ /// The type to convert the values to.
+ /// The source collection type.
+ /// An that contains the converted values of every node in the source collection.
+ public static IEnumerable Children(this IEnumerable source) where T : JToken
{
ValidationUtils.ArgumentNotNull(source, "source");
@@ -249,5 +273,31 @@ namespace Newtonsoft.Json.Linq
//TODO
//public static void Remove(this IEnumerable source) where T : JContainer;
+
+ ///
+ /// Returns the input typed as .
+ ///
+ /// An of that contains the source collection.
+ /// The input typed as .
+ public static IJEnumerable AsJEnumerable(this IEnumerable source)
+ {
+ return source.AsJEnumerable();
+ }
+
+ ///
+ /// Returns the input typed as .
+ ///
+ /// The source collection type.
+ /// An of that contains the source collection.
+ /// The input typed as .
+ public static IJEnumerable AsJEnumerable(this IEnumerable source) where T : JToken
+ {
+ if (source == null)
+ return null;
+ else if (source is IJEnumerable)
+ return (IJEnumerable)source;
+ else
+ return new JEnumerable(source);
+ }
}
}
\ No newline at end of file
diff --git a/Src/Newtonsoft.Json/Linq/JContainer.cs b/Src/Newtonsoft.Json/Linq/JContainer.cs
index d439845b..48b759fc 100644
--- a/Src/Newtonsoft.Json/Linq/JContainer.cs
+++ b/Src/Newtonsoft.Json/Linq/JContainer.cs
@@ -162,7 +162,7 @@ namespace Newtonsoft.Json.Linq
///
/// Returns a collection of the child values of this token, in document order.
///
- ///
+ /// The type to convert the values to.
///
/// A containing the child values of this , in document order.
///
diff --git a/Src/Newtonsoft.Json/Linq/JObject.cs b/Src/Newtonsoft.Json/Linq/JObject.cs
index 6ee5572e..0ec0aa40 100644
--- a/Src/Newtonsoft.Json/Linq/JObject.cs
+++ b/Src/Newtonsoft.Json/Linq/JObject.cs
@@ -281,10 +281,10 @@ namespace Newtonsoft.Json.Linq
}
///
- /// Removes the specified property name.
+ /// Removes the property with the specified name.
///
/// Name of the property.
- ///
+ /// true if item was successfully removed; otherwise, false.
public bool Remove(string propertyName)
{
JProperty property = Property(propertyName);
@@ -300,7 +300,7 @@ namespace Newtonsoft.Json.Linq
///
/// Name of the property.
/// The value.
- ///
+ /// true if a value was successfully retrieved; otherwise, false.
public bool TryGetValue(string propertyName, out JToken value)
{
JProperty property = Property(propertyName);
diff --git a/Src/Newtonsoft.Json/Linq/JToken.cs b/Src/Newtonsoft.Json/Linq/JToken.cs
index 085cc193..5c46f07b 100644
--- a/Src/Newtonsoft.Json/Linq/JToken.cs
+++ b/Src/Newtonsoft.Json/Linq/JToken.cs
@@ -246,8 +246,9 @@ namespace Newtonsoft.Json.Linq
}
///
- /// 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.
///
+ /// The type to filter the child tokens on.
/// A containing the child tokens of this , in document order.
public JEnumerable Children() where T : JToken
{
@@ -257,6 +258,7 @@ namespace Newtonsoft.Json.Linq
///
/// Returns a collection of the child values of this token, in document order.
///
+ /// The type to convert the values to.
/// A containing the child values of this , in document order.
public virtual IEnumerable Values()
{
@@ -712,7 +714,13 @@ namespace Newtonsoft.Json.Linq
}
#endregion
- IEnumerator IEnumerable.GetEnumerator()
+ ///
+ /// Returns an enumerator that iterates through a collection.
+ ///
+ ///
+ /// An object that can be used to iterate through the collection.
+ ///
+ public IEnumerator GetEnumerator()
{
return ((IEnumerable)this).GetEnumerator();
}
@@ -726,7 +734,7 @@ namespace Newtonsoft.Json.Linq
IJEnumerable IJEnumerable.this[object key]
{
- get { return Children()[key]; }
+ get { return this[key]; }
}
}
}
\ No newline at end of file