简体   繁体   English

如何使用 yamldo.net 序列化动态 json object

[英]how to serialize dynamic json object with yamldotnet

i'm trying to creating yaml file from json using with YamlDotNet library, but i need to use dynamic object because i don't know what type of object they will send me,我正在尝试使用 YamlDotNet 库从 json 创建 yaml 文件,但我需要使用动态 object 因为我不知道他们会发送给我什么类型的 object,

I created base class for serialization like this:我为这样的序列化创建了 base class:

public class plugins
{
     public string name { get; set; }
     public object config { get; set; }
}

EDIT It worked when I edited it as StriplingWarrior said.编辑当我按照 StriplingWarrior 所说的那样编辑它时,它起作用了。

public class plugins
{
     public string name { get; set; }
     public IDictionary<string, object> config { get; set; }
}

EDIT 2编辑 2

i don't know my json file how will be like because of that i tried to use dynamic, when i use IDictionary<string, object> it's worked but when my json data have list inside list, result again empty, how i need to change my class or i need to write my own Converter?我不知道我的 json 文件会是什么样子,因为我尝试使用动态,当我使用IDictionary<string, object>它有效但是当我的 json 数据在列表中有列表时,结果再次为空,我如何需要更改我的 class 还是我需要编写自己的转换器?

    {
        "name": "correlation-id",
        "config": {
            "add": {
                "headers": [
                    "content-encoding:deflate,gzip",
                    "accept-encoding:deflate,gzip;q=1.0,*;q=0.5%"
                ]
            }
        }
    }

Result:结果:

    plugins:
    - name: correlation-id
      config:
        generator: uuid
        header_name: Aura-Correlation-Id
        echo_downstream: true
    - name: correlation-id
      config:
        config: data
        header_name: Aura-Correlation-Id
        val3: true
    - name: correlation-id
      config:
        add:
          headers:
          - []
          - []

EDIT 2 END编辑 2 结束

And my json values like that:我的 json 值是这样的:

[
    {
        "name": "corr-id",
        "config": {
            "generator": "uuid",
            "header_name": "-Id",
            "echo_downstream": true
        }
    },
    {
        "name": "cation-id2",
        "config": {
            "val4": "val3",
            "header_name": "Aura-Id",
            "echo_downstream": true,
            "title": "kube"
        }
    },
    {
        "name": "ation-id2",
        "config": {
            "val1": "val2",
            "title": "val3"
        }
    },
    {
        "name": "ati2",
        "config": {
            "contact": "some val",
            "group": "lenght",
            "title": "transform"
        }
    }
]

Final result like that:最终结果是这样的:

    plugins:

    - name: corr-id

      config:

        generator: []

        header_name: []

        echo_downstream: &o0 []

    - name: cation-id2

      config:

        val4: &o1 []

        header_name: []

        echo_downstream: *o0

        title: []

    - name: ation-id2

      config:

        val1: []

        title: *o1

    - name: ati2

      config:

        contact: []

        group: []

        title: []

I tried send to serializer json string but returned to me same data and i also tried made new JObject and add all values this object but it didnt worked.我尝试发送到序列化器 json 字符串,但返回给我相同的数据,我也尝试制作新的 JObject 并添加所有值这个 object 但它没有用。

Solution is:解决方案是:

Changed object to IDictionary<string, object>object更改为IDictionary<string, object>

public class plugins
{
     public string name { get; set; }
     public IDictionary<string, object> config { get; set; }
}

With Cinchoo ETL - an open source library, you can do do the conversion easily with few lines of code使用Cinchoo ETL - 一个开源库,您只需几行代码即可轻松完成转换

using (var r = new ChoJSONReader("*** JSON file path ***"))
{
    using (var w = new ChoYamlWriter("*** Yaml file path ***"))
    {
        dynamic rec = new ChoDynamicObject();
        rec.plugins = r.ToArray();
        
        w.Write(rec);
    }
}

Output: Output:

plugins:
  - name: corr-id
    config:
      generator: uuid
      header_name: -Id
      echo_downstream: true
  - name: cation-id2
    config:
      val4: val3
      header_name: Aura-Id
      echo_downstream: true
      title: kube
  - name: ation-id2
    config:
      val1: val2
      title: val3
  - name: ati2
    config:
      contact: some val
      group: lenght
      title: transform

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM