Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added GetValueOrDefault method to the JsonValue class [SPRNETREST-25]

  • Loading branch information...
commit 64b0c27579e6c93c00269bd0ead309c82422b97f 1 parent b51d8f2
@bbaia bbaia authored
View
72 src/Spring.Rest/Json/JsonValue.cs
@@ -404,6 +404,42 @@ public T GetValue<T>(string name)
}
/// <summary>
+ /// Returns the value associated with the specified entry name, or the object's default value,
+ /// if the value held by this instance is a JSON object structure.
+ /// </summary>
+ /// <typeparam name="T">The type to convert the value to.</typeparam>
+ /// <param name="name">The name of the entry that contains the value to get.</param>
+ /// <returns>The value associated with the specified name.</returns>
+ /// <exception cref="JsonException">
+ /// If the value held by this instance is not a JSON object structure.
+ /// </exception>
+ public T GetValueOrDefault<T>(string name)
+ {
+ return this.GetValueOrDefault<T>(name, default(T));
+ }
+
+ /// <summary>
+ /// Returns the value associated with the specified entry name, or the specified default value,
+ /// if the value held by this instance is a JSON object structure.
+ /// </summary>
+ /// <typeparam name="T">The type to convert the value to.</typeparam>
+ /// <param name="name">The name of the entry that contains the value to get.</param>
+ /// <param name="defaultValue">The default value to return if the entry does not exists.</param>
+ /// <returns>The value associated with the specified name if the entry does not exists.</returns>
+ /// <exception cref="JsonException">
+ /// If the value held by this instance is not a JSON object structure.
+ /// </exception>
+ public T GetValueOrDefault<T>(string name, T defaultValue)
+ {
+ JsonValue jsonValue = this.GetValue(name);
+ if (jsonValue != null)
+ {
+ return jsonValue.GetValue<T>();
+ }
+ return defaultValue;
+ }
+
+ /// <summary>
/// Returns the value at the specified entry index
/// if the value held by this instance is a JSON array structure.
/// </summary>
@@ -430,6 +466,42 @@ public T GetValue<T>(int index)
}
/// <summary>
+ /// Returns the value at the specified entry index, or the object's default value,
+ /// if the value held by this instance is a JSON array structure.
+ /// </summary>
+ /// <typeparam name="T">The type to convert the value to.</typeparam>
+ /// <param name="index">The zero-based index of the entry that contains the value to get.</param>
+ /// <returns>The value at the specified index.</returns>
+ /// <exception cref="JsonException">
+ /// If the value held by this instance is not a JSON array structure.
+ /// </exception>
+ public T GetValueOrDefault<T>(int index)
+ {
+ return this.GetValueOrDefault<T>(index, default(T));
+ }
+
+ /// <summary>
+ /// Returns the value at the specified entry index, or the specified default value,
+ /// if the value held by this instance is a JSON array structure.
+ /// </summary>
+ /// <typeparam name="T">The type to convert the value to.</typeparam>
+ /// <param name="index">The zero-based index of the entry that contains the value to get.</param>
+ /// <param name="defaultValue">The default value to return if the entry does not exists.</param>
+ /// <returns>The value at the specified index.</returns>
+ /// <exception cref="JsonException">
+ /// If the value held by this instance is not a JSON array structure.
+ /// </exception>
+ public T GetValueOrDefault<T>(int index, T defaultValue)
+ {
+ JsonValue jsonValue = this.GetValue(index);
+ if (jsonValue != null)
+ {
+ return jsonValue.GetValue<T>();
+ }
+ return defaultValue;
+ }
+
+ /// <summary>
/// Returns all <see cref="JsonValue"/>s
/// if the value held by this instance is a JSON object or array structure.
/// </summary>
View
28 test/Spring.Rest.Tests/Json/JsonValueTests.cs
@@ -230,6 +230,20 @@ public void GetObjectValueOnJsonObject()
}
[Test]
+ public void GetObjectValueOrDefaultValueOnJsonObject()
+ {
+ JsonValue value = JsonValue.Parse("{\"Name\":\"Value\"}");
+
+ Assert.IsTrue(value.IsObject);
+ Assert.IsNull(value.GetValue("Bla"));
+ Assert.AreEqual("Value", value.GetValueOrDefault<string>("Name"));
+ Assert.AreEqual(null, value.GetValueOrDefault<string>("Bla"));
+ Assert.AreEqual(String.Empty, value.GetValueOrDefault<string>("Bla", String.Empty));
+ Assert.AreEqual(0, value.GetValueOrDefault<int>("Gla"));
+ Assert.AreEqual(-1, value.GetValueOrDefault<int>("Gla", -1));
+ }
+
+ [Test]
[ExpectedException(
typeof(JsonException),
ExpectedMessage = "The value held by this instance is not a JSON object structure.")]
@@ -253,6 +267,20 @@ public void GetArrayValueOnJsonArray()
}
[Test]
+ public void GetArrayValueOrDefaultValueOnJsonArray()
+ {
+ JsonValue value = JsonValue.Parse("[1, 2]");
+
+ Assert.IsTrue(value.IsArray);
+ Assert.IsNull(value.GetValue(7));
+ Assert.AreEqual(2, value.GetValueOrDefault<int>(1));
+ Assert.AreEqual(null, value.GetValueOrDefault<string>(3));
+ Assert.AreEqual(String.Empty, value.GetValueOrDefault<string>(3, String.Empty));
+ Assert.AreEqual(0, value.GetValueOrDefault<int>(4));
+ Assert.AreEqual(-1, value.GetValueOrDefault<int>(4, -1));
+ }
+
+ [Test]
[ExpectedException(
typeof(JsonException),
ExpectedMessage = "The value held by this instance is not a JSON array structure.")]
Please sign in to comment.
Something went wrong with that request. Please try again.