decodeString method

String decodeString(
  1. dynamic src
)

Decode a String

Implementation

String decodeString(dynamic src) {
  var p = src is GsonParsable
      ? src
      : src is String
          ? GsonParsable(src)
          : throw ('The src is not a valid input to decode an Array from');

  var str = '"';

  if (p.actual() == '"' || p.actual() == "'") {
    var search = p.next();
    while (p.actual() != search) {
      if (p.actual() == '\\') {
        str += p.next();
      } else if (p.actual() == '"') {
        str += '\\' + p.next();
        continue;
      }
      str += p.next();
    }
    if (!p.ended) {
      p.skip();
    }
  } else if (_PURE_STRING.hasMatch(p.actual())) {
    while (_PURE_STRING.hasMatch(p.actual())) {
      if (p.actual() == '\\') {
        str += p.next();
      }
      str += p.next();
    }
  } else {
    throw p.error(
        'String has to start with a \"\\\"\" or \"\\\'\" when it contains some characters');
  }

  return json.decode(str + '"');
}