Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/shiburagi/jsontodart

VS Code Extension
https://github.com/shiburagi/jsontodart

dart dart-model flutter json jsontodart vscode-extension

Last synced: about 1 month ago
JSON representation

VS Code Extension

Awesome Lists containing this project

README

        

# jsontodart

Demo : https://jsontodart.zariman.dev/

## How to use

### Method 1
1) Copy JSON
2) Open ```Command Pallete```, and find ```JsonToDart: Convert JSON from Clipboard```
3) Fill the box, select the option
4) Done

### Method 2
1) Copy JSON
2) Right click on folder or dart file
3) Click ```Convert JSON from Clipboard Here```
4) Fill the box, select the option
6) Done

### More Configuration
To handle **null** value on generated model, you can define/change data type on pubspec.yaml

Example:
```yaml
jsonToDart:
nullValueDataType: String #default: dynamic
```

## Sample
```json
{
"data": {
"id": 2,
"email": "[email protected]",
"first_name": "Janet",
"last_name": "Weaver",
"avatar": "https://reqres.in/img/faces/2-image.jpg"
},
"support": {
"url": "https://reqres.in/#support-heading",
"text": "To keep ReqRes free, contributions towards server costs are appreciated!"
}
}
```

Output:

```dart

class User {
Data? data;
Support? support;

User({this.data, this.support});

User.fromJson(Map json) {
this.data = json["data"] == null ? null : Data.fromJson(json["data"]);
this.support = json["support"] == null ? null : Support.fromJson(json["support"]);
}

Map toJson() {
final Map data = new Map();
if(this.data != null)
data["data"] = this.data?.toJson();
if(this.support != null)
data["support"] = this.support?.toJson();
return data;
}
}

class Support {
String? url;
String? text;

Support({this.url, this.text});

Support.fromJson(Map json) {
this.url = json["url"];
this.text = json["text"];
}

Map toJson() {
final Map data = new Map();
data["url"] = this.url;
data["text"] = this.text;
return data;
}
}

class Data {
int? id;
String? email;
String? firstName;
String? lastName;
String? avatar;

Data({this.id, this.email, this.firstName, this.lastName, this.avatar});

Data.fromJson(Map json) {
this.id = json["id"];
this.email = json["email"];
this.firstName = json["first_name"];
this.lastName = json["last_name"];
this.avatar = json["avatar"];
}

Map toJson() {
final Map data = new Map();
data["id"] = this.id;
data["email"] = this.email;
data["first_name"] = this.firstName;
data["last_name"] = this.lastName;
data["avatar"] = this.avatar;
return data;
}
}

```

## Customize (pubspec.yaml)
```yaml
jsonToDart:
outputFolder: lib/models #default: lib/
typeChecking: true #default: undefined (Select from picker)
nullValueDataType: String #default: dynamic
nullSafety: false #default: true
copyWithMethod: true #default: false
mergeArrayApproach: false #default: true

#default: false,
#if true, value = (json[key] as num).toInt() or value = (_data[key] as num).toDouble()
#if false, value = json[key]
#if "ask", selection popup will apear before parse json
checkNumberAsNum: true # true, false, "ask"
```