Skip to content

Commit

Permalink
Merge pull request #61 from iamarnas/master
Browse files Browse the repository at this point in the history
Added new features and enhancement
  • Loading branch information
iamarnas authored Sep 14, 2021
2 parents c398942 + 129d400 commit 6245f12
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 40 deletions.
11 changes: 8 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 3.3.7

- Improved double values conversion.
- Added possibility to override the default path with a new one by adding `__path` key by working with models.jsonc file. Example: `"__path": "/lib/models/user"`.

## 3.3.6

- Bugfix.
Expand All @@ -12,9 +17,9 @@
```json
{
"userPost.post": {
"id": 1,
"description": "Json To Dart Model",
"completed": false
"id": 1,
"description": "Json To Dart Model",
"completed": false
}
}
```
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,13 @@ To customize your classes is very easy. If you want fast to create a simple clas

`Json to Dart Model` generator keeps all your JSON objects in the file with the name `models.jsonc` and allows you to configure your classes according to your preferences. `models.jsonc` content is a list that contains all of your JSON objects that will later be converted to Dart classes. The `jsonc` format allows you to comment on your JSON objects to easily find them later or make it easier to explain to your team. To create the `models.jsonc` file you can run command in the command palette `Build Models` or use keys binging `Shift + Ctrl + Alt + B` and you will be asked if you want to create a file, hit Enter to add the file. To configure options for output, go to the `Settings/Extensions/JSON To Dart Model`

**Create file manually**. Just add a new file to your app directory `my_app/models.jsonc` and put all JSON objects to the list object separated by commas. **Note that you add base class names to each object with key** `"__className": "MyClass",` the class name will be removed from the object and used as the root class name for your code syntax. Duplicate class names are not allowed to avoid overwriting the files. Inside your `models.jsonc` file should look like this:
**Create file manually**. Just add a new file to your app directory `my_app/models.jsonc` and put all JSON objects to the list object separated by commas. **Note that you add base class names to each object with key** `"__className": "MyClass",` the class name will be removed from the object and used as the root class name for your code syntax. Duplicate class names are not allowed to avoid overwriting the files. Inside your `models.jsonc`. By adding key `__path` you can override the default path and navigate your models where you want and how your want in your workspace. File should look like this:

```jsonc
[
{
"__className": "UserPosts", // <- The base class name of the object.
"__className": "UserPost", // <- The base class name of the object.
"__path": "/lib/models/user_post", // <- override default path with a new one by adding '__path' key.
"userId": 1,
"id": 1, // To mark as required value, change "id" to "r@id".
"title": "Json To Dart Model", // To mark as a default value, change "title" to "d@title".
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "json-to-dart",
"displayName": "Json to Dart Model",
"description": "Extension convert Json to Dart Model class",
"version": "3.3.6",
"version": "3.3.7",
"publisher": "hirantha",
"icon": "icon.png",
"engines": {
Expand Down Expand Up @@ -256,4 +256,4 @@
"mkdirp": "^1.0.4",
"pluralize": "^8.0.0"
}
}
}
59 changes: 33 additions & 26 deletions src/models-file.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import * as fs from 'fs';

import { ConfigurationTarget, window } from 'vscode';
import { config } from './configuration';
import { getWorkspaceRoot } from './utils';
import { printLine } from './syntax';


class Models {
private fileName: string = '/models.jsonc';
Expand All @@ -25,30 +24,38 @@ class Models {
}

private toString() {
let sb = '';
sb += printLine('// GENERATED FOR JSON TO DART MODEL');
sb += printLine('[', 1);
sb += printLine('// Useful links to work with this file:', 1, 1);
sb += printLine('// About jsonc: https://github.com/onury/jsonc', 1, 1);
sb += printLine('// Try jsonc: https://komkom.github.io', 1, 1);
sb += printLine('//', 1, 1);
sb += printLine('// To configure generator, go to Settings/Extensions/JSON To Dart Model', 1, 1);
sb += printLine('//', 1, 1);
sb += printLine('// Add your json objects here separated by commas.', 1, 1);
sb += printLine('// Note that you add class names to each object with key "__className":', 1, 1);
sb += printLine('// And avoid duplicate class names in this list for best results.', 1, 1);
sb += printLine('// FOR EXAMPLE:', 1, 1);
sb += printLine('/*\n', 1, 1);
sb += printLine('{', 1, 1);
sb += printLine('"__className": "UserPost", // <- The base class name of the object.', 1, 2);
sb += printLine('"userId": 1,', 1, 2);
sb += printLine('"id": 1, // To mark as required value, change "id" to "r@id".', 1, 2);
sb += printLine('"title": "Json To Dart Model", // To mark as a default value, change "title" to "d@title".', 1, 2);
sb += printLine('"body": "Json to Dart advanced..."', 1, 2);
sb += printLine('}\n', 1, 1);
sb += printLine('*/', 1, 1);
sb += printLine(']', 1);
return sb;
return `// GENERATED BY JSON TO DART MODEL
[
// Useful links to work with this file:
// About jsonc: https://github.com/onury/jsonc
// Try jsonc: https://komkom.github.io
//
// To configure generator, go to Settings/Extensions/JSON To Dart Model
//
// Add your json objects here separated by commas.
// Note that you add class names to each object with key "__className":
// And avoid duplicate class names in this list for best results.
// FOR EXAMPLE:
/*
// Uncomment this to test and run builder with Shift + Ctrl + Alt + B
{
// To add enhancemed name to all generated files add new name after dot.
// Example: user_post.model. Result: user_post.model.dart
"__className": "user_post", // <- The base class name of the object.
// It's possible to override the default path with a new one by adding "__path" key.
// - it's useful if you want split your models to different workspace directories.
"__path": "/lib/models/user", // <- this is not required.
"userId": 1,
"id": 1, // To mark as required value, change "id" to "r@id".
"title": "Json To Dart Model", // To mark as a default value, change "title" to "d@title".
"body": "Json to Dart advanced..."
// Force new type by adding new one after dot.
// Note: it works only with not primitive values.
// "type": {...} // <- Example: "type.post_type". Result: PostType type;
}
*/
]`;
}

async duplicatesClass(objects: any[]): Promise<string[]> {
Expand Down
15 changes: 9 additions & 6 deletions src/syntax.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import * as _ from 'lodash';

import { camelCase, equalByType, filterListType, getNestedObject, pascalCase, snakeCase } from './utils';
import { ClassNameModel } from './settings';
import { Input } from './input';
import { TypeDefinition } from './constructor';
import { Input } from './input';
import { ClassNameModel } from './settings';
import { emptyClass } from './syntax/empty-class.syntax';
import { handleJsonValue } from './model-generator';
import { camelCase, equalByType, filterListType, getNestedObject, pascalCase, snakeCase } from './utils';


export const emptyListWarn = 'list is empty';
export const ambiguousListWarn = 'list is ambiguous';
Expand Down Expand Up @@ -342,15 +341,19 @@ export function jsonParseValue(
input: Input
) {
const jsonValue = valueFromJson(key);
const nullable = questionMark(input, typeDef.nullable);
let formatedValue = '';

if (typeDef.isPrimitive) {
if (typeDef.isDate) {
formatedValue = jsonParseClass(key, typeDef, input);
} else {
if (typeDef.type?.match('dynamic') && !typeDef.isList) {
formatedValue = `${jsonValue}`;
} if (typeDef.type?.match('double')) {
formatedValue = `(${jsonValue} as num${nullable})${nullable}.toDouble()`;
} else {
formatedValue = `${jsonValue} as ${typeDef.type}` + questionMark(input, typeDef.nullable);
formatedValue = `${jsonValue} as ${typeDef.type}` + nullable;
}
}
} else {
Expand Down

0 comments on commit 6245f12

Please sign in to comment.