Commits

Anonymous committed 34a152b

Updated extension methods to accept nulls.
Corrected GetIndentedString() in JsonValue.

Comments (0)

Files changed (4)

Binary file modified.

Binary file modified.

Json/Extensions/JsonExtensions.cs

 		/// <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;
+			return (obj == null) ? null : 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.
 		/// <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;
+			return (obj == null) ? null : 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.
 		/// <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;
+			return (obj == null) ? null : 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.
 		/// <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;
+			return (obj == null) ? null : 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.
 		/// <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;
+			return (obj == null) ? null : obj.ContainsKey(key) && (obj[key].Type != JsonValueType.Null) ? obj[key].Object : null;
 		}
 	}
 }

Json/JsonValue.cs

 				case JsonValueType.Array:
 					return arrayValue.GetIndentedString(indentLevel);
 				default:
-					return "null";
+					return ToString();
 			}
 		}
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.