Convertidor de UBJSON a JSON

UBJSON a JSON

Acerca de UBJSON

¿Qué es UBJSON?

UBJSON es una codificación binaria basada en marcadores para valores JSON. Este convertidor decodifica null y booleanos, tipos enteros y flotantes, números de alta precisión, caracteres y strings, arrays, objetos, marcadores no-op, contenedores tipados o con recuento y arrays uint8 optimizados; al codificar también admite envoltorios JSON para datos binarios, int64, float64 y alta precisión.

¿Cuándo usar UBJSON?

Usa UBJSON principalmente para integraciones existentes. Confirma que el otro extremo admita contenedores tipados con $ y contenedores con recuento #, además del marcador H de alta precisión; este convertidor emite contenedores con recuento y arrays uint8 optimizados, y rechaza marcadores no compatibles o bytes sobrantes.

UBJSON en JavaScript

const UBJSON = require('ubjson');

// The npm package is legacy; use it mainly for compatibility with existing UBJSON data.
const value = { hello: 'world' };
const buffer = Buffer.alloc(1024);
const offset = UBJSON.packToBufferSync(value, buffer);
const encoded = buffer.subarray(0, offset);

console.log(encoded.toString('hex'));
UBJSON.unpackBuffer(encoded, (error, decoded) => {
	if (error) throw error;
	console.log(decoded);
});
			

UBJSON en Go

package main

import (
	"fmt"

	"github.com/jmank88/ubjson"
)

func main() {
	value := map[string]any{"hello": "world"}
	encoded, _ := ubjson.Marshal(value)
	fmt.Printf("%x\n", encoded)

	var decoded map[string]any
	_ = ubjson.Unmarshal(encoded, &decoded)
	fmt.Println(decoded["hello"])
	// Output: world
}
				

UBJSON en PHP

<?php
// PHP does not have a widely used maintained UBJSON package.
// This writes the common optimized uint8 array form used for binary payloads.
$bytes = "\x01\x02\x03";
$encoded = '[' . '$' . 'U' . '#' . 'U' . chr(strlen($bytes)) . $bytes;

echo bin2hex($encoded) . PHP_EOL;
// Output: 5b2455235503010203