JsonAny extension type

A zero-cost wrapper around any JSON value including null.

Unlike JsonValue, this type provides helper methods to convert the value to a specific type, or to check its type. For each possible JSON type, there is a cooresponding set of methods to:

Example

import 'package:jsonut/jsonut.dart';

void main() {
  final value = JsonAny.parse('{"key": true}');
  final object = value.object();
  print(object['key'].boolean()); // true
}

Safety

It is undefined behavior to cast (i.e. using as or a similar method such as List.cast) an Object to a JsonAny if it is not a valid JSON value. See JsonValue for more information.

on
Implemented types

Constructors

JsonAny.from(Object? value)
Converts the given value to a JsonValue.
factory
JsonAny.parse(String input)
Parses and returns the given input as a JSON value.
factory
JsonAny.parseUtf8(List<int> bytes)
Parses and returns the given UTF-8 encoded bytes as a JSON value.
factory

Properties

isArray bool
Whether the value is an array.
no setter
isBool bool
Whether the value is a boolean.
no setter
isNull bool
Whether the value is null.
no setter
isNumber bool
Whether the value is a number.
no setter
isObject bool
Whether the value is an object.
no setter
isString bool
Whether the value is a string.
no setter

Methods

array() JsonArray
Returns the value as an array.
arrayOrEmpty() JsonArray
Returns the value as an array, or an empty array if it is not an array.
arrayOrNull() JsonArray?
Returns the value as an array, or null if it is not an array.
as<T>() → T
Returns the value cast to the given type T.
asAny() JsonAny
Converts this object into a JsonAny.
inherited
asOrNull<T>() → T?
Returns the value cast to the given type T, or null otherwise.
boolean() JsonBoolean
Returns the value as a boolean.
booleanOrFalse() JsonBoolean
Returns the value as a boolean, or false if it is not a boolean.
booleanOrNull() JsonBoolean?
Returns the value as a boolean, or null if it is not a boolean.
number() JsonNumber
Returns the value as a number.
numberOrNull() JsonNumber?
Returns the value as a number, or null if it is not a number.
numberOrZero() JsonNumber
Returns the value as a number, or 0 if it is not a number.
object() JsonObject
Returns the value as an object.
objectOrEmpty() JsonObject
Returns the value as an object, or an empty object if it is not an object.
objectOrNull() JsonObject?
Returns the value as an object, or null if it is not an object.
string() JsonString
Returns the value as a string.
stringOrEmpty() JsonString
Returns the value as a string, or an empty string if it is not a string.
stringOrNull() JsonString?
Returns the value as a string, or null if it is not a string.