encode method

EncodeOutput encode(
  1. EncodeInput input
)

Implementation

EncodeOutput encode(EncodeInput input) {
  final v = _convertToDart(input.value);
  final String s;
  switch (input.format) {
    case EncodeInput_Format.JSON:
      try {
        s = JsonEncoder().convert(v);
      } catch (e) {
        return EncodeOutput(isError: true, errorMessage: e.toString());
      }
    case EncodeInput_Format.YAML:
      try {
        s = (YamlEditor('')..update([], v)).toString();
      } catch (e) {
        return EncodeOutput(isError: true, errorMessage: e.toString());
      }
    default:
      return EncodeOutput(
          isError: true, errorMessage: 'Unsupported format: ${input.format}');
  }
  return EncodeOutput(text: s);
}