Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awesome/cd8a17f654c0c137ea3e7d97ab374ea2 to your computer and use it in GitHub Desktop.
Save awesome/cd8a17f654c0c137ea3e7d97ab374ea2 to your computer and use it in GitHub Desktop.
ruby-hash-with-or-without-rockets-to-javascript-object-notation-aka-json-style.rb
# ruby-hash-with-or-without-rockets-to-javascript-object-notation-aka-json-style.rb
#
# - context: https://alwayscoding.ca/momentos/2012/06/15/ruby-hash-syntax-hashrocket-vs-json-style
# - regex by SoAwesomeMane: https://rubular.com/r/9wlDeHLHejZvuB
# - regex research: https://sadique.io/blog/2013/09/29/named-capture-groups-in-regular-expressions
# - more regex research: https://www.regular-expressions.info/refquick.html "\k Named backreference"
# - json schema from https://www.liquid-technologies.com/online-json-to-schema-converter
# - 🍻🍻🍻
hash = {"$schema"=>"http://json-schema.org/draft-04/schema#",
"type"=>"object",
"properties"=>
{"_id"=>{"type"=>"string"},
"title"=>{"type"=>"string"},
"attachment"=>{"type"=>"string"},
"attachment_data"=>
{"type"=>"object",
"properties"=>
{"id"=>{"type"=>"string"},
"storage"=>{"type"=>"string"},
"metadata"=>
{"type"=>"object",
"properties"=>{"filename"=>{"type"=>"string"}, "size"=>{"type"=>"integer"}, "mime_type"=>{"type"=>"string"}, "duration"=>{"type"=>"string"}},
"required"=>["filename", "size", "mime_type", "duration"]}},
"required"=>["id", "storage", "metadata"]}},
"required"=>["_id", "title", "attachment", "attachment_data"]}
JSON.generate(hash).gsub(/(?<spaces>\s*)"(?<key>[^"]+)":/, '\k<spaces>\k<key>:')
# => "{$schema:\"http://json-schema.org/draft-04/schema#\",type:\"object\",properties:{_id:{type:\"string\"},title:{type:\"string\"},attachment:{type:\"string\"},attachment_data:{type:\"object\",properties:{id:{type:\"string\"},storage:{type:\"string\"},metadata:{type:\"object\",properties:{filename:{type:\"string\"},size:{type:\"integer\"},mime_type:{type:\"string\"},duration:{type:\"string\"}},required:[\"filename\",\"size\",\"mime_type\",\"duration\"]}},required:[\"id\",\"storage\",\"metadata\"]}},required:[\"_id\",\"title\",\"attachment\",\"attachment_data\"]}"
# use puts for maximum effect
pretty_str = JSON.pretty_generate(hash).gsub(/(?<spaces>\s*)"(?<key>[^"]+)":/, '\k<spaces>\k<key>:')
# => "{\n $schema: \"http://json-schema.org/draft-04/schema#\",\n type: \"object\",\n properties: {\n _id: {\n type: \"string\"\n },\n title: {\n type: \"string\"\n },\n attachment: {\n type: \"string\"\n },\n attachment_data: {\n type: \"object\",\n properties: {\n id: {\n type: \"string\"\n },\n storage: {\n type: \"string\"\n },\n metadata: {\n type: \"object\",\n properties: {\n filename: {\n type: \"string\"\n },\n size: {\n type: \"integer\"\n },\n mime_type: {\n type: \"string\"\n },\n duration: {\n type: \"string\"\n }\n },\n required: [\n \"filename\",\n \"size\",\n \"mime_type\",\n \"duration\"\n ]\n }\n },\n required: [\n \"id\",\n \"storage\",\n \"metadata\"\n ]\n }\n },\n required: [\n \"_id\",\n \"title\",\n \"attachment\",\n \"attachment_data\"\n ]\n}"
puts pretty_str
=begin
{
$schema: "http://json-schema.org/draft-04/schema#",
type: "object",
properties: {
_id: {
type: "string"
},
title: {
type: "string"
},
attachment: {
type: "string"
},
attachment_data: {
type: "object",
properties: {
id: {
type: "string"
},
storage: {
type: "string"
},
metadata: {
type: "object",
properties: {
filename: {
type: "string"
},
[…]
required: [
"_id",
"title",
"attachment",
"attachment_data"
]
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment