Created
April 6, 2016 20:39
-
-
Save huttj/02ad59cd049c2312d86a5811a3a7723b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<link rel="stylesheet" href="style.css"> | |
<style> | |
textarea { | |
width: calc(100% - .5em); | |
height: 50vh; | |
} | |
pre { | |
width: calc(100% - 2em); | |
border: 1px solid #ccc; | |
padding: 1em; | |
border-radius: 6px; | |
} | |
</style> | |
</head> | |
<body> | |
<textarea id="input" cols="30" rows="10"></textarea> | |
<pre id="output"></pre> | |
<script type="text/javascript"> | |
var input = document.getElementById('input'); | |
var output = document.getElementById('output'); | |
input.addEventListener('change', handler); | |
input.addEventListener('keyup', handler); | |
function handler(e) { | |
var result; | |
try { | |
result = JSON.stringify(jsonToSchema(JSON.parse(e.target.value)), null, 2); | |
} catch (e) { | |
result = 'Error: ' + e.message; | |
} | |
output.textContent = result; | |
} | |
function jsonToSchema(obj) { | |
var type = typeof obj; | |
switch (type) { | |
case 'object': | |
for (var key in obj) { | |
obj[key] = jsonToSchema(obj[key]); | |
} | |
return obj; | |
default: | |
return type; | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Demo