Skip to content

Instantly share code, notes, and snippets.

@onliniak
Created October 3, 2025 09:22
Show Gist options
  • Save onliniak/baba3cd451c27e5ad73e36ccd524c131 to your computer and use it in GitHub Desktop.
Save onliniak/baba3cd451c27e5ad73e36ccd524c131 to your computer and use it in GitHub Desktop.
require "json"
# Remember depth and key's name of JSON object
class GlobalContext
property global_context = {} of Float32 => String | Int32 | Float64 | JSON::Any
property key_or_value = {} of String => String
def start(parsed)
global_context[0.0] = parsed.first_key
# https://crystal-lang.org/api/1.16.3/Enumerable.html#each%28%26%3AT-%3E%29-instance-method
parsed[parsed.first_key].as_a.each do |value|
new_main_object(value.to_s)
end
end
def read_this_hash
pp global_context # ameba:disable Lint/DebugCalls
end
def order_sub_object(string_pair, index, max)
if index == 1 # first item
global_context[global_context.last_key.next_float] = key_or_value["key"]
end
# ToDo: Performance: Unecessary calculations
max.times do |i|
value = string_pair[i].split(",")[0] unless i <= 1
key = string_pair[i].split(",")[1] unless i <= 1
if global_context.has_value?(clean_json_string(value || "")) || global_context.has_value? ""
else
p clean_json_string value || ""
global_context[global_context.last_key + 0.1] = clean_json_string value || ""
end
if global_context.has_value?(clean_json_string(key || "")) || global_context.has_value? ""
else
global_context[global_context.last_key.next_float] = clean_json_string key || ""
end
end
if index == max # last item
global_context[global_context.last_key.next_float] = key_or_value["value"]
end
end
def put_this_into_sub_object(string_pair, index, max)
if string_pair[index].split(",").size == 1 # single value detected
cleaned_string = clean_json_string(string_pair[index]).strip
key_or_value["value"] = cleaned_string unless index == 1
key_or_value["key"] = cleaned_string
# elsif string_pair[index].split(",").size == 2 # key-value detected
order_sub_object string_pair, index, max + 1
end
end
def new_sub_object(object)
global_context[global_context.last_key + 1] = clean_json_string(object[0]).strip
object.each_with_index do |v, index|
put_this_into_sub_object(object, index, object.size - 2) unless v == object[0]
end
end
def new_main_object(string)
if string.split("=>").size == 2 # Empty object detected
global_context[global_context.last_key + 1] = clean_json_string(string).strip
else
new_sub_object string.split("=>")
end
end
def clean_json_string(string : String)
string.delete &.in?('{', "\"", '"', '=', '>', ',', '}')
end
end
json_s = %(
{
"row": [
{
"column": {}
},
{
"column": {
"text": "Hello World!",
"image": "img.png",
"smok": "wawelski",
"asas": "sdsddds"
}
}
]
}
)
global_context = GlobalContext.new
global_context.start(JSON.parse(json_s).as_h)
global_context.read_this_hash
{"image": "crystallang/crystal:1-alpine"}
@onliniak
Copy link
Author

onliniak commented Oct 3, 2025

Może głupio to zabrzmi ale miałem całe mnóstwo profesjonalnych backupów nazwanych projekt.zip, projekt(1).zip, projekt_backup.zip ...
Skończyłem pisać tę klasę dawno temu, miałem znaleźć chwilę by ją wrzucić i co się okazuje ?
To konkretne archiwum którego szukam nie istnieje, a ja mam dostęp jedynie do starej wersji bez komentarzy.

W skrócie chciałem sobie zrobić system szablonów w stylu deklaratywnym/XML. Coś jak:
<text><bold>pogrubiony</bold>zwykły tekst<cursive>kursywa</cursive></text>
I teraz muszę wiedzieć co od czego zależy i ile mam dzieci w danym miejscu.
System pokazywał mi całe drzewko w stylu Float32. Miałem też specjalną funkcję do sprawdzania, czy
dana liczba jest podobna do innej zmiennoprzecinkowej.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment