Commits

gde...@USGVL5J5DKV1.babgsetc.pvt  committed 34a152b

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

  • Participants
  • Parent commits aa93149

Comments (0)

Files changed (4)

File Json.sln.docstates.suo

Binary file modified.

File Json.suo

Binary file modified.

File 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;
 		}
 	}
 }

File Json/JsonValue.cs

 				case JsonValueType.Array:
 					return arrayValue.GetIndentedString(indentLevel);
 				default:
-					return "null";
+					return ToString();
 			}
 		}