json2image

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 18, 2025 License: MIT Imports: 11 Imported by: 0

README

JSON2Image

一个将JSON数据转换为图片的Go库,支持自定义字体、颜色、样式和JSON裁剪功能。

功能特性

  • 🎨 多种字体支持:内置Monaco、微软雅黑、苹方、王壬金石字体,也支持自定义字体
  • 🌈 可配置颜色方案:支持自定义层级颜色和括号颜色
  • ✂️ JSON裁剪功能:支持复杂的路径规则,可提取JSON的特定部分
  • 🔧 灵活配置:通过选项模式轻松配置各种参数
  • 📱 多种输出格式:支持保存为PNG文件或输出为Base64字符串
  • 🔄 向后兼容:保持与旧版本API的兼容性

安装

go get github.com/BeCrafter/json2image

基本用法

使用默认配置
import "github.com/BeCrafter/json2image"

jsonData := `{
    "name": "John Doe",
    "age": 30,
    "hobbies": ["reading", "coding"]
}`

// 保存为文件
_, err := json2image.Json2Image(jsonData, nil, "output.png")

// 或获取Base64字符串
base64Str, err := json2image.Json2Image(jsonData, nil)

高级配置

自定义字体和样式
config := json2image.DefaultConfig().
    WithFont(json2image.FontTypePingFang).  // 使用苹方字体
    WithFontSize(16).                       // 字体大小
    WithLineHeight(24).                     // 行高
    WithPadding(30).                        // 内边距
    WithBackgroundColor(0.95, 0.95, 0.98)  // 背景色

_, err := json2image.Json2Image(jsonData, config, "custom.png")
自定义颜色方案
// 定义层级颜色
levelColors := [][3]float64{
    {0.1, 0.2, 0.8}, // 深蓝色
    {0.8, 0.1, 0.2}, // 深红色
    {0.1, 0.8, 0.2}, // 深绿色
}

// 定义括号颜色
braceColors := [][3]float64{
    {0.4, 0.5, 0.9}, // 浅蓝色
    {0.9, 0.4, 0.5}, // 浅红色
    {0.4, 0.9, 0.5}, // 浅绿色
}

config := json2image.DefaultConfig().
    WithLevelColors(levelColors).
    WithBraceLevelColors(braceColors).
    WithDefaultTextColor(0.2, 0.2, 0.2)

_, err := json2image.Json2Image(jsonData, config, "colors.png")
使用自定义字体
// 使用系统字体文件
config := json2image.DefaultConfig().
    WithCustomFont("/path/to/your/font.ttf")

_, err := json2image.Json2Image(jsonData, config, "custom_font.png")

JSON裁剪功能

JSON裁剪允许你提取JSON中的特定部分,支持复杂的路径规则:

jsonData := `{
    "users": [
        {
            "name": "Alice",
            "profile": {
                "email": "alice@example.com",
                "age": 25
            }
        },
        {
            "name": "Bob", 
            "profile": {
                "email": "bob@example.com",
                "age": 30
            }
        }
    ],
    "metadata": {
        "total": 2
    }
}`

// 定义裁剪规则
config := json2image.DefaultConfig().WithCropRules(
    "users[*].name",           // 所有用户的姓名
    "users[*].profile.email",  // 所有用户的邮箱
    "users[0].profile.age",    // 第一个用户的年龄
    "metadata.total",          // 总数
)

_, err := json2image.CropJson2Image(jsonData, config, "cropped.png")
裁剪规则语法
  • field.subfield - 访问嵌套字段
  • array[*] - 访问数组所有元素
  • array[0] - 访问数组第0个元素
  • array[0,2] - 访问数组第0和第2个元素
  • parent.*.field - 使用通配符访问所有子对象的字段

配置选项

字体类型
json2image.FontTypeMonaco     // Monaco字体
json2image.FontTypeMsyh       // 微软雅黑字体
json2image.FontTypePingFang   // 苹方字体
json2image.FontTypeWrjs       // 王壬金石字体
json2image.FontTypeCustom     // 自定义字体
链式配置方法
方法 说明
WithFont(fontType) 设置字体类型
WithCustomFont(path) 设置自定义字体路径
WithFontSize(size) 设置字体大小
WithLineHeight(height) 设置行高
WithPadding(padding) 设置内边距
WithBackgroundColor(r,g,b) 设置背景色
WithLevelColors(colors) 设置层级颜色
WithBraceLevelColors(colors) 设置括号颜色
WithDefaultTextColor(r,g,b) 设置默认文本颜色
WithCropRules(rules...) 设置裁剪规则

向后兼容

为了保持与旧版本的兼容性,我们提供了兼容的函数:

// 旧版本兼容
_, err := json2image.Json2ImageDefault(jsonData, "output.png")

// 裁剪功能兼容
rules := []string{"field1", "field2"}
_, err := json2image.CropJson2ImageDefault(jsonData, rules, "output.png")

API 参考

主要函数
Json2Image
func Json2Image(jsonData string, config *Config, outputPath ...string) (string, error)

将JSON数据转换为图片。

参数:

  • jsonData: JSON字符串
  • config: 配置选项,传入nil使用默认配置
  • outputPath: 可选的输出文件路径

返回值:

  • 如果提供了outputPath,返回空字符串和可能的错误
  • 如果未提供outputPath,返回Base64编码的图片数据
CropJson2Image
func CropJson2Image(jsonData string, config *Config, outputPath ...string) (string, error)

