Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/CaiJingLong/json2dart
Browse files Browse the repository at this point in the history
  • Loading branch information
CaiJingLong committed Aug 8, 2018
2 parents 2a83862 + ad1aa77 commit 4716743
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 32 deletions.
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@

一个网页版本的json转dart库

不同于https://github.com/debuggerx01/JSONFormat4Flutter的是 这个是为了json_serializable创建的
页面较简陋
## 说明

不同于https://github.com/debuggerx01/JSONFormat4Flutter 的是:这个是为了json_serializable创建的
页面较简陋,功能完善😊

## todo
1. 格式化代码:完成
2. 编写格式化相关的逻辑
json_serializable 可以参考flutter官网json部分 [flutter](https://flutter.io/json)

项目的展示地址: https://caijinglong.github.io/json2dart/

项目使用dart web开发
## 说明
主体功能完结,有需要修改的地方欢迎留言


```
dart --version
Dart VM version: 2.0.0-dev.69.5 (Tue Jul 31 15:05:14 2018 +0200) on "macos_x64"
```

## license

Expand Down
1 change: 0 additions & 1 deletion lib/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class Generator {

void refreshTemplate(DefaultTemplate template) {
var fieldList = template.fieldList;
bool needRefresh = false;
fieldList.forEach((filed) {
if (filed is MapField) {
// filed.typeString
Expand Down
21 changes: 20 additions & 1 deletion lib/json_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ bool useJsonKey = true;

bool isCamelCase = true;

var downloadFileName = "";

const defaultValue = """{
"body": "",
"data": [1],
Expand Down Expand Up @@ -45,6 +47,7 @@ void main() {

InputElement eJsonKey = querySelector("#use_json_key");
InputElement eCamelCase = querySelector("#camelCase");
TextAreaElement result = querySelector("#result");

void onJsonKeyChange() {
useJsonKey = eJsonKey.checked;
Expand Down Expand Up @@ -78,12 +81,27 @@ void main() {
refreshData();

querySelector("#copy").onClick.listen((event) {
TextAreaElement result = querySelector("#result");
result.focus();
result.setSelectionRange(0, result.textLength);
document.execCommand("copy", null, "");
result.blur();
});

ButtonElement saveButton = querySelector("#save");
saveButton.onClick.listen((event) async {
Blob blob = Blob([result.value]);
// FileSystem _filesystem =
// await window.requestFileSystem(1024 * 1024, persistent: false);
// FileEntry fileEntry = await _filesystem.root.createFile('dart_test.csv');
// FileWriter fw = await fileEntry.createWriter();
// fw.write(blob);
// File file = await fileEntry.file();
AnchorElement saveLink = document.createElementNS("http://www.w3.org/1999/xhtml", "a");
saveLink.href = Url.createObjectUrlFromBlob(blob);
// saveLink.type = "download";
saveLink.download = downloadFileName;
saveLink.click();
});
}

void refreshData() {
Expand All @@ -107,6 +125,7 @@ void refreshData() {
var generator = Generator(string, entityClassName);
var dartCode = generator.makeDartCode();
var dartFileName = ("${generator.fileName}.dart");
downloadFileName = dartFileName;
querySelector("#file_name").text = "应该使用的文件名为: $dartFileName";

result.value = dartCode;
Expand Down
4 changes: 4 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ environment:

#dependencies:
# path: ^1.4.1
dependencies:
# Your other regular dependencies here
json_annotation: ^0.2.3

dev_dependencies:
build_runner: ^0.9.0
build_web_compilers: ^0.4.0
json_serializable: ^0.5.4
55 changes: 30 additions & 25 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
<html>

<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta name="scaffolded-by" content="https://github.com/google/stagehand"/>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="scaffolded-by" content="https://github.com/google/stagehand" />
<title>hello_world</title>
<link rel="stylesheet" href="styles.css">
<link rel="icon" href="favicon.ico">
Expand All @@ -15,30 +15,35 @@

<body>

<div>
<div id="input" class="textarea">
<div class="top">
将json粘贴至左边
<div>
<div id="input" class="textarea">
<div class="top">
将json粘贴至左边
</div>
<textarea id="json" title="json字符串"></textarea>
<div>
<button id="format">格式化</button>
</div>
</div>
<textarea id="json" title="json字符串"></textarea>
<div>
<button id="format">格式化</button>
<div id="output" class="textarea">
<div class="top"></div>
<div class="result_title">
类名称
<input id="out_entity_name" />
<input type="checkbox" id="use_json_key" />
<span id="check_label">jsonKey annotation</span>
<input type="checkbox" id="camelCase" />
<span id="camelCaseLabel">use camelCase</span>
</div>
<div id="file_name"></div>
<textarea id="result" title="结果"></textarea>
<div>
<button id="copy">复制</button>
<button id="save">下载</button>
</div>
</div>
</div>
<div id="output" class="textarea">
<div class="top"></div>
<div class="result_title">
类名称
<input id="out_entity_name"/>
<input type="checkbox" id="use_json_key"/><span id="check_label">jsonKey annotation</span>
<input type="checkbox" id="camelCase"/><span id="camelCaseLabel">use camelCase</span>
</div>
<div id="file_name"></div>
<textarea id="result" title="结果"></textarea>
<div><button id="copy">复制</button></div>
</div>

</div>
</div>
</body>

</html>

0 comments on commit 4716743

Please sign in to comment.