├── .gitignore ├── preview.gif ├── .vscodeignore ├── CHANGELOG.md ├── .vscode ├── settings.json ├── launch.json └── tasks.json ├── tsconfig.json ├── README.md ├── test ├── index.ts └── extension.test.ts ├── LICENSE ├── package.json ├── src └── extension.ts └── vsc-extension-quickstart.md /.gitignore: -------------------------------------------------------------------------------- 1 | out 2 | node_modules -------------------------------------------------------------------------------- /preview.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sallar/vscode-json-to-js-object/HEAD/preview.gif -------------------------------------------------------------------------------- /.vscodeignore: -------------------------------------------------------------------------------- 1 | .vscode/** 2 | .vscode-test/** 3 | out/test/** 4 | test/** 5 | src/** 6 | **/*.map 7 | .gitignore 8 | tsconfig.json 9 | vsc-extension-quickstart.md 10 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | All notable changes to the "json-to-js-object" extension will be documented in this file. 3 | 4 | Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file. 5 | 6 | ## [Unreleased] 7 | - Initial release -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | // Place your settings in this file to overwrite default and user settings. 2 | { 3 | "files.exclude": { 4 | "out": false // set this to true to hide the "out" folder with the compiled JS files 5 | }, 6 | "search.exclude": { 7 | "out": true // set this to false to include "out" folder in search results 8 | } 9 | } -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "target": "es6", 5 | "outDir": "out", 6 | "lib": [ 7 | "es6" 8 | ], 9 | "sourceMap": true, 10 | "rootDir": "." 11 | }, 12 | "exclude": [ 13 | "node_modules", 14 | ".vscode-test" 15 | ] 16 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # VSCode Extension: JSON to JS Object 2 | 3 | This extension converts a JSON object to JavaScript object in Visual Studio Code. 4 | 5 | ## Install 6 | 7 | This extension is available for free in the [Visual Studio Code Marketplace](https://marketplace.visualstudio.com/items?itemName=sallar.json-to-js-object). 8 | 9 | ``` 10 | ext install json-to-js-object 11 | ``` 12 | 13 | ## Features 14 | 15 | + Converts JSON Object to JavaScript Object 16 | + Converts `snake-case` keys to `camelCase` keys while converting the object 17 | 18 | ## How To Use 19 | 20 | 1. Select a valid JSON object in your editor (if nothing is selected then the whole file is checked) 21 | 2. Choose `Convert JSON to JS Object` in the command palette (`Cmt/Ctrl+Shift+P`) 22 | 23 | ![](preview.gif) 24 | 25 | ## Keyboard Shortcut 26 | 27 | This extension does not define any keyboard shortcuts by default. However if you need one, you can use the `jsonToJSObject.convert` command to define your own shortcut. 28 | 29 | ## License 30 | 31 | This extension is released under the [MIT License](LICENSE). 32 | -------------------------------------------------------------------------------- /test/index.ts: -------------------------------------------------------------------------------- 1 | // 2 | // PLEASE DO NOT MODIFY / DELETE UNLESS YOU KNOW WHAT YOU ARE DOING 3 | // 4 | // This file is providing the test runner to use when running extension tests. 5 | // By default the test runner in use is Mocha based. 6 | // 7 | // You can provide your own test runner if you want to override it by exporting 8 | // a function run(testRoot: string, clb: (error:Error) => void) that the extension 9 | // host can call to run the tests. The test runner is expected to use console.log 10 | // to report the results back to the caller. When the tests are finished, return 11 | // a possible error to the callback or null if none. 12 | 13 | var testRunner = require('vscode/lib/testrunner'); 14 | 15 | // You can directly control Mocha options by uncommenting the following lines 16 | // See https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically#set-options for more info 17 | testRunner.configure({ 18 | ui: 'tdd', // the TDD UI is being used in extension.test.ts (suite, test, etc.) 19 | useColors: true // colored output from test results 20 | }); 21 | 22 | module.exports = testRunner; -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Sallar Kaboli 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | // A launch configuration that compiles the extension and then opens it inside a new window 2 | { 3 | "version": "0.1.0", 4 | "configurations": [ 5 | { 6 | "name": "Launch Extension", 7 | "type": "extensionHost", 8 | "request": "launch", 9 | "runtimeExecutable": "${execPath}", 10 | "args": ["--extensionDevelopmentPath=${workspaceRoot}" ], 11 | "stopOnEntry": false, 12 | "sourceMaps": true, 13 | "outFiles": [ "${workspaceRoot}/out/src/**/*.js" ], 14 | "preLaunchTask": "npm" 15 | }, 16 | { 17 | "name": "Launch Tests", 18 | "type": "extensionHost", 19 | "request": "launch", 20 | "runtimeExecutable": "${execPath}", 21 | "args": ["--extensionDevelopmentPath=${workspaceRoot}", "--extensionTestsPath=${workspaceRoot}/out/test" ], 22 | "stopOnEntry": false, 23 | "sourceMaps": true, 24 | "outFiles": [ "${workspaceRoot}/out/test/**/*.js" ], 25 | "preLaunchTask": "npm" 26 | } 27 | ] 28 | } 29 | -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | // Available variables which can be used inside of strings. 2 | // ${workspaceRoot}: the root folder of the team 3 | // ${file}: the current opened file 4 | // ${fileBasename}: the current opened file's basename 5 | // ${fileDirname}: the current opened file's dirname 6 | // ${fileExtname}: the current opened file's extension 7 | // ${cwd}: the current working directory of the spawned process 8 | 9 | // A task runner that calls a custom npm script that compiles the extension. 10 | { 11 | "version": "0.1.0", 12 | 13 | // we want to run npm 14 | "command": "npm", 15 | 16 | // the command is a shell script 17 | "isShellCommand": true, 18 | 19 | // show the output window only if unrecognized errors occur. 20 | "showOutput": "silent", 21 | 22 | // we run the custom script "compile" as defined in package.json 23 | "args": ["run", "compile", "--loglevel", "silent"], 24 | 25 | // The tsc compiler is started in watching mode 26 | "isWatching": true, 27 | 28 | // use the standard tsc in watch mode problem matcher to find compile problems in the output. 29 | "problemMatcher": "$tsc-watch" 30 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "json-to-js-object", 3 | "displayName": "JSON to JS Object", 4 | "description": "Convert a JSON Object to Javascript Object", 5 | "version": "0.0.4", 6 | "publisher": "sallar", 7 | "engines": { 8 | "vscode": "^1.10.0" 9 | }, 10 | "categories": [ 11 | "Formatters" 12 | ], 13 | "repository": { 14 | "type": "git", 15 | "url": "git+https://github.com/sallar/vscode-json-to-js-object.git" 16 | }, 17 | "homepage": "https://github.com/sallar/vscode-json-to-js-object#readme", 18 | "license": "MIT", 19 | "author": "Sallar Kaboli ", 20 | "activationEvents": [ 21 | "onCommand:jsonToJSObject.convert" 22 | ], 23 | "main": "./out/src/extension", 24 | "contributes": { 25 | "commands": [{ 26 | "command": "jsonToJSObject.convert", 27 | "title": "Convert JSON to JavaScript Object" 28 | }] 29 | }, 30 | "scripts": { 31 | "vscode:prepublish": "tsc -p ./", 32 | "compile": "tsc -watch -p ./", 33 | "postinstall": "node ./node_modules/vscode/bin/install", 34 | "test": "node ./node_modules/vscode/bin/test" 35 | }, 36 | "devDependencies": { 37 | "typescript": "^2.0.3", 38 | "vscode": "^1.0.0", 39 | "mocha": "^2.3.3", 40 | "@types/node": "^6.0.40", 41 | "@types/mocha": "^2.2.32" 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/extension.ts: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | import * as vscode from 'vscode'; 3 | 4 | export function convert(json: string) { 5 | try { 6 | // Try to parse, to see if it's real JSON 7 | JSON.parse(json); 8 | 9 | const regex = /\"([^"]+)\":/g; 10 | const hyphenRegex = /-([a-z])/g; 11 | 12 | return json.replace(regex, match => { 13 | return match 14 | .replace(hyphenRegex, g => g[1].toUpperCase()) 15 | .replace(regex, "$1:"); 16 | }); 17 | } catch (err) { 18 | return false; 19 | } 20 | } 21 | 22 | function convertCommand(editor: vscode.TextEditor, edit: vscode.TextEditorEdit) { 23 | const { selection, document } = editor; 24 | const text = selection.isEmpty ? 25 | document.getText() : 26 | document.getText(selection); 27 | 28 | const converted = convert(text); 29 | 30 | if (converted === false) { 31 | vscode.window.showInformationMessage('Please select a valid JSON Object.'); 32 | return; 33 | } 34 | 35 | edit.replace( 36 | selection.isEmpty ? 37 | new vscode.Range( 38 | new vscode.Position(0, 0), 39 | new vscode.Position(document.lineCount, 0) 40 | ) : 41 | selection, 42 | converted 43 | ); 44 | 45 | } 46 | 47 | export function activate(context: vscode.ExtensionContext) { 48 | 49 | context.subscriptions.push( 50 | vscode.commands.registerTextEditorCommand('jsonToJSObject.convert', convertCommand) 51 | ); 52 | 53 | } 54 | 55 | export function deactivate() { } 56 | -------------------------------------------------------------------------------- /test/extension.test.ts: -------------------------------------------------------------------------------- 1 | import * as assert from 'assert'; 2 | import * as vscode from 'vscode'; 3 | import * as myExtension from '../src/extension'; 4 | 5 | const jsonObj = ` 6 | { 7 | "$schema": "http://json-schema.org/draft-04/schema#", 8 | "title": "Product", 9 | "description": "A product from Acme's catalog", 10 | "type": "object", 11 | "snake-case": true, 12 | "properties": { 13 | "id": { 14 | "description": "The unique identifier for a product", 15 | "type": "integer" 16 | }, 17 | "name": { 18 | "description": "Name of the product", 19 | "type": "string" 20 | }, 21 | "price": { 22 | "type": "number", 23 | "minimum": 0, 24 | "exclusiveMinimum": true 25 | } 26 | }, 27 | "required": ["id", "name", "price"] 28 | } 29 | `; 30 | 31 | const jsObj = ` 32 | { 33 | $schema: "http://json-schema.org/draft-04/schema#", 34 | title: "Product", 35 | description: "A product from Acme's catalog", 36 | type: "object", 37 | snakeCase: true, 38 | properties: { 39 | id: { 40 | description: "The unique identifier for a product", 41 | type: "integer" 42 | }, 43 | name: { 44 | description: "Name of the product", 45 | type: "string" 46 | }, 47 | price: { 48 | type: "number", 49 | minimum: 0, 50 | exclusiveMinimum: true 51 | } 52 | }, 53 | required: ["id", "name", "price"] 54 | } 55 | `; 56 | 57 | suite("Extension Tests", () => { 58 | test("Replaces a JSON Object succesfully", () => { 59 | assert.equal(myExtension.convert(jsonObj), jsObj); 60 | }); 61 | }); 62 | -------------------------------------------------------------------------------- /vsc-extension-quickstart.md: -------------------------------------------------------------------------------- 1 | # Welcome to your first VS Code Extension 2 | 3 | ## What's in the folder 4 | * This folder contains all of the files necessary for your extension 5 | * `package.json` - this is the manifest file in which you declare your extension and command. 6 | The sample plugin registers a command and defines its title and command name. With this information 7 | VS Code can show the command in the command palette. It doesn’t yet need to load the plugin. 8 | * `src/extension.ts` - this is the main file where you will provide the implementation of your command. 9 | The file exports one function, `activate`, which is called the very first time your extension is 10 | activated (in this case by executing the command). Inside the `activate` function we call `registerCommand`. 11 | We pass the function containing the implementation of the command as the second parameter to 12 | `registerCommand`. 13 | 14 | ## Get up and running straight away 15 | * press `F5` to open a new window with your extension loaded 16 | * run your command from the command palette by pressing (`Ctrl+Shift+P` or `Cmd+Shift+P` on Mac) and typing `Hello World` 17 | * set breakpoints in your code inside `src/extension.ts` to debug your extension 18 | * find output from your extension in the debug console 19 | 20 | ## Make changes 21 | * you can relaunch the extension from the debug toolbar after changing code in `src/extension.ts` 22 | * you can also reload (`Ctrl+R` or `Cmd+R` on Mac) the VS Code window with your extension to load your changes 23 | 24 | ## Explore the API 25 | * you can open the full set of our API when you open the file `node_modules/vscode/vscode.d.ts` 26 | 27 | ## Run tests 28 | * open the debug viewlet (`Ctrl+Shift+D` or `Cmd+Shift+D` on Mac) and from the launch configuration dropdown pick `Launch Tests` 29 | * press `F5` to run the tests in a new window with your extension loaded 30 | * see the output of the test result in the debug console 31 | * make changes to `test/extension.test.ts` or create new test files inside the `test` folder 32 | * by convention, the test runner will only consider files matching the name pattern `**.test.ts` 33 | * you can create folders inside the `test` folder to structure your tests any way you want --------------------------------------------------------------------------------