对JSON进行裁剪后转换为图片。配置中必须包含裁剪规则。

DefaultConfig
func DefaultConfig() *Config

返回默认配置实例。

示例项目

查看测试文件了解更多使用示例:

  • helper_test.go - 配置和字体测试
  • json2image_test.go - 图片生成测试
  • jsoncrop_test.go - JSON裁剪测试

注意事项

  1. 字体兼容性: 某些字体文件可能不受支持,建议使用标准的TTF或OTF格式
  2. 内存使用: 大型JSON数据可能消耗较多内存
  3. 临时文件: 内置字体会创建临时文件,函数执行完成后会自动清理
  4. 自定义字体: 使用自定义字体时,请确保字体文件存在且格式正确

许可证

本项目采用 [许可证名称] 许可证 - 查看 LICENSE 文件了解详情

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CropJson2Image

func CropJson2Image(jsonData string, config *Config, outputPath ...string) (string, error)

CropJson2Image 将裁剪后的数据转换为图片

func CropJson2ImageDefault added in v0.0.3

func CropJson2ImageDefault(jsonData string, rules []string, outputPath ...string) (string, error)

CropJson2ImageDefault 使用默认配置将裁剪后的JSON转换为图片(向后兼容)

func Json2Image

func Json2Image(jsonData string, config *Config, outputPath ...string) (string, error)

Json2Image 将JSON数据转换为图片 参数: - jsonData: JSON字符串 - config: 配置选项,如果为nil则使用默认配置 - outputPath: 输出路径(可选),如果不提供则返回base64字符串

func Json2ImageDefault added in v0.0.3

func Json2ImageDefault(jsonData string, outputPath ...string) (string, error)

Json2ImageDefault 使用默认配置将JSON转换为图片(向后兼容)

func JsonCrop

func JsonCrop(input interface{}, rules []string) ([]byte, error)

Types

type ColorConfig added in v0.0.3

type ColorConfig struct {
	LevelColors      [][3]float64 // LevelColors 各层级的颜色
	BraceLevelColors [][3]float64 // BraceLevelColors 括号的颜色
	DefaultTextColor [3]float64   // DefaultTextColor 默认文本颜色
}

ColorConfig 颜色配置

type ColoredLine

type ColoredLine struct {
	// contains filtered or unexported fields
}

ColoredLine 带颜色信息的行

type Config added in v0.0.3

type Config struct {
	Font      FontConfig  // Font 字体配置
	Image     ImageConfig // Image 图片配置
	Color     ColorConfig // Color 颜色配置
	CropRules []string    // CropRules 裁剪规则
}

Config 配置选项

func DefaultConfig added in v0.0.3

func DefaultConfig() *Config

DefaultConfig 返回默认配置

func (*Config) WithBackgroundColor added in v0.0.3

func (c *Config) WithBackgroundColor(r, g, b float64) *Config

WithBackgroundColor 设置背景色

func (*Config) WithBraceLevelColors added in v0.0.3

func (c *Config) WithBraceLevelColors(colors [][3]float64) *Config

WithBraceLevelColors 设置括号颜色

func (*Config) WithCropRules added in v0.0.3

func (c *Config) WithCropRules(rules ...string) *Config

WithCropRules 设置裁剪规则

func (*Config) WithCustomFont added in v0.0.3

func (c *Config) WithCustomFont(fontPath string) *Config

WithCustomFont 设置自定义字体

func (*Config) WithDefaultTextColor added in v0.0.3

func (c *Config) WithDefaultTextColor(r, g, b float64) *Config

WithDefaultTextColor 设置默认文本颜色

func (*Config) WithFont added in v0.0.3

func (c *Config) WithFont(fontType FontType) *Config

WithFont 设置字体类型

func (*Config) WithFontSize added in v0.0.3

func (c *Config) WithFontSize(size float64) *Config

WithFontSize 设置字体大小

func (*Config) WithLevelColors added in v0.0.3

func (c *Config) WithLevelColors(colors [][3]float64) *Config

WithLevelColors 设置层级颜色

func (*Config) WithLineHeight added in v0.0.3

func (c *Config) WithLineHeight(lineHeight float64) *Config

WithLineHeight 设置行高

func (*Config) WithPadding added in v0.0.3

func (c *Config) WithPadding(padding float64) *Config

WithPadding 设置内边距

type FontConfig added in v0.0.3

type FontConfig struct {
	Type       FontType // Type 字体类型
	CustomPath string   // CustomPath 自定义字体文件路径(当Type为FontTypeCustom时使用)
	Size       float64  // Size 字体大小
	LineHeight float64  // LineHeight 行高
}

FontConfig 字体配置

type FontType added in v0.0.3

type FontType int

FontType 表示字体类型

const (
	FontTypeMonaco   FontType = iota // FontTypeMonaco Monaco字体
	FontTypeMsyh                     // FontTypeMsyh 微软雅黑字体
	FontTypePingFang                 // FontTypePingFang 苹方字体
	FontTypeCustom                   // FontTypeCustom 自定义字体
)

type ImageConfig added in v0.0.3

type ImageConfig struct {
	Padding         float64    // Padding 内边距
	BackgroundColor [3]float64 // BackgroundColor 背景色
}

ImageConfig 图片配置

type PathStep

type PathStep struct {
	Key     string
	Indices []int // 将单个 Index 改为 Indices 数组
}

Directories

Path Synopsis
scripts
cmd command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL