JsonParsing
Namespace: Vercel.AI.ProviderUtils
public static class JsonParsingParses JSON text, optionally checking it against a JSON Schema document.
Inheritance: object
Methods
Section titled “Methods”IsParsable(string)
Section titled “IsParsable(string)”public static bool IsParsable(string text)True when Parse(string) accepts <code class=“paramref”>text</code>.
Parameters
| Name | Type | Description |
|---|---|---|
text |
string |
Returns: bool
Parse(string, JsonNode?)
Section titled “Parse(string, JsonNode?)”public static JsonElement Parse(string text, JsonNode? schema = null)Parses <code class=“paramref”>text</code>. Schema failures throw TypeValidationException.
Parameters
| Name | Type | Description |
|---|---|---|
text |
string | |
schema |
JsonNode |
Returns: JsonElement
SafeParse(string, JsonNode?)
Section titled “SafeParse(string, JsonNode?)”public static JsonParseResult SafeParse(string text, JsonNode? schema = null)Parses <code class=“paramref”>text</code> without throwing for invalid JSON or schema mismatches.
Parameters
| Name | Type | Description |
|---|---|---|
text |
string | |
schema |
JsonNode |
Returns: JsonParseResult
SafeValidateTypes(JsonElement, JsonNode)
Section titled “SafeValidateTypes(JsonElement, JsonNode)”public static JsonParseResult SafeValidateTypes(JsonElement value, JsonNode schema)Validates <code class=“paramref”>value</code> without throwing. Maps to safeValidateTypes.
Parameters
| Name | Type | Description |
|---|---|---|
value |
JsonElement | |
schema |
JsonNode |
Returns: JsonParseResult
ValidateTypes(JsonElement, JsonNode)
Section titled “ValidateTypes(JsonElement, JsonNode)”public static JsonElement ValidateTypes(JsonElement value, JsonNode schema)Returns <code class=“paramref”>value</code> projected through <code class=“paramref”>schema</code>. Maps to validateTypes.
Parameters
| Name | Type | Description |
|---|---|---|
value |
JsonElement | |
schema |
JsonNode |
Returns: JsonElement
Exceptions
| Type | Condition |
|---|---|
| TypeValidationException | The value does not match the schema. |
Apache-2.0 · Copyright 2023 Vercel, Inc. · unofficial community port, not an official Vercel product