Skip to content

Commit

Permalink
Merge pull request #31 from iamarnas/master
Browse files Browse the repository at this point in the history
Syntax correction
  • Loading branch information
iamarnas authored Apr 23, 2021
2 parents ff9bcfa + 301cfdc commit 1f94d5c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

## [Released]

## [3.1.2] - 2021-04-23

### Fixed

- see [pull request](https://github.com/hiranthaR/Json-to-Dart-Model/pull/30) from [Arnas](https://github.com/iamarnas)
- Improved some syntax correction.

## [3.1.1] - 2021-04-17

### Added
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@

Given a JSON string, this library will generate all the necessary Dart classes to parse and generate JSON. Also designed to generate Flutter-friendly model classes following the [Flutter's doc recommendation](https://flutter.io/json/#serializing-json-manually-using-dartconvert) and [Effective Dart: Style](https://dart.dev/guides/language/effective-dart/style). Extention supports for both **Serializing JSON manually** and **Serializing JSON** using code generation libraries like **Freezed** and **Json Serializable**. If you are an API service provider, you can build a `models.jsonc` file for your users to convert JSON to Dart language with a few clicks.

> **Note:** When you use `Freezed` or `Json Serializable` then `Json to Dart Model` generates only types and everything that happens after, then `Json Serializable` takes care of the rest and is responsible for generated code.
<!-- HOW IT WORKS -->
## How it Works

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.

2 changes: 1 addition & 1 deletion 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.1.1",
"version": "3.1.2",
"publisher": "hirantha",
"icon": "icon.png",
"engines": {
Expand Down
12 changes: 6 additions & 6 deletions src/syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,22 @@ const jsonParseClass = (key: string, typeDef: TypeDefinition, input: Input): str
if (typeDef.isDate) {
formatedValue += printLine(`.map((e) => ${parseDateTime("e")})`, true, tabs);
} else {
formatedValue += printLine(`.map((e) => ${buildParseClass(key, jsonValue, true)})`, true, tabs);
formatedValue += printLine(`.map((e) => ${buildParseClass(key, "e")})`, true, tabs);
}
} else {
if (typeDef.isDate) {
formatedValue += printLine(`?.map((e) => ${parseDateTime("e")})`, true, tabs);
} else {
formatedValue += printLine(`?.map((e) => ${buildParseClass(key, jsonValue, true)})`, true, tabs);
formatedValue += printLine(`?.map((e) => ${buildParseClass(key, "e")})`, true, tabs);
}
}
} else {
formatedValue += printLine(`?.map((e) => e == null`, true, 3 + 2 * result.length);
formatedValue += printLine(`? null`, true, 5 + 2 * result.length);
if (typeDef.isDate) {
formatedValue += printLine(`: ${parseDateTime(jsonValue)})`, true, 5 + 2 * result.length);
formatedValue += printLine(`: ${parseDateTime("e")})`, true, 5 + 2 * result.length);
} else {
formatedValue += printLine(`: ${buildParseClass(key, jsonValue)})`, true, 5 + 2 * result.length);
formatedValue += printLine(`: ${buildParseClass(key, "e")})`, true, 5 + 2 * result.length);
}
}
for (let i = 0; i < result.length - 1; i++) {
Expand Down Expand Up @@ -260,9 +260,9 @@ const buildToJsonClass = (expression: string, nullSafety: boolean = false): stri
: `${expression}?.toJson()`;
};

const buildParseClass = (className: string, expression: string, nullSafety: boolean = false): string => {
const buildParseClass = (className: string, expression: string): string => {
const name = pascalCase(className).replace(/_/g, "");
return `${name}.fromJson(${nullSafety ? "e" : expression} as Map<String, dynamic>)`;
return `${name}.fromJson(${expression} as Map<String, dynamic>)`;
};

const parseDateTime = (expression: string): string => {
Expand Down

0 comments on commit 1f94d5c

Please sign in to comment.