checkJson function

void checkJson(
  1. String? json,
  2. String param, {
  3. bool emptyIsDefault = false,
})

Validate a caller-supplied raw JSON string before it crosses the C C ABI; throws FormatException naming param (source) on bad syntax. Null passes through (optional parameters); empty/blank is rejected like the FFI does for REQUIRED params, unless emptyIsDefault — pass it for every OPTIONAL JSON param (nullable in the Dart signature: config_json of provider/router/moa, opts_json of embed / file upload / transcription), where the FFI treats NULL/empty as "use defaults".

Implementation

void checkJson(String? json, String param, {bool emptyIsDefault = false}) {
  if (json == null) return;
  if (json.trim().isEmpty) {
    if (emptyIsDefault) return;
    throw FormatException('$param: invalid JSON: empty', param);
  }
  final Object? value;
  try {
    value = jsonDecode(json);
  } on FormatException catch (e) {
    throw FormatException('$param: ${e.message}', param);
  }
  checkPortable(value, param);
}