Files
tinyblocks/libraries/tinyblocks-json/src/tb_json.c
T
mguinhos 1199d2d7c7 Adiciona tinyblocks-blueprint e libraries json/xml/csv; abrir/salvar/exportar projeto
- tinyblocks-blueprint: dona do estado de edicao (abas, pool de blocos,
  vinculos parent/prel/next/sub/args, blocos compostos e variaveis) e da
  serializacao. JSON nativo (to/from) usando a AST JSON do core e export
  JSON/XML/CSV. Testado com round-trip (test-blueprint).
- tinyblocks-json: lexer + parser + emissor JSON independentes.
- tinyblocks-xml: lexer + parser de XML -> AST JSON do core + emissor.
- tinyblocks-csv: divisor de linhas/campos + parser/emissor sobre a AST JSON.
- Editor: botoes Abrir / Salvar / Exportar JSON XML CSV no menu; converte
  Ed <-> TbBlueprint; barra de caminho com modo; .json pode ser arrastado.
- Variaveis: tb_variables_clear e tb_variables_set_text.
- Harness: teste de salvar/abrir projeto; tests/blueprint_test.c.
2026-10-20 19:51:48 -03:00

118 lines
4.2 KiB
C

/*
* TinyBlocks - tb_json.c
* Library tinyblocks-json: blocos Json.Get (consulta) e Json.Valid.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tinyblocks/tinyblocks.h>
#include "tb_json.h"
#define CAT_COLOR 240/255.f, 173/255.f, 78/255.f, 1.f
#define CAT_DARK 211/255.f, 132/255.f, 27/255.f, 1.f
/* Caminho "a.b[0].c" ou "a.0.c" sobre a arvore. */
static const JsonValue* path_walk(const JsonValue* root, const char* dotted) {
const JsonValue* v = root;
const char* p = dotted;
char part[128];
while (*p && v) {
size_t n = 0;
while (*p && *p != '.' && *p != '[' && n < sizeof(part) - 1) part[n++] = *p++;
part[n] = 0;
if (n) {
if (v->kind == JSON_OBJ) v = json_obj_get(v, part);
else if (v->kind == JSON_ARR) v = json_arr_at(v, (size_t)strtoul(part, NULL, 10));
else v = NULL;
}
if (v && *p == '[') {
p++;
size_t idx = 0;
while (*p >= '0' && *p <= '9') idx = idx * 10 + (size_t)(*p++ - '0');
if (*p == ']') p++;
v = json_arr_at(v, idx);
}
if (*p == '.') p++;
}
return v;
}
static const char* value_to_text(const JsonValue* v, char* buf, size_t cap) {
if (!v) { buf[0] = 0; return buf; }
switch (v->kind) {
case JSON_STR: snprintf(buf, cap, "%s", v->str ? v->str : ""); break;
case JSON_INT: snprintf(buf, cap, "%lld", v->ival); break;
case JSON_REAL: snprintf(buf, cap, "%g", v->fval); break;
case JSON_BOOL: snprintf(buf, cap, "%s", v->bval ? "true" : "false"); break;
case JSON_NULL: snprintf(buf, cap, "null"); break;
default: buf[0] = 0; break;
}
return buf;
}
static void eval_get(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_Value text = tb_block_get_input(self, rt, 0);
TinyBlocks_Value key = tb_block_get_input(self, rt, 1);
const char* src = (text.kind == TB_VALUE_TEXT && text.as.text) ? text.as.text : "";
const char* dotted = (key.kind == TB_VALUE_TEXT && key.as.text) ? key.as.text : "";
JsonLexError err;
JsonValue* doc = json_parse(src, &err);
char out[512] = "";
if (doc) {
value_to_text(path_walk(doc, dotted), out, sizeof(out));
json_free(doc);
}
tb_block_set_output(self, 0, tb_value_text(out));
self->dirty = false;
}
static void eval_valid(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_Value text = tb_block_get_input(self, rt, 0);
const char* src = (text.kind == TB_VALUE_TEXT && text.as.text) ? text.as.text : "";
JsonLexError err;
JsonValue* doc = json_parse(src, &err);
bool ok = doc != NULL;
json_free(doc);
tb_block_set_output(self, 0, tb_value_integer(ok ? 1 : 0));
self->dirty = false;
}
static bool json_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
static const TinyBlocks_Category cat = { "JSON", {CAT_COLOR}, {CAT_DARK} };
tb_library_set_category(lib, &cat);
static const TinyBlocks_SlotDesc get_in[] = {{"texto", TB_VALUE_TEXT}, {"chave", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc valid_in[] = {{"texto", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc out_val[] = {{"valor", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc out_bool[] = {{"valido", TB_VALUE_INTEGER}};
TinyBlocks_TypeBlock* a = tb_type_block_create(rt, "Json.Get", get_in, 2, out_val, 1, eval_get);
TinyBlocks_TypeBlock* b = tb_type_block_create(rt, "Json.Valid", valid_in, 1, out_bool, 1, eval_valid);
return a && b && tb_library_add(lib, &a->base) && tb_library_add(lib, &b->base);
}
static void json_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { (void)rt; (void)lib; }
TINYBLOCKS_DEFINE_LIBRARY(json,
"tinyblocks-json",
"1.0.0",
json_init,
json_deinit)
const TinyBlocks_LibraryDescriptor* tb_json_get_library(void) {
return &_tb_lib_desc_json;
}
const char* tb_json_get_path(const char* text, const char* dotted, const char* def) {
static char buf[256];
JsonLexError err;
JsonValue* doc = json_parse(text, &err);
if (!doc) return def;
const JsonValue* v = path_walk(doc, dotted);
if (!v) { json_free(doc); return def; }
value_to_text(v, buf, sizeof(buf));
json_free(doc);
return buf;
}