搜索插件、代码仓与 APP 分类

插件详情v3.0.1

json2yaml

Dart 包,用于将 JSON 数据渲染为 YAML,内置自动美化功能,并支持 Dart pubspec.yaml 和 pubspec.lock 约定

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
9.9万
喜欢数
12
Pub 分数
160

安装插件

flutter pub add json2yaml

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Dart 包,可将 JSON 数据转换为 YAML 格式并自动格式化。它支持 Dart 的 pubspec.yaml 和 pubspec.lock 文件中使用的约定,非常适合用于配置和依赖管理任务。该包可确保输出整洁、易读,并可无缝集成到跨多个平台的 Flutter 和 Dart 项目中。

适合场景

  • 将 JSON 配置转换为 YAML
  • 生成 pubspec.yaml 文件
  • 解析和渲染依赖项清单
  • 自动化配置文件生成
  • 在 JSON 和 YAML 格式之间同步数据

主要特点

  • YAML 输出的自动美化
  • 对 pubspec.yaml 和 pubspec.lock 约定的支持
  • 通过 flutter pub add 简单集成
  • 跨平台兼容性
  • 轻量且高效的解析
  • 保持数据结构完整性

适合对象

  • Dart 和 Flutter 开发人员
  • CI/CD 流水线自动化
  • 配置管理
  • 工具和代码生成脚本
  • 需要 JSON 到 YAML 转换的项目

使用建议

  • 仅限基本 YAML 格式
  • 无模式验证
  • 不支持复杂的 YAML 功能,例如锚点或别名
  • 取决于输入JSON的有效性
  • 可能不支持嵌套结构中的所有边缘情况

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

json2yaml Build Status pubspec_lock version

Dart package to render JSON data to YAML:

  • Correct handling of nested structures
  • Built-in automatic beautifer
  • Supports Dart pubspec.yaml conventions
  • Compatible with conventions imposed by Dart pubspec.lock generator

Known limitations

  • Strings (single line and multiline) are always formatted as they are, without word wrapping for better human readability

json2yaml()

json2yaml is the function to format JSON data to YAML.

  const developerData = {
    'name': "Martin D'vloper",
    'job': 'Developer',
    'skill': 'Elite',
    'employed': true,
    'foods': ['Apple', 'Orange', 'Strawberry', 'Mango'],
    'languages': {
      'perl': 'Elite',
      'python': 'Elite',
      'pascal': 'Lame',
    },
    'education': '4 GCSEs\n3 A-Levels\nBSc in the Internet of Things'
  };

  print(json2yaml(developerData));

Advanced usage: YAML formatting styles

json2yaml supports the optional argument to customize YAML formatting for various use cases. At the moment, it supports the three following formatting styles:

  • YamlStyle.generic (default) -- Default formatting style applicable in most cases
  • YamlStyle.pubspecYaml -- YAML formatting style following pubspec.yaml formatting conventions
  • YamlStyle.pubspecLock -- YAML formatting style following pubspec.lock formatting conventions

YAML style is supplied as an optional argument to json2yaml():

/// Yaml formatting control options
enum YamlStyle {
  generic,
  pubspecYaml,
  pubspecLock,
}

/// Converts JSON to YAML representation
String json2yaml(
  Map<String, dynamic> json, {
  YamlStyle yamlStyle = YamlStyle.generic,
});

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目