@@ -23,6 +23,22 @@ public static Either<string, Uri> TryAsAbsoluteUri(this JsonValue? jsonValue) =>
2323 . Bind ( uriString => Uri . TryCreate ( uriString , UriKind . Absolute , out var uri )
2424 ? Either < string , Uri > . Right ( uri )
2525 : $ "JSON value '{ uriString } ' is not a valid absolute URI.") ;
26+
27+ public static Either < string , int > TryAsInt ( this JsonValue ? jsonValue ) =>
28+ jsonValue is null
29+ ? "JSON value is null."
30+ : jsonValue . TryGetValue < int > ( out var intValue )
31+ || ( jsonValue . TryGetValue < string > ( out var stringValue ) && int . TryParse ( stringValue , out intValue ) )
32+ ? intValue
33+ : "JSON value is not an integer." ;
34+
35+ public static Either < string , bool > TryAsBool ( this JsonValue ? jsonValue ) =>
36+ jsonValue is null
37+ ? "JSON value is null."
38+ : jsonValue . TryGetValue < bool > ( out var boolValue )
39+ || ( jsonValue . TryGetValue < string > ( out var stringValue ) && bool . TryParse ( stringValue , out boolValue ) )
40+ ? boolValue
41+ : "JSON value is not a boolean." ;
2642}
2743
2844public static class JsonNodeExtensions
@@ -49,6 +65,14 @@ public static Either<string, string> TryAsString(this JsonNode? node) =>
4965 public static Either < string , Uri > TryAsAbsoluteUri ( this JsonNode ? node ) =>
5066 node . TryAsJsonValue ( )
5167 . Bind ( jsonValue => jsonValue . TryAsAbsoluteUri ( ) ) ;
68+
69+ public static Either < string , int > TryAsInt ( this JsonNode ? node ) =>
70+ node . TryAsJsonValue ( )
71+ . Bind ( jsonValue => jsonValue . TryAsInt ( ) ) ;
72+
73+ public static Either < string , bool > TryAsBool ( this JsonNode ? node ) =>
74+ node . TryAsJsonValue ( )
75+ . Bind ( jsonValue => jsonValue . TryAsBool ( ) ) ;
5276}
5377
5478public static class JsonArrayExtensions
@@ -134,7 +158,25 @@ public static Either<string, string> TryGetNonEmptyOrWhiteSpaceStringProperty(th
134158 public static string GetStringProperty ( this JsonObject jsonObject , string propertyName ) =>
135159 jsonObject . TryGetStringProperty ( propertyName )
136160 . IfLeftThrow ( ) ;
137-
161+
162+ public static int GetIntProperty ( this JsonObject jsonObject , string propertyName ) =>
163+ jsonObject . TryGetIntProperty ( propertyName )
164+ . IfLeftThrow ( ) ;
165+
166+ public static Either < string , int > TryGetIntProperty ( this JsonObject ? jsonObject , string propertyName ) =>
167+ jsonObject . TryGetProperty ( propertyName )
168+ . Bind ( JsonNodeExtensions . TryAsInt )
169+ . BindPropertyError ( propertyName ) ;
170+
171+ public static bool GetBoolProperty ( this JsonObject jsonObject , string propertyName ) =>
172+ jsonObject . TryGetBoolProperty ( propertyName )
173+ . IfLeftThrow ( ) ;
174+
175+ public static Either < string , bool > TryGetBoolProperty ( this JsonObject ? jsonObject , string propertyName ) =>
176+ jsonObject . TryGetProperty ( propertyName )
177+ . Bind ( JsonNodeExtensions . TryAsBool )
178+ . BindPropertyError ( propertyName ) ;
179+
138180 public static JsonObject Parse < T > ( T obj ) =>
139181 TryParse ( obj )
140182 . IfLeft ( ( ) => throw new JsonException ( $ "Could not parse { typeof ( T ) . Name } as a JSON object.") ) ;
0 commit comments