Commits

Anonymous committed aa93149

Moved new extension methods into Extensions namespace and made public.
Added documentation for new methods.

Comments (0)

Files changed (10)

Json.Tests/Json/JsonArrayTest.cs

 			Assert.AreEqual(expected, actual);
 		}
 		[TestMethod]
+		public void Parse_EmptyArray_ReturnsEmptyArray()
+		{
+			var s = "[]";
+			var expected = new JsonArray();
+			var i = 0;
+			var actual = new JsonArray(s, ref i);
+			Assert.AreEqual(expected, actual);
+		}
+		[TestMethod]
 		[ExpectedException(typeof(JsonSyntaxException))]
 		public void Parse_StringMissingValue_ThrowsJsonSyntaxException()
 		{

Json.Tests/Json/JsonObjectTest.cs

 			Assert.AreEqual(expected, actual);
 		}
 		[TestMethod]
+		public void Parse_EmptyObject_ReturnsEmptyObject()
+		{
+			var s = "{}";
+			var expected = new JsonObject();
+			var i = 0;
+			var actual = new JsonObject(s, ref i);
+			Assert.AreEqual(expected, actual);
+		}
+		[TestMethod]
 		[ExpectedException(typeof(JsonSyntaxException))]
 		public void Parse_StringMissingValue_ThrowsJsonSyntaxException()
 		{

Json.Tests/Manatee.Tests.csproj

       <Name>Manatee.Json</Name>
     </ProjectReference>
   </ItemGroup>
-  <ItemGroup>
-    <Shadow Include="Test References\Manatee.Json.accessor" />
-  </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
        Other similar extension points exist, see Microsoft.Common.targets.

Json.Tests/Test References/Manatee.Json.accessor

-Manatee.Json.dll
-Desktop

Json.suo

Binary file modified.

Json/Extensions/JsonExtensions.cs

+/***************************************************************************************
+
+	Copyright 2013 Little Crab Solutions
+
+	   Licensed under the Apache License, Version 2.0 (the "License");
+	   you may not use this file except in compliance with the License.
+	   You may obtain a copy of the License at
+
+		 http://www.apache.org/licenses/LICENSE-2.0
+
+	   Unless required by applicable law or agreed to in writing, software
+	   distributed under the License is distributed on an "AS IS" BASIS,
+	   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+	   See the License for the specific language governing permissions and
+	   limitations under the License.
+ 
+	File Name:		JsonExtensions.cs
+	Namespace:		Manatee.Json
+	Class Name:		JsonExtensions
+	Purpose:		Provides extension methods for JsonObjects.
+
+***************************************************************************************/
+using Manatee.Json.Enumerations;
+
+namespace Manatee.Json.Extensions
+{
+	/// <summary>
+	/// Provides extension methods for JsonValues.
+	/// </summary>
+	public static class JsonExtensions
+	{
+		/// <summary>
+		/// Returns a string or null if the key is not found or is not a string.
+		/// </summary>
+		/// <param name="obj">The object to search</param>
+		/// <param name="key">The key</param>
+		/// <returns>A string or null if the key is not found or is not a string</returns>
+		public static string TryGetString(this JsonObject obj, string key)
+		{
+			return obj.ContainsKey(key) && (obj[key].Type == JsonValueType.String) ? obj[key].String : null;
+		}
+		/// <summary>
+		/// Returns a double or null if the key is not found or is not a string.
+		/// </summary>
+		/// <param name="obj">The object to search</param>
+		/// <param name="key">The key</param>
+		/// <returns>A double or null if the key is not found or is not a string</returns>
+		public static double? TryGetNumber(this JsonObject obj, string key)
+		{
+			return obj.ContainsKey(key) && (obj[key].Type == JsonValueType.Number) ? obj[key].Number : (double?) null;
+		}
+		/// <summary>
+		/// Returns a bool or null if the key is not found or is not a string.
+		/// </summary>
+		/// <param name="obj">The object to search</param>
+		/// <param name="key">The key</param>
+		/// <returns>A bool or null if the key is not found or is not a string</returns>
+		public static bool? TryGetBoolean(this JsonObject obj, string key)
+		{
+			return obj.ContainsKey(key) && (obj[key].Type != JsonValueType.Null) ? obj[key].Boolean : (bool?) null;
+		}
+		/// <summary>
+		/// Returns a JsonArray or null if the key is not found or is not a string.
+		/// </summary>
+		/// <param name="obj">The object to search</param>
+		/// <param name="key">The key</param>
+		/// <returns>A JsonArray or null if the key is not found or is not a string</returns>
+		public static JsonArray TryGetArray(this JsonObject obj, string key)
+		{
+			return obj.ContainsKey(key) && (obj[key].Type != JsonValueType.Null) ? obj[key].Array : null;
+		}
+		/// <summary>
+		/// Returns a JsonObject or null if the key is not found or is not a string.
+		/// </summary>
+		/// <param name="obj">The object to search</param>
+		/// <param name="key">The key</param>
+		/// <returns>A JsonObject or null if the key is not found or is not a string</returns>
+		public static JsonObject TryGetObject(this JsonObject obj, string key)
+		{
+			return obj.ContainsKey(key) && (obj[key].Type != JsonValueType.Null) ? obj[key].Object : null;
+		}
+	}
+}

Json/Extensions/LinqExtensions.cs

 
 ***************************************************************************************/
 using System.Collections.Generic;
+using System.Linq;
+using Manatee.Json.Serialization;
 
 namespace Manatee.Json.Extensions
 {
 			}
 			return json;
 		}
+		/// <summary>
+		/// Converts a collection of strings to a JsonArray.
+		/// </summary>
+		/// <param name="list">A collection of strings</param>
+		/// <returns>A JsonArray containing the strings</returns>
+		public static JsonValue ToJson(this IEnumerable<string> list)
+		{
+			if (list == null) return JsonValue.Null;
+			var json = new JsonArray();
+			json.AddRange(list.Select(j => new JsonValue(j)));
+			return json;
+		}
+		/// <summary>
+		/// Converts a collection of bools to a JsonArray.
+		/// </summary>
+		/// <param name="list">A collection of booleans</param>
+		/// <returns>A JsonArray containing the booleans</returns>
+		public static JsonValue ToJson(this IEnumerable<bool> list)
+		{
+			if (list == null) return JsonValue.Null;
+			var json = new JsonArray();
+			json.AddRange(list.Select(j => new JsonValue(j)));
+			return json;
+		}
+		/// <summary>
+		/// Converts a collection of Nullable&lt;bool&gt; to a JsonArray.
+		/// </summary>
+		/// <param name="list">A collection of Nullable&lt;bool&gt;</param>
+		/// <returns>A JsonArray containing the Nullable&lt;bool&gt;</returns>
+		public static JsonValue ToJson(this IEnumerable<bool?> list)
+		{
+			if (list == null) return JsonValue.Null;
+			var json = new JsonArray();
+			json.AddRange(list.Select(b => b.HasValue ? new JsonValue(b.Value) : JsonValue.Null));
+			return json;
+		}
+		/// <summary>
+		/// Converts a collection of JsonArrays to a JsonArray.
+		/// </summary>
+		/// <param name="list">A collection of JsonArrays</param>
+		/// <returns>A JsonArray containing the JsonArrays</returns>
+		public static JsonValue ToJson(this IEnumerable<JsonArray> list)
+		{
+			if (list == null) return JsonValue.Null;
+			var json = new JsonArray();
+			json.AddRange(list.Select(j => new JsonValue(j)));
+			return json;
+		}
+		/// <summary>
+		/// Converts a collection of JsonObjects to a JsonArray.
+		/// </summary>
+		/// <param name="list">A collection of JsonObjects</param>
+		/// <returns>A JsonArray containing the JsonObjects</returns>
+		public static JsonValue ToJson(this IEnumerable<JsonObject> list)
+		{
+			if (list == null) return JsonValue.Null;
+			var json = new JsonArray();
+			json.AddRange(list.Select(j => new JsonValue(j)));
+			return json;
+		}
+		/// <summary>
+		/// Converts a collection of doubles to a JsonArray.
+		/// </summary>
+		/// <param name="list">A collection of doubles</param>
+		/// <returns>A JsonArray containing the doubles</returns>
+		public static JsonValue ToJson(this IEnumerable<double> list)
+		{
+			if (list == null) return JsonValue.Null;
+			var json = new JsonArray();
+			json.AddRange(list.Select(j => new JsonValue(j)));
+			return json;
+		}
+		/// <summary>
+		/// Serializes a collection of objects which implement IJsonCompatible to a JsonArray of equivalent JsonValues.
+		/// </summary>
+		/// <param name="list">A collection of equivalent JsonValues</param>
+		/// <returns>A JsonArray containing the equivalent JsonValues</returns>
+		public static JsonValue ToJson(this IEnumerable<IJsonCompatible> list)
+		{
+			if (list == null) return JsonValue.Null;
+			var json = new JsonArray();
+			json.AddRange(list.Select(j => j == null ? JsonValue.Null : j.ToJson()));
+			return json;
+		}
+		/// <summary>
+		/// Deserializes a collection of JsonValues to an IEnumerable of the objects.
+		/// </summary>
+		/// <typeparam name="T">The type of object contained in the collection</typeparam>
+		/// <param name="json">The collection of JsonValues</param>
+		/// <returns>A collection of the deserialized objects</returns>
+		public static IEnumerable<T> FromJson<T>(this IEnumerable<JsonValue> json) where T : IJsonCompatible, new()
+		{
+			if (json == null) return null;
+			var list = new List<T>();
+			foreach (var value in json)
+			{
+				T item = new T();
+				item.FromJson(value);
+				list.Add(item);
+			}
+			return list;
+		}
+		/// <summary>
+		/// Deserializes a JsonValue to its equivalent object.
+		/// </summary>
+		/// <typeparam name="T">The type of object</typeparam>
+		/// <param name="json">The JsonValue to deserialize</param>
+		/// <returns>A collection of the deserialized objects</returns>
+		public static T FromJson<T>(this JsonObject json) where T : IJsonCompatible, new()
+		{
+			if (json == null) return default(T);
+			T obj = new T();
+			obj.FromJson(json);
+			return obj;
+		}
 	}
 }

Json/JsonExtensions.cs

-/***************************************************************************************
-
-	Copyright 2013 Little Crab Solutions
-
-	   Licensed under the Apache License, Version 2.0 (the "License");
-	   you may not use this file except in compliance with the License.
-	   You may obtain a copy of the License at
-
-		 http://www.apache.org/licenses/LICENSE-2.0
-
-	   Unless required by applicable law or agreed to in writing, software
-	   distributed under the License is distributed on an "AS IS" BASIS,
-	   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-	   See the License for the specific language governing permissions and
-	   limitations under the License.
- 
-	File Name:		JsonExtensions.cs
-	Namespace:		Manatee.Json
-	Class Name:		JsonExtensions
-	Purpose:		Provides extension methods for Manatee.Json.
-
-***************************************************************************************/
-using System.Collections.Generic;
-using System.Linq;
-using Manatee.Json.Enumerations;
-using Manatee.Json.Serialization;
-
-namespace Manatee.Json
-{
-	internal static class JsonExtensions
-	{
-
-		#region Try Get
-
-		public static string TryGetString(this JsonObject obj, string key)
-		{
-			return obj.ContainsKey(key) && (obj[key].Type != JsonValueType.Null) ? obj[key].String : null;
-		}
-		public static double? TryGetNumber(this JsonObject obj, string key)
-		{
-			return obj.ContainsKey(key) && (obj[key].Type != JsonValueType.Null) ? obj[key].Number : (double?) null;
-		}
-		public static bool? TryGetBoolean(this JsonObject obj, string key)
-		{
-			return obj.ContainsKey(key) && (obj[key].Type != JsonValueType.Null) ? obj[key].Boolean : (bool?) null;
-		}
-		public static JsonArray TryGetArray(this JsonObject obj, string key)
-		{
-			return obj.ContainsKey(key) && (obj[key].Type != JsonValueType.Null) ? obj[key].Array : null;
-		}
-		public static JsonObject TryGetObject(this JsonObject obj, string key)
-		{
-			return obj.ContainsKey(key) && (obj[key].Type != JsonValueType.Null) ? obj[key].Object : null;
-		}
-
-		#endregion
-
-		#region From JSON
-
-		public static IEnumerable<T> FromJson<T>(this IEnumerable<JsonValue> json) where T : IJsonCompatible, new()
-		{
-			if (json == null) return null;
-			var list = new List<T>();
-			foreach (var value in json)
-			{
-				T item = new T();
-				item.FromJson(value);
-				list.Add(item);
-			}
-			return list;
-		}
-		public static T FromJson<T>(this JsonObject json) where T : IJsonCompatible, new()
-		{
-			if (json == null) return default(T);
-			T obj = new T();
-			obj.FromJson(json);
-			return obj;
-		}
-
-		#endregion
-
-		#region To JSON
-
-		public static JsonValue ToJson(this IEnumerable<string> list)
-		{
-			if (list == null) return JsonValue.Null;
-			var json = new JsonArray();
-			json.AddRange(list.Select(j => new JsonValue(j)));
-			return json;
-		}
-		public static JsonValue ToJson(this IEnumerable<bool> list)
-		{
-			if (list == null) return JsonValue.Null;
-			var json = new JsonArray();
-			json.AddRange(list.Select(j => new JsonValue(j)));
-			return json;
-		}
-		public static JsonValue ToJson(this IEnumerable<bool?> list)
-		{
-			if (list == null) return JsonValue.Null;
-			var json = new JsonArray();
-			json.AddRange(list.Select(b => b.HasValue ? new JsonValue(b.Value) : JsonValue.Null));
-			return json;
-		}
-		public static JsonValue ToJson(this IEnumerable<JsonArray> list)
-		{
-			if (list == null) return JsonValue.Null;
-			var json = new JsonArray();
-			json.AddRange(list.Select(j => new JsonValue(j)));
-			return json;
-		}
-		public static JsonValue ToJson(this IEnumerable<JsonObject> list)
-		{
-			if (list == null) return JsonValue.Null;
-			var json = new JsonArray();
-			json.AddRange(list.Select(j => new JsonValue(j)));
-			return json;
-		}
-		public static JsonValue ToJson(this IEnumerable<double> list)
-		{
-			if (list == null) return JsonValue.Null;
-			var json = new JsonArray();
-			json.AddRange(list.Select(j => new JsonValue(j)));
-			return json;
-		}
-		public static JsonValue ToJson(this IEnumerable<IJsonCompatible> list)
-		{
-			if (list == null) return JsonValue.Null;
-			var json = new JsonArray();
-			json.AddRange(list.Select(j => j == null ? JsonValue.Null : j.ToJson()));
-			return json;
-		}
-
-		#endregion
-	}
-}

Json/Manatee.Json.csproj

     <Compile Include="Exceptions\JsonValueIncorrectTypeException.cs" />
     <Compile Include="Exceptions\JsonValueParseException.cs" />
     <Compile Include="JsonArray.cs" />
-    <Compile Include="JsonExtensions.cs" />
+    <Compile Include="Extensions\JsonExtensions.cs" />
     <Compile Include="JsonObject.cs" />
     <Compile Include="Extensions\XmlExtensions.cs" />
     <Compile Include="JsonValue.cs" />

Json/Properties/AssemblyInfo.cs

 [assembly: AssemblyVersion("1.0.0.0")]
 [assembly: AssemblyFileVersion("1.0.0.0")]
 
-[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Manatee.Tests")]
+[assembly: InternalsVisibleTo("Manatee.Tests")]