当前位置: 首页 > news >正文

JSON dump in Ruby

 

In Ruby, the json library’s API differs from Python’s json.dump.

The equivalent in Ruby would be something like this:

require 'json'data = {name: "Christopher",message: "こんにちは世界"  # Japanese characters to test non-ASCII
}# Pretty-print with indentation and allow non-ASCII characters
json_str = JSON.pretty_generate(data, indent: '  ', ascii_only: false)puts json_str

 

Explanation:

  • JSON.pretty_generate(obj, opts = {}) → gives indentation and line breaks.

  • indent: ' ' → sets 2-space indentation.

  • ascii_only: false → prevents escaping non-ASCII characters (i.e., keeps UTF-8 characters intact).

  • Ruby doesn’t have ensure_ascii like Python, but ascii_only: false is the direct counterpart.

If instead you want to dump directly to a file (like Python’s json.dump does):

File.open("output.json", "w:utf-8") do |f|f.write(JSON.pretty_generate(data, indent: '  ', ascii_only: false))
end

 


Would you like me to also show you the non-pretty (compact but still UTF-8 safe) equivalent of Python’s json.dump(..., ensure_ascii=False) without indentation?

 

http://www.zskr.cn/news/13100.html

相关文章:

  • 详细介绍:python+django/flask+uniapp基于微信小程序的瑜伽体验课预约系统
  • GraphRAG对自然语言处理中深层语义分析的革命性影响与未来启示 - 教程
  • 详细介绍:云原生 vs 传统部署
  • 记录 | 关于陪伴型交互AI的一些探讨
  • luogu P1719 最大加权矩形
  • Laravel5.8 利用 snappyPDF 生成PDF文件
  • 数据结构——链表 - 详解
  • 25秋周总结4
  • 饥荒联机版
  • LinuxC++项目开发日志——基于正倒排索引的boost搜索引擎(5——通过cpp-httplib库建立网页模块) - 详解
  • 微信二次开发文档
  • 【底层机制】Android标准C库为什么选择 bionic 而不是 musl【一】 - 详解
  • DiffDock 环境安装和启用教程
  • 20250927
  • 详细介绍:CTFshow系列——PHP特性Web113-115(123)
  • [题解]P11533 [NOISG 2023 Finals] Topical
  • [题解]P10231 [COCI 2023/2024 #4] Putovanje
  • WPF Prism register interface and service, view and viewmodel, IRegionManager, RequestNavigate
  • 让YOLO飞起来:从CPU到GPU的配置指南
  • 忘形篇
  • 1748:约瑟夫问题
  • 候机的队伍
  • Keil uVision5 设置 hex 输出路径,不放Objects目录下
  • 垃圾收集器G1ZGC详解
  • gen-ui-python
  • 2025国内裱纸机厂家最新推荐排行榜:聚焦智能高速与全自动机型,权威精选综合实力 TOP3 厂家
  • 使用Windbg分析dmp文件的方法以及实战分析实例分享 - 教程
  • Vivado兼容第三方软件工具对照表Modelsim,Questasim,Matlab
  • 2025 年电脑租赁公司最新推荐排行榜:深度解析 TOP3 优质租电脑公司,助企业个人租赁电脑选择指南
  • 完整教程:✨WPF编程基础【1.2】:XAML中的属性