in tools/code/common/Json.cs [99:200]
public static JsonNode GetProperty(this JsonObject? jsonObject, string propertyName) =>
jsonObject.TryGetProperty(propertyName)
.IfLeftThrow();
public static Either<string, JsonNode> TryGetProperty(this JsonObject? jsonObject, string propertyName) =>
jsonObject is null
? "JSON object is null."
: jsonObject.TryGetPropertyValue(propertyName, out var property)
? property is null
? $"Property '{propertyName}' is null."
: property
: $"Property '{propertyName}' is missing.";
private static T IfLeftThrow<T>(this Either<string, T> either) =>
either.IfLeft(error => throw new JsonException(error));
public static JsonObject GetJsonObjectProperty(this JsonObject? jsonObject, string propertyName) =>
jsonObject.TryGetJsonObjectProperty(propertyName)
.IfLeftThrow();
public static Either<string, JsonObject> TryGetJsonObjectProperty(this JsonObject? jsonObject, string propertyName) =>
jsonObject.TryGetProperty(propertyName)
.Bind(JsonNodeExtensions.TryAsJsonObject)
.BindPropertyError(propertyName);
private static Either<string, T> BindPropertyError<T>(this Either<string, T> either, string propertyName) =>
either.MapLeft(error => $"Property '{propertyName}' is invalid. {error}");
public static Either<string, JsonArray> TryGetJsonArrayProperty(this JsonObject? jsonObject, string propertyName) =>
jsonObject.TryGetProperty(propertyName)
.Bind(JsonNodeExtensions.TryAsJsonArray)
.BindPropertyError(propertyName);
public static Uri GetAbsoluteUriProperty(this JsonObject jsonObject, string propertyName) =>
jsonObject.TryGetAbsoluteUriProperty(propertyName)
.IfLeftThrow();
public static Either<string, Uri> TryGetAbsoluteUriProperty(this JsonObject? jsonObject, string propertyName) =>
jsonObject.TryGetProperty(propertyName)
.Bind(JsonNodeExtensions.TryAsAbsoluteUri)
.BindPropertyError(propertyName);
public static Either<string, string> TryGetStringProperty(this JsonObject? jsonObject, string propertyName) =>
jsonObject.TryGetProperty(propertyName)
.Bind(JsonNodeExtensions.TryAsString)
.BindPropertyError(propertyName);
public static string GetNonEmptyOrWhiteSpaceStringProperty(this JsonObject jsonObject, string propertyName) =>
jsonObject.TryGetNonEmptyOrWhiteSpaceStringProperty(propertyName)
.IfLeftThrow();
public static Either<string, string> TryGetNonEmptyOrWhiteSpaceStringProperty(this JsonObject? jsonObject, string propertyName) =>
jsonObject.TryGetProperty(propertyName)
.Bind(JsonNodeExtensions.TryAsString)
.Bind(value => string.IsNullOrWhiteSpace(value)
? Either<string, string>.Left($"Property '{propertyName}' is empty or whitespace.")
: Either<string, string>.Right(value))
.BindPropertyError(propertyName);
public static string GetStringProperty(this JsonObject jsonObject, string propertyName) =>
jsonObject.TryGetStringProperty(propertyName)
.IfLeftThrow();
public static int GetIntProperty(this JsonObject jsonObject, string propertyName) =>
jsonObject.TryGetIntProperty(propertyName)
.IfLeftThrow();
public static Either<string, int> TryGetIntProperty(this JsonObject? jsonObject, string propertyName) =>
jsonObject.TryGetProperty(propertyName)
.Bind(JsonNodeExtensions.TryAsInt)
.BindPropertyError(propertyName);
public static bool GetBoolProperty(this JsonObject jsonObject, string propertyName) =>
jsonObject.TryGetBoolProperty(propertyName)
.IfLeftThrow();
public static Either<string, bool> TryGetBoolProperty(this JsonObject? jsonObject, string propertyName) =>
jsonObject.TryGetProperty(propertyName)
.Bind(JsonNodeExtensions.TryAsBool)
.BindPropertyError(propertyName);
public static JsonObject Parse<T>(T obj) =>
TryParse(obj)
.IfLeft(() => throw new JsonException($"Could not parse {typeof(T).Name} as a JSON object."));
public static Either<string, JsonObject> TryParse<T>(T obj) =>
JsonSerializer.SerializeToNode(obj, SerializerOptions)
.TryAsJsonObject();
[return: NotNullIfNotNull(nameof(jsonObject))]
public static JsonObject? SetProperty(this JsonObject? jsonObject, string propertyName, JsonNode? jsonNode)
{
if (jsonObject is null)
{
return null;
}
else
{
jsonObject[propertyName] = jsonNode;
return jsonObject;
}
}