简体   繁体   中英

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,

I created base class for serialization like this:

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

EDIT It worked when I edited it as StriplingWarrior said.

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

EDIT 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?

    {
        "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

And my json values like that:

[
    {
        "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.

Solution is:

Changed object to 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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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