JSONValue object to access.
Key to look up.
Value to return if the key is not found.
The value found in the JSON object, or the fallback value if the key was not found.
import std.conv : to; { const json = JSONValue([ "abc" : "def", "ghi" : "jkl" ]); { const value = getOrFallback(json, "abc"); assert((value == "def"), value); } { const value = getOrFallback(json, "ghi"); assert((value == "jkl"), value); } { const value = getOrFallback(json, "mno"); assert((value == string.init), value); } { const value = getOrFallback(json, "pqr", "INVALID"); assert((value == "INVALID"), value); } } { const json = JSONValue([ "123" : 123, "456" : 456 ]); { const value = getOrFallback!long(json, "123"); assert((value == 123L), value.to!string); } { const int value = getOrFallback!int(json, "456"); assert((value == 456), value.to!string); } { const value = getOrFallback!long(json, "789"); assert((value == 0L), value.to!string); } { const short value = getOrFallback!short(json, "012", 999); assert((value == 999), value.to!string); } }
Fetches a value from a JSONValue, or returns a fallback value if the key doesn't exist in the object.
Limitations: Only work on JSONType.object-type JSONValues.