decodeString method

String decodeString (dynamic src)

Decode a String

Implementation

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

  String str = '"';

  if(p.actual() == "\"" || p.actual() == "\'") {
    String 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 + '"');
}