Skip to content

Commit ae855e9

Browse files
committed
Add YAML<->JSON support.
1 parent 4cc2f0e commit ae855e9

File tree

4 files changed

+56
-15
lines changed

4 files changed

+56
-15
lines changed

package-lock.json

Lines changed: 6 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "An extension for Visual Studio Code that allows you to quickly convert text selections.",
44
"publisher": "mitchdenny",
55
"displayName": "Encode Decode",
6-
"version": "1.6.0",
6+
"version": "1.7.0",
77
"keywords": [
88
"encode",
99
"decode",
@@ -86,6 +86,7 @@
8686
"ent": "^2.2.0",
8787
"entities": "^2.1.0",
8888
"html-entities": "2.0.2",
89+
"js-yaml": "^3.13.1",
8990
"unicode-escape": "^0.1.0"
9091
}
9192
}

src/extension.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as hash from './lib/hash';
99
import * as unicode from './lib/unicode';
1010
import * as urlEncode from './lib/urlencode';
1111
import * as xmlentities from './lib/xmlentities';
12+
import * as yaml from './lib/yaml';
1213

1314
class Context {
1415
public failedChanges: Change[] = [];
@@ -144,7 +145,9 @@ function selectAndApplyTransformation(textEditor: vscode.TextEditor, edit: vscod
144145
new unicode.StringToUnicodeTransformer(),
145146
new unicode.UnicodeToStringTransformer(),
146147
new urlEncode.StringToEncodedUrlTransformer(),
147-
new urlEncode.EncodedUrlToStringTransformer()
148+
new urlEncode.EncodedUrlToStringTransformer(),
149+
new yaml.JsonToYamlTransformer(),
150+
new yaml.YamlToJsonTransformer()
148151
];
149152

150153
vscode.window.showQuickPick(transformers).then((transformer) => {

src/lib/yaml.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import * as core from './core';
2+
var yaml = require('js-yaml');
3+
4+
export class JsonToYamlTransformer implements core.Transformer {
5+
public get label(): string {
6+
return 'JSON to YAML';
7+
}
8+
9+
public get description(): string {
10+
return this.label;
11+
}
12+
13+
public check(input: string): boolean {
14+
return true;
15+
}
16+
17+
public transform(input: string): string {
18+
let parsedInput = JSON.parse(input);
19+
let output = yaml.dump(parsedInput);
20+
21+
return output;
22+
}
23+
}
24+
25+
export class YamlToJsonTransformer implements core.Transformer {
26+
public get label(): string {
27+
return 'YAML to JSON';
28+
}
29+
30+
public get description(): string {
31+
return this.label;
32+
}
33+
34+
public check(input: string): boolean {
35+
return true;
36+
}
37+
38+
public transform(input: string): string {
39+
var parsedInput = yaml.load(input);
40+
let output = JSON.stringify(parsedInput);
41+
42+
return output;
43+
}
44+
}

0 commit comments

Comments
 (0)