Search...r::JSON(3pm)

πŸ“¦
version 8.12 Β· 5 versions
, in debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

Search::Elasticsearch::Role::Serializer::JSON β€” bestie, it's a Serializer role for JSON modules. That's the whole vibe. It's the role that handles turning your Perl stuff into JSON and back again, no cap. ✨


🌸 VERSION 🌸

This is version 8.12 β€” that's the version, periodt. πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

Okay so here's the tea β˜•: this role takes your Perl data structures and encodes them into JSON strings, and it also takes JSON strings and decodes them back into Perl data structures. That's it, that's the job. Round trip, in and out, main character energy. πŸ”₯


πŸŽ€ METHODS πŸŽ€

πŸ’œ encode()

$bytes = $serializer->encode($ref);
$bytes = $serializer->encode($str);

The encode() method takes your array and hash refs and converts them into their JSON equivalents. Simple. Iconic.

But wait β€” if you hand it a string instead, it just gives you back that string as the UTF8 encoded version of itself. And the empty string and undef? Those get returned as is, untouched, living their best life. 🫢

✨ encode_pretty()

$bytes = $serializer->encode_pretty($ref);
$bytes = $serializer->encode_pretty($str);

Literally works exactly like encode() β€” same energy, same behaviour β€” except the JSON output comes out pretty-printed. So it's got the little indents and the nice formatting. It's giving ✨aesthetic✨. 🌈

πŸ“ encode_bulk()

$bytes = $serializer->encode_bulk([\%hash,\%hash,...]);
$bytes = $serializer->encode_bulk([$str,$str,...]);

So encode_bulk() expects an array ref of hashes or strings, okay bestie? Every single hash or string in there gets run through encode() first, and then they all get joined together by newline characters, and then a final newline character gets appended at the very end. πŸŽ€

Why tho? Because this is the special JSON format used for bulk requests. That's the whole point. It's the bulk-request format, iykyk. πŸ’…

🧚 decode()

$var = $serializer->decode($json_bytes);
$str = $serializer->decode($bytes);

Here's how decode() decides what to do: if the value you pass in looks like JSON β€” like, if it starts with a { or a [ character β€” then it gets decoded from JSON. But if it doesn't look like JSON, it just comes back as the UTF8 decoded version of itself. And just like with encode(), the empty string and undef get returned as is. 🌸

πŸ’€ Heads up: the check is literally just "does it start with { or [" β€” that's the vibe check it runs.

πŸ‘‘ AUTHOR πŸ‘‘

Enrico Zimuel <enrico.zimuel@elastic.co> β€” the legend who wrote this. Slay. πŸ’–


This software is Copyright (c) 2024 by Elasticsearch BV. 🫢

This is free software, licensed under:

The Apache License, Version 2.0, January 2004

Okay bestie, that's the whole page πŸ’– β€” this was Search...r::JSON(3pm) from the User Contributed Perl Documentation, running on perl v5.38.2, dated 2024-02-04. Go forth and serialize. Periodt. βœ¨πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from