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

Add tryDecode and tryEncode to json #49138

Open
rifkyputra opened this issue Jan 28, 2022 · 2 comments
Open

Add tryDecode and tryEncode to json #49138

rifkyputra opened this issue Jan 28, 2022 · 2 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-convert type-enhancement A request for a change that isn't a bug

Comments

@rifkyputra
Copy link

json.decode will throw error when input is empty string.

Uncaught Error: FormatException: SyntaxError: Unexpected end of JSON input

here is the sample :

import 'dart:convert';

void main() {
  
  final foo = "";
  
  final list = json.decode(foo);
  
  print(list);
 
  
}

it would be nice to add json.tryDecode and json.tryEncode since json data can be from external source such as api or shared pref (in flutter).

@rifkyputra
Copy link
Author

rifkyputra commented Feb 2, 2022


import 'dart:convert';

void main() {
  final content =' {"s": "hello"}';
  
  
  final foo = validJson.encode(content);
  print(foo);
  
  
  final bar= validJson.encode("");
  print(bar);
  
  final baz = validJson.encode("Not Valid Json");
  print(baz);
  
  
}


const ValidJson validJson = ValidJson();

class ValidJson {
  
  const ValidJson();
  
  String? encode(Object value,  {Object? Function(dynamic)? toEncodable}) {
    
    if (value is String) {
       try {
         json.decode(value) as Map;
         return value;
      } catch(e) {
        return null;
      }
    }
    
    
    final encoded = json.encode(value, toEncodable: toEncodable);
    
    try {
      json.decode(encoded) as Map;
    } catch(e) {
      print(e);
      return null;
    }
    
    
    return encoded;
    
  } 
}



@lrhn lrhn transferred this issue from dart-lang/convert May 30, 2022
@lrhn lrhn added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-convert type-enhancement A request for a change that isn't a bug labels May 30, 2022
@lrhn
Copy link
Member

lrhn commented May 30, 2022

The argument against having json.tryDecode is that a valid decoding can also return null, so there would be no way to tell the difference between json.tryDecode("null") and json.tryDecode("arglebargle").

On the other hand, people decoding JSON usually know that they're expecting a list or map, and would treat a null as as an error anyway.

@lrhn lrhn changed the title Add tryDecode and tryEncode Add tryDecode and tryEncode to json May 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-convert type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

2 participants