split<R> method

R split<R>(
  1. R onNull(),
  2. R onBool(
    1. bool
    ),
  3. R onInt(
    1. int
    ),
  4. R onDouble(
    1. double
    ),
  5. R onString(
    1. String
    ),
  6. R onList(
    1. List<Json>
    ),
  7. R onMap(
    1. Map<String, Json>
    ),
)

Handle each possible shape of this JSON value.

This method is used to discriminate the possible shapes of this JSON value, similarly to a switch expression. It accepts one function per shape, unconditionally, and using positional parameters. This is the most concise approach. Use splitNamed if a more flexible (but less concise) approach is preferred.

In the case where this JSON value is malformed (i.e., it is not of type Null, bool, int, double, String, List<dynamic>, or Map<String, dynamic>), an InvalidJsonTypeException is thrown, holding the given value.

Implementation

R split<R>(
  R Function() onNull,
  R Function(bool) onBool,
  R Function(int) onInt,
  R Function(double) onDouble,
  R Function(String) onString,
  R Function(List<Json>) onList,
  R Function(Map<String, Json>) onMap,
) {
  var v = value;
  if (v == null) return onNull();
  if (v is bool) return onBool(v);
  if (v is int) return onInt(v);
  if (v is double) return onDouble(v);
  if (v is String) return onString(v);
  if (v is List<Json>) return onList(v);
  if (v is Map<String, Json>) return onMap(v);
  throw InvalidJsonTypeException(value);
}