Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
xianyu committed Jun 5, 2021
1 parent bd32b96 commit a3198ba
Show file tree
Hide file tree
Showing 30 changed files with 7,678 additions and 103 deletions.
111 changes: 8 additions & 103 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,104 +1,9 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
node_modules
.DS_Store
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
.idea
dist-ssr
*.local
*.lock
upgrade.sh
/yarn-error.log
10 changes: 10 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
*.md
*.json
*.yml
*.yaml
*.toal
*.ini
.idea
node_modules
test
dist
11 changes: 11 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"tabWidth": 4,
"useTabs": false,
"semi": false,
"singleQuote": true,
"bracketSpacing": true,
"jsxBracketSameLine": true,
"printWidth": 180,
"trailingComma": "none",
"arrowParens": "avoid"
}
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
# json-to-x
将json生成其他语言的数据结构。generating data structure of other languages with JSON.


## 参考

- [UI框架](https://primefaces.org/primevue/showcase/#/setup)
- [ace](https://ace.c9.io/#nav=api)
- [全局事件总线](https://vue3.chengpeiquan.com/communication.html#eventbus-new)
134 changes: 134 additions & 0 deletions __test__/codegen_json.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import { Scanner } from '../src/core/scanner'
import { Parser } from '../src/core/parse'
import { CodegenJson } from '../src/core/codegen_json'

// 生成json字符串
function run(s: string): string {
// 扫描器生成tokens
const scan = new Scanner(s)
// 将tokens解析成树结构
const parser = new Parser(scan.tokens)
// 生成json字符串
const codegen = new CodegenJson(parser.root)
return codegen.json
}

test('测试简单对象', () => {
const s = '{"a":1,"b":true,"c":false,"d":null,"e":-1.234,"f":"xxx"}'
expect(run(s)).toBe(s)
})

test('测试特殊对象', () => {
const s = '{"a":+1,"b":+0,"c":-0}'
expect(run(s)).toBe(`{"a":1,"b":0,"c":0}`)
})

test('测试空对象', () => {
const s = '{}'
expect(run(s)).toBe(s)
})

test('测试简单数组', () => {
const s = '[1,true,false,null,"xxx",0,1.23,-0.234]'
expect(run(s)).toBe(s)
})

test('测试空数组', () => {
const s = '[]'
expect(run(s)).toBe(s)
})

test('测试复杂对象-1', () => {
const s = `{"basic":{"null":null,"true":true,"false":false,"integer0":0,"integer1":23123,"integer2":-2434234,"float0":0.1232,"float1":231.34234,"float2":-31.1231,"float3":-0.2313,"string1":"this is string1","string2":"23121","string3":"sda3242;klk3423"},"complex":{"object1":{"key1":123,"key2":-1.2,"key3":true,"key4":false,"key5":null,"key6":"value","object2":{"arr":["1","2","3"],"arr2":[{"arr2_key1":-123.231,"arr2_key2":"2313","arr2_key3":"[3121]"}]}}},"array1":[1,2,3,4],"array2":[1.342,4.342,-123.34,34,0,-313],"array3":["aaa","bbb","ccc","dddd"],"array4":[true,null,231,"sd32",23.343],"array5":[[1,2,3],[4,5,6]],"array6":[["aaa","bbb"],["ccc"]],"array7":[[true,null],[0,false,null,3.4,-23]],"array8":[[{"key1":123,"key2":"sdad","key3":213.4}]],"array9":[[[{"key1":123,"key2":"sdad","key3":213.4}]]]}`
expect(run(s)).toBe(s)
})

test('测试复杂对象-2', () => {
const s = `{"a":1,"b":{"c":true,"d":null,"e":{"f":"xxx"}}}`
expect(run(s)).toBe(s)
})

test('测试复杂对象-3', () => {
const s = `{"c":[[{"d":[{"e":[true]}]}]]}`
expect(run(s)).toBe(s)
})

test('测试复杂对象-4', () => {
const s = `[{"input_index":0,"candidate_index":0,"delivery_line_1":"1 N Rosedale St","last_line":"Baltimore MD 21229-3737","delivery_point_barcode":"212293737013","components":{"primary_number":"1","street_predirection":"N","street_name":"Rosedale","street_suffix":"St","city_name":"Baltimore","state_abbreviation":"MD","zipcode":"21229","plus4_code":"3737","delivery_point":"01","delivery_point_check_digit":"3"},"metadata":{"record_type":"S","zip_type":"Standard","county_fips":"24510","county_name":"Baltimore City","carrier_route":"C047","congressional_district":"07","rdi":"Residential","elot_sequence":"0059","elot_sort":"A","latitude":39.28602,"longitude":-76.6689,"precision":"Zip9","time_zone":"Eastern","utc_offset":-5,"dst":true},"analysis":{"dpv_match_code":"Y","dpv_footnotes":"AABB","dpv_cmra":"N","dpv_vacant":"N","active":"Y"}},{"input_index":0,"candidate_index":1,"delivery_line_1":"1 S Rosedale St","last_line":"Baltimore MD 21229-3739","delivery_point_barcode":"212293739011","components":{"primary_number":"1","street_predirection":"S","street_name":"Rosedale","street_suffix":"St","city_name":"Baltimore","state_abbreviation":"MD","zipcode":"21229","plus4_code":"3739","delivery_point":"01","delivery_point_check_digit":"1"},"metadata":{"record_type":"S","zip_type":"Standard","county_fips":"24510","county_name":"Baltimore City","carrier_route":"C047","congressional_district":"07","rdi":"Residential","elot_sequence":"0064","elot_sort":"A","latitude":39.2858,"longitude":-76.66889,"precision":"Zip9","time_zone":"Eastern","utc_offset":-5,"dst":true},"analysis":{"dpv_match_code":"Y","dpv_footnotes":"AABB","dpv_cmra":"N","dpv_vacant":"N","active":"Y"}}]`
expect(run(s)).toBe(s)
})

test('测试复杂对象-5', () => {
const s = `[[[[[[[[[[[[[1]]]]]]]]]]]]]`
expect(run(s)).toBe(s)
})

test('测试复杂对象-6', () => {
const s = `[{"a":1231},{"a":1231},{"a":1231}]`
expect(run(s)).toBe(s)
})

test('测试复杂对象-7', () => {
const s = `{a:23,b:{"z":true,"x":[{p:-0.2,q:null}]}}`
expect(run(s)).toBe(`{"a":23,"b":{"z":true,"x":[{"p":-0.2,"q":null}]}}`)
})

test('测试复杂对象-8', () => {
const s = `{1:"a",2:"b",3:"c"}`
expect(run(s)).toBe(`{"1":"a","2":"b","3":"c"}`)
})

test('语法扩展-1', () => {
const s = `{
"a":123
"b":true
c:[1 2 3]
}`
expect(run(s)).toBe(`{"a":123,"b":true,"c":[1,2,3]}`)
})

test('语法扩展-2', () => {
const s = `[a b c {c:false} true null]`
expect(run(s)).toBe(`["a","b","c",{"c":false},true,null]`)
})

test('语法扩展-3', () => {
const s = `{
a:-1231.323
b: {
z:31
// 测试
p: true
123:"key是字符串"
}
}`
expect(run(s)).toBe(`{"a":-1231.323,"b":{"z":31,"p":true,"123":"key是字符串"}}`)
})

test('测试注释-1', () => {
const s = `{
"a": 123, // 数字
"b": "xxx",
"c": [ /* 数组 */
true,
23123,
0
]
}`
expect(run(s)).toBe(`{"a":123,"b":"xxx","c":[true,23123,0]}`)
})

test('测试注释-2', () => {
const s = `{
"a": 123, // 数字
"b": "xxx",
"c": [ /* 数组 */
true,
/* 注释1 */
23123,
0
// 注释2
]
}`
expect(run(s)).toBe(`{"a":123,"b":"xxx","c":[true,23123,0]}`)
})
20 changes: 20 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- 网页 Meta 标签生成器,Powered By https://www.dute.org -->
<title>JSON-TO-X</title>
<meta name="description" content="将json生成其他语言的数据结构。generating data structure of other languages with JSON." />
<meta name="keywords" content="json-to-go" />
<meta name="author" content="lhlyu" />
<meta name="language" content="zh-CN" />
<title>json-to-x</title>
</head>
<body>
<div id="app"></div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/src-noconflict/ace.js"></script>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
7 changes: 7 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
transform: {
'^.+\\.tsx?$': 'ts-jest'
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node']
}
43 changes: 43 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "json-to-x",
"version": "0.0.1",
"description": "generating data structure of other languages with JSON",
"author": "lhlyu",
"scripts": {
"fmt": "prettier --write .",
"dev": "vite",
"build": "vue-tsc --noEmit && vite build",
"serve": "vite preview",
"test": "jest",
"start": "rimraf dist && tsc && node dist/run.js"
},
"license": "MIT",
"dependencies": {
"ace-builds": "^1.4.12",
"clipboard": "^2.0.8",
"debounce": "^1.2.1",
"gofmt.js": "^0.0.2",
"mitt": "^2.1.0",
"primeicons": "^4.1.0",
"primevue": "^3.5.0",
"vue": "^3.0.11"
},
"devDependencies": {
"@types/ace": "^0.0.46",
"@types/debounce": "^1.2.0",
"@types/jest": "^26.0.23",
"@types/vfile-message": "^2.0.0",
"@vitejs/plugin-vue": "^1.2.2",
"@vue/compiler-sfc": "^3.0.11",
"autoprefixer": "^10.2.5",
"jest": "^27.0.4",
"postcss": "^8.2.15",
"postcss-loader": "^5.3.0",
"prettier": "^2.3.0",
"sass": "^1.32.13",
"ts-jest": "^27.0.2",
"typescript": "^4.2.4",
"vite": "^2.3.3",
"vue-tsc": "^0.1.4"
}
}
Loading

1 comment on commit a3198ba

@vercel
Copy link

@vercel vercel bot commented on a3198ba Jun 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.