- Notifications
You must be signed in to change notification settings - Fork47
The tool to convert json to dart code, support Windows,Mac,Web.
License
fluttercandies/JsonToDart
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
The tool to convert json to dart code, support Windows,Mac,Web。
Language: English |中文简体
Microsoft Store The function is not ,it will replace withFlutter for UWP in the future.
Click Format button, it will convert the Json string into Dart class structure.
It will protect data when convert data as T safety.
classFFConvert {FFConvert._();staticT?Function<TextendsObject?>(dynamic value) convert=<T>(dynamic value) {if (value==null) {returnnull; }return json.decode(value.toString())asT?; };}T?asT<TextendsObject?>(dynamic value, [T? defaultValue]) {if (valueisT) {return value; }try {if (value!=null) {finalString valueS= value.toString();if (''isT) {return valueSasT; }elseif (0isT) {returnint.parse(valueS)asT; }elseif (0.0isT) {returndouble.parse(valueS)asT; }elseif (falseisT) {if (valueS=='0'|| valueS=='1') {return (valueS=='1')asT; }return (valueS=='true')asT; }else {returnFFConvert.convert<T>(value); } } }catch (e, stackTrace) {log('asT<$T>', error: e, stackTrace: stackTrace);return defaultValue; }return defaultValue;}
you can override [FFConvert.convert] to handle special case.
FFConvert.convert=<TextendsObject?>(dynamic value) {if (value==null) {returnnull; }finaldynamic output= json.decode(value.toString());if (<int>[]isT&& outputisList<dynamic>) {return output.map<int?>((dynamic e)=>asT<int>(e)).toList()asT; }elseif (<String,String>{}isT&& outputisMap<dynamic,dynamic>) {return output.map<String,String>((dynamic key,dynamic value)=>MapEntry<String,String>(key.toString(), value.toString()))asT; }elseif (constTestMode()isT&& outputisMap<dynamic,dynamic>) {returnTestMode.fromJson(output)asT; }return json.decode(value.toString())asT?; };
It can protect your array in case of one has some error.
voidtryCatch(Function f) {try { f?.call(); }catch (e, stack) {debugPrint("$e");debugPrint("$stack"); }}
The first object may be not has all properties.
We can slove it by setting traverse array count. It will traverse the array and merge the object.
1, 20, and 99 options are provided.
99 means traverse all of the array.
none,camel case,pascal,hungarian notation
camel case is recommended.
none,ascending,descending
Whether add [Data Protection] and [Array Protection].In fact, you only need it at first time then extract it into your code and import it.
Support to generate null-safety code.
You can add copyright,dart code, creator into here. support [Date yyyy MM-dd] format to generate time.
none,final options are provided.
You can set nullable if you are enable null-saftey.
When smart nullable is checked, all null fields and missing properties of the array will be automatically nullable.
zh_hans,zh_hant and en are support.
Dart class structure are shown at right.
First column is the key in Json.
Second column is Property type or Class name. Yellow background will show if it is Class.
Third column is property name.
It will show red background if something is empty.
Click generate button, Dart code will generate at left side. Dart code will set into the clipboard.
About
The tool to convert json to dart code, support Windows,Mac,Web.