module

Telegram::JSONHelper

Comprehensive JSON serialization helper for Telegram Bot API

This module provides utilities for converting Crystal types to JSON-compatible formats while handling the specific requirements of the Telegram Bot API. It includes:

  • Safe type conversion with comprehensive error handling
  • Support for all Telegram API types including file uploads
  • Performance optimizations to minimize allocations
  • Validation for API-specific constraints

Example

class MyClient
  include Telegram::JSONHelper

  def send_message(text : String, keyboard : InlineKeyboardMarkup? = nil)
    params = build_request_hash(
      "text" => text,
      "reply_markup" => keyboard
    )
    # params will only include non-nil values
  end
end

Constants

CACHED_JSON_VALUES = {true => JSON::Any.new(true), false => JSON::Any.new(false), nil => JSON::Any.new(nil)} of Nil | Bool => JSON::Any

Cache for frequently used JSON::Any values Performance optimization for common boolean and nil values