将yaml转换为json而不使用struct

huy*_*uan 5 json yaml go

Services: 
-   Orders: 
    -   ID: $save ID1
        SupplierOrderCode: $SupplierOrderCode
    -   ID: $save ID2
        SupplierOrderCode: 111111
Run Code Online (Sandbox Code Playgroud)

我想将这个yaml字符串转换为json,因为源数据是动态的,所以我无法将它映射到结构:

var body interface{}
err := yaml.Unmarshal([]byte(s), &body)
Run Code Online (Sandbox Code Playgroud)

然后我想再次将该接口转换为json字符串:

b, _ := json.Marshal(body)
Run Code Online (Sandbox Code Playgroud)

但是会发生错误:

panic: json: unsupported type: map[interface {}]interface {}
Run Code Online (Sandbox Code Playgroud)

icz*_*cza 20

前言:我对以下解决方案进行了优化和改进,并在此处将其作为库发布:github.com/icza/dyno.以下convert()功能可用dyno.ConvertMapI2MapS().


问题是,如果使用最通用的interface{}类型来解组,则github.com/go-yaml/yaml包用于解组键值对的默认类型将是map[interface{}]interface{}.

第一个想法是使用map[string]interface{}:

var body map[string]interface{}
Run Code Online (Sandbox Code Playgroud)

但是,如果yaml配置的深度超过1,则此尝试将失败,因为此body映射将包含其类型将再次出现的其他映射map[interface{}]interface{}.

问题是深度未知,并且可能存在除地图之外的其他值,因此使用map[string]map[string]interface{}并不好.

一种可行的方法是将yamlunmarshal解组为type类型的值interface{},并以递归方式遍历结果,并将遇到的每个值转换map[interface{}]interface{}map[string]interface{}值.必须处理地图和切片.

以下是此转换器功能的示例:

func convert(i interface{}) interface{} {
    switch x := i.(type) {
    case map[interface{}]interface{}:
        m2 := map[string]interface{}{}
        for k, v := range x {
            m2[k.(string)] = convert(v)
        }
        return m2
    case []interface{}:
        for i, v := range x {
            x[i] = convert(v)
        }
    }
    return i
}
Run Code Online (Sandbox Code Playgroud)

并使用它:

func main() {
    fmt.Printf("Input: %s\n", s)
    var body interface{}
    if err := yaml.Unmarshal([]byte(s), &body); err != nil {
        panic(err)
    }

    body = convert(body)

    if b, err := json.Marshal(body); err != nil {
        panic(err)
    } else {
        fmt.Printf("Output: %s\n", b)
    }
}

const s = `Services:
-   Orders:
    -   ID: $save ID1
        SupplierOrderCode: $SupplierOrderCode
    -   ID: $save ID2
        SupplierOrderCode: 111111
`
Run Code Online (Sandbox Code Playgroud)

输出:

Input: Services:
-   Orders:
    -   ID: $save ID1
        SupplierOrderCode: $SupplierOrderCode
    -   ID: $save ID2
        SupplierOrderCode: 111111

Output: {"Services":[{"Orders":[
    {"ID":"$save ID1","SupplierOrderCode":"$SupplierOrderCode"},
    {"ID":"$save ID2","SupplierOrderCode":111111}]}]}
Run Code Online (Sandbox Code Playgroud)

有一点需要注意:通过Go映射从yaml切换到JSON,你将失去项目的顺序,因为Go map中的元素(键值对)不是有序的.这可能是也可能不是问题.


小智 10

http://sigs.k8s.io/yaml是“go-yaml 的包装器,旨在在与结构之间进行编组时能够更好地处理 YAML”。除其他外,它提供yaml.YAMLToJSON了应该做你想做的方法。