Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Are JsonDecode() and JsonEncoder() has a try option? #71

Open
SittiphanSittisak opened this issue Oct 30, 2022 · 0 comments
Open

Are JsonDecode() and JsonEncoder() has a try option? #71

SittiphanSittisak opened this issue Oct 30, 2022 · 0 comments

Comments

@SittiphanSittisak
Copy link

SittiphanSittisak commented Oct 30, 2022

This option work like int.tryParse() that will return int?.

Where are these?

  JsonDecoder().tryConvert(data);
  JsonEncoder().tryConvert(data);

I can create this by

class Json {
  static String? tryEncode(data) {
    try {
      return jsonEncode(data);
    } catch (e) {
      return null;
    }
  }

  static dynamic tryDecode(data) {
    try {
      return jsonDecode(data);
    } catch (e) {
      return null;
    }
  }
}

void main() {
  String? jsonEncode = Json.tryEncode(dataEncoded);
  dynamic jsonDecode = Json.tryDecode(dataDecoded);
  print("jsonEncode: $jsonEncode");
  print("jsonDecode: $jsonDecode");
}

However, I'm uncomfortable with this.
Is it impossible to add this function?

@SittiphanSittisak SittiphanSittisak changed the title Are JsonDecode()/JsonEncoder() has a try option? Are JsonDecode() and JsonEncoder() has a try option? Oct 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant