in tools/code/common/Json.cs [14:45]
public static Either<string, string> TryAsString(this JsonValue? jsonValue) =>
jsonValue is null
? Either<string, string>.Left("JSON value is null.")
: jsonValue.TryGetValue<string>(out var stringValue)
? Either<string, string>.Right(stringValue)
: Either<string, string>.Left("JSON value is not a string.");
public static Either<string, Uri> TryAsAbsoluteUri(this JsonValue? jsonValue) =>
jsonValue.TryAsString()
.Bind(uriString => Uri.TryCreate(uriString, UriKind.Absolute, out var uri)
? Either<string, Uri>.Right(uri)
: $"JSON value '{uriString}' is not a valid absolute URI.");
public static Either<string, int> TryAsInt(this JsonValue? jsonValue) =>
jsonValue is null
? "JSON value is null."
: jsonValue.TryGetValue<int>(out var intValue)
|| (jsonValue.TryGetValue<string>(out var stringValue) && int.TryParse(stringValue, out intValue))
? intValue
: "JSON value is not an integer.";
public static Either<string, bool> TryAsBool(this JsonValue? jsonValue) =>
jsonValue is null
? "JSON value is null."
: jsonValue.TryGetValue<bool>(out var boolValue)
|| (jsonValue.TryGetValue<string>(out var stringValue) && bool.TryParse(stringValue, out boolValue))
? boolValue
: "JSON value is not a boolean.";
}
public static class JsonNodeExtensions
{