fromContent static method
Tries to construct an APIConfig from content
.
- If
type
is defined (JSON
,YAML
orproperties
), forces decoding of the specified type format. - If
autoIdentify
istrue
it tries to detect the format to decode.
Implementation
static APIConfig? fromContent(String content,
{String? type, bool autoIdentify = true, String? source}) {
type ??= '';
type = type.toLowerCase().trim();
if (type == '*') {
autoIdentify = true;
}
if (type == 'json' || type == 'js') {
try {
return APIConfig.fromJsonEncoded(content).._source = source;
} catch (_) {
throw FormatException('Error parsing JSON!');
}
}
if (type == 'yaml' || type == 'yml') {
try {
return APIConfig.fromYAML(content).._source = source;
} catch (_) {
throw FormatException('Error parsing YAML!');
}
}
if (type == 'properties' || type == 'prop') {
try {
return APIConfig.fromPropertiesEncoded(content).._source = source;
} catch (_) {
throw FormatException('Error parsing properties!');
}
}
if (autoIdentify) {
try {
return APIConfig.fromJsonEncoded(content).._source = source;
} catch (_) {}
try {
return APIConfig.fromYAML(content).._source = source;
} catch (_) {}
try {
return APIConfig.fromPropertiesEncoded(content).._source = source;
} catch (_) {}
}
return null;
}