Skip to content

Commit b9f46f0

Browse files
authored
feat(julia): implement converter (#22)
1 parent 8eb89b8 commit b9f46f0

File tree

6 files changed

+484
-8
lines changed

6 files changed

+484
-8
lines changed

README.md

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# json2struct
22

3-
json2struct is a tool that translates JSON into type definitions. It currently supports translating to TypeScript and Python.
3+
json2struct is a tool that translates JSON into type definitions. It currently supports translating to TypeScript, Python and Julia.
44

55
The goal is for the definitions to be used as a starting point for the user, and not as a single source of truth.
66

@@ -86,7 +86,7 @@ To use another language pass the `--language` option.
8686
"map_key": { "key": "value" }
8787
}
8888

89-
$ npx json2struct example.json example.d.ts --language python
89+
$ npx json2struct example.json example.py --language python
9090
```
9191
9292
Writes the following to the output file:
@@ -108,9 +108,41 @@ class GeneratedStruct(TypedDict):
108108
string_key: str
109109
```
110110
111-
## Defaults
111+
#### Julia
112112
113-
If json2struct isn't able to determine the type of a given element, it tries to convert it to the closest type possible.
113+
```sh
114+
# example.json
115+
{
116+
"number_key": 1,
117+
"string_key": "json2struct",
118+
"boolean_key": true,
119+
"array_key": [42],
120+
"map_key": { "key": "value" }
121+
}
122+
123+
$ npx json2struct example.json example.jl --language julia
124+
```
125+
126+
Writes the following to the output file:
127+
128+
```julia
129+
# example.jl
130+
struct SubStruct1
131+
key::String
132+
end
133+
134+
struct GeneratedStruct
135+
array_key::Array{Int64}
136+
boolean_key::Bool
137+
map_key::SubStruct1
138+
number_key::Int64
139+
string_key::String
140+
end
141+
```
142+
143+
## Unknown values
144+
145+
If json2struct isn't able to determine the type of a given element, it tries to convert it to the closest type possible. In most languages this will be the `Any` type of the language.
114146
115147
### Empty arrays
116148
@@ -128,6 +160,12 @@ Array<unknown>;
128160
List[Any]
129161
```
130162
163+
#### Julia
164+
165+
```julia
166+
Array{Any}
167+
```
168+
131169
### Empty hashmaps
132170
133171
Hashmaps without keys will be converted to:
@@ -144,10 +182,18 @@ Record<string, unknown>;
144182
Dict[Any, Any]
145183
```
146184
185+
#### Julia
186+
187+
```
188+
Dict{Any,Any}
189+
```
190+
147191
## Notes
148192
149193
As said earlier the aim of this tool is to be used as a helper when writing type definitions, for that reason json2struct tries not to augment the output in any way.
150194
151195
One such example is not flattening the values of maps. In some cases it might make sense to flatten `[{ "a": 1 }, { "b": 1 }]` into `type GeneratedStruct = [{ a?: number; b?: number }];`. But in most cases flattening of maps requires having preexisting knowledge about the data that should be expected. For that reason json2struct prefers to let the user augment the type definition after, instead of imposing it's views on the user.
152196
153197
For the same reason json2struct does not take into account whether a key is actually valid in the given language.
198+
199+
Since this project is mostly meant to be a way for me to familiarize myself with different langauges, the types might not be the most optimal.

examples/example.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
struct SubStruct1
2+
key::String
3+
end
4+
5+
struct GeneratedStruct
6+
array_key::Array{Int64}
7+
boolean_key::Bool
8+
map_key::SubStruct1
9+
number_key::Int64
10+
string_key::String
11+
end

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "json2struct",
33
"version": "0.3.0",
4-
"description": "Easily translate JSON into type definitions. Currently supporting TypeScript and Python",
4+
"description": "Easily translate JSON into type definitions",
55
"main": "dist/index.js",
66
"bin": {
77
"json2struct": "./dist/index.js"
@@ -16,7 +16,8 @@
1616
"test:watch": "vitest",
1717
"local": "npm uninstall -g && npm install -g && json2struct",
1818
"example:typescript": "node dist/index.js ./examples/example.json ./examples/example.d.ts --language typescript --overwrite",
19-
"example:python": "node dist/index.js ./examples/example.json ./examples/example.py --language python --overwrite"
19+
"example:python": "node dist/index.js ./examples/example.json ./examples/example.py --language python --overwrite",
20+
"example:julia": "node dist/index.js ./examples/example.json ./examples/example.jl --language julia --overwrite"
2021
},
2122
"repository": {
2223
"type": "git",
@@ -27,7 +28,8 @@
2728
"types",
2829
"cli",
2930
"typescript",
30-
"python"
31+
"python",
32+
"julia"
3133
],
3234
"author": "Mads Hougesen",
3335
"license": "MIT",

0 commit comments

Comments
 (0)