venerik
venerik

Reputation: 5904

How to convert JSON to YAML using YamlDotNet

I am trying to convert JSON to YAML using YamlDotNet. This is the code I have:

class Program
{
    static void Main(string[] args)
    {
        var json = "{\"swagger\":\"2.0\",\"info\":{\"title\":\"UberAPI\",\"description\":\"MoveyourappforwardwiththeUberAPI\",\"version\":\"1.0.0\"},\"host\":\"api.uber.com\",\"schemes\":[\"https\"],\"basePath\":\"/v1\",\"produces\":[\"application/json\"]}";
        var swaggerDocument = JsonConvert.DeserializeObject(json);

        var serializer = new YamlDotNet.Serialization.Serializer();

        using (var writer = new StringWriter())
        {
            serializer.Serialize(writer, swaggerDocument);
            var yaml = writer.ToString();
            Console.WriteLine(yaml);
        }
    }
}

This is the JSON I provide:

{
   "swagger":"2.0",
   "info":{
      "title":"UberAPI",
      "description":"MoveyourappforwardwiththeUberAPI",
      "version":"1.0.0"
   },
   "host":"api.uber.com",
   "schemes":[
      "https"
   ],
   "basePath":"/v1",
   "produces":[
      "application/json"
   ]
}

This is the YAML I expect:

swagger: '2.0'
info:
  title: UberAPI
  description: MoveyourappforwardwiththeUberAPI
  version: 1.0.0
host: api.uber.com
schemes:
  - https
basePath: /v1
produces:
  - application/json

However, this is the output I get:

swagger: []
info:
  title: []
  description: []
  version: []
host: []
schemes:
- []
basePath: []
produces:
- []

I don't have a clue why all properties are empty arrays.

I also tried typed deserialization and serialization like this:

var specification = JsonConvert.DeserializeObject<SwaggerDocument>(json);
...
serializer.Serialize(writer, swaggerDocument, typeof(SwaggerDocument));

But that produces

{}

Any help is much appreciated.

Upvotes: 8

Views: 13638

Answers (7)

Yatharth Varshney
Yatharth Varshney

Reputation: 2103

You don't actually need to deserialize JSON into strongly typed object you can convert JSON to YAML using dynamic Expando object as well. Here is a small example:-

var json = "{
        'Name':'Peter',
        'Age':22,
        'CourseDet':{
                'CourseName':'CS',
                'CourseDescription':'Computer Science',
                },
        'Subjects':['Computer Languages','Operating Systems']
        }";

var expConverter = new ExpandoObjectConverter();
dynamic deserializedObject = JsonConvert.DeserializeObject<ExpandoObject>(json, expConverter);

var serializer = new YamlDotNet.Serialization.Serializer();
var yaml = serializer.Serialize(deserializedObject);

You can see a detailed explanation of both methods, i.e., using strongly typed object and dynamic object, here.

Upvotes: 19

Rudi Jansen van Vuuren
Rudi Jansen van Vuuren

Reputation: 455

var serializer = new SharpYaml.Serialization.Serializer();
var yaml = serializer.Deserialize(jObject.ToString());
return serializer.Serialize(yaml);

YAML supports JSON deserialization, therefor you can either convert the JObject to a string or just give the serializer your JSON directly without using JObject.

Upvotes: 0

Mike Schenk
Mike Schenk

Reputation: 1582

If you're starting with a JSON string, as the OP has, and you want idiomatic YAML output, it's worth noting that JSON is valid YAML, so YamlDotNet can load it directly, then a simple visitor can adjust the "style" before writing it back out.

using System;
using System.IO;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.RepresentationModel;

public static void Main()
{
    var json = "{\"swagger\":\"2.0\",\"info\":{\"title\":\"UberAPI\",\"description\":\"MoveyourappforwardwiththeUberAPI\",\"version\":\"1.0.0\"},\"host\":\"api.uber.com\",\"schemes\":[\"https\"],\"basePath\":\"/v1\",\"produces\":[\"application/json\"]}";
    var yaml = new YamlStream();
    yaml.Load(new StringReader(json));
    yaml.Accept(new JsonToYamlConverterVisitor());
    var outputYaml = new StringWriter();
    yaml.Save(outputYaml, false);
    outputYaml.Flush();
    var yamlString = outputYaml.ToString();
    Console.WriteLine(yamlString);
}

class JsonToYamlConverterVisitor : YamlVisitorBase
{
    public override void Visit(YamlScalarNode scalar)
    {
        if (scalar.Style == ScalarStyle.DoubleQuoted && (scalar.Value == "null" || scalar.Value == "true" || scalar.Value == "false"))
            scalar.Style = ScalarStyle.DoubleQuoted;
        else scalar.Style = ScalarStyle.Plain;
        base.Visit(scalar);
    }

    public override void Visit(YamlSequenceNode sequence)
    {
        sequence.Style = SequenceStyle.Block;
        base.Visit(sequence);
    }

    public override void Visit(YamlMappingNode mapping)
    {
        mapping.Style = MappingStyle.Block;
        base.Visit(mapping);
    }
}

which outputs

swagger: 2.0
info:
  title: UberAPI
  description: MoveyourappforwardwiththeUberAPI
  version: 1.0.0
host: api.uber.com
schemes:
- https
basePath: /v1
produces:
- application/json
...

Run this example on DotNetFiddle: https://dotnetfiddle.net/YUJjzQ

Upvotes: 0

T Laird-McConnell
T Laird-McConnell

Reputation: 416

FWIW I wrote a nuget library to make YamlDotNet work great with Json.Net, honoring all of the JSON.net serialization attributes.

    var yaml = YamlConvert.SerializeObject(obj);
    var obj2 = YamlConvert.DeserializeObject<T>(yaml);

It works by adding a YamlDotNet type serialization class for JTokens (JObject/JArray/JValue)

    var serializer = new SerializerBuilder()
         .WithTypeConverter(new JTokenYamlConverter())
         .Build();

Upvotes: 1

Rob
Rob

Reputation: 9979

I think there is problem when json deserialization returns JObject. Looks like yaml serializer doesn't like it.

I used deserialization with specified type as you mentioned JsonConvert.DeserializeObject<SwaggerDocument>(json) and this is what I get

Swagger: 2.0
Info:
  Title: UberAPI
  Description: MoveyourappforwardwiththeUberAPI
  Version: 1.0.0
Host: api.uber.com
Schemes:
- https
BasePath: /v1
Produces:
- application/json

This is my whole code:

class Program
{
    static void Main(string[] args)
    {
        var json = "{\"Swagger\":\"2.0\",\"Info\":{\"Title\":\"UberAPI\",\"Description\":\"MoveyourappforwardwiththeUberAPI\",\"Version\":\"1.0.0\"},\"Host\":\"api.uber.com\",\"Schemes\":[\"https\"],\"BasePath\":\"/v1\",\"Produces\":[\"application/json\"]}";
        var swaggerDocument = JsonConvert.DeserializeObject<SwaggerDocument>(json);
        
        var serializer = new YamlDotNet.Serialization.Serializer();

        using (var writer = new StringWriter())
        {
            serializer.Serialize(writer, swaggerDocument);
            var yaml = writer.ToString();
            Console.WriteLine(yaml);
        }
    }
}

public class Info
{
    public string Title { get; set; }
    public string Description { get; set; }
    public string Version { get; set; }
}

public class SwaggerDocument
{
    public string Swagger { get; set; }
    public Info Info { get; set; }
    public string Host { get; set; }
    public List<string> Schemes { get; set; }
    public string BasePath { get; set; }
    public List<string> Produces { get; set; }
}

update

Two issues here.

When deserializing class with fields, by default, json.net won't take them into consideration when doing this job. For this purpose we have to customize the deserialization process by creating a custom contract resolver. We can easily do this by

var swaggerDocument = JsonConvert.DeserializeObject<SwaggerDocument>(json, new JsonSerializerSettings
{
    ContractResolver = new MyContractResolver()
});

public class MyContractResolver : DefaultContractResolver
{
    protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
    {
        var props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
            .Select(p => base.CreateProperty(p, memberSerialization))
            .Union(type.GetFields(BindingFlags.Public | BindingFlags.Instance)
                .Select(f => base.CreateProperty(f, memberSerialization)))
            .ToList();
        props.ForEach(p => { p.Writable = true; p.Readable = true; });
        return props;
    }
}

There is second issue when we want to serialize class with fields: Values from fields won't be included into yaml result. I haven't figured out how to deal with this yet.

Do you have to use Swashbuckle.Swagger type or you can just create wrapper/decorator/DTO for this type?

I hope it helps you.

Upvotes: 3

darangor
darangor

Reputation: 19

I'm using the following piece of code to build a Yaml element from a JSON and writing it to a file.

Here is the code:

    public static void BuildParametrizedYAML(string element, string element1)
    {
        var jsonBreakers = @"
        {
            'watchers' : {
                'timer' : '10',
                'watcherPool' : '5',
                's3fileExtension' : '.avr.gz',
                'maxRetriesTask' : '3',
                'telemetryFolder' : '/data',
                'telemetryProcessor' : { 
                    'url' : '"+ element1 + @"'
                },
                'breakers' : 
                [
                    {
                        'breakerId' : 'COMMANDER',
                        'firstRetryTimeout' : '1000',
                        'secondRetryTimeout' : '6000',
                        'retries' : '5'
                    },
                    {
                        'breakerId' : 'PROCESSOR',
                        'firstRetryTimeout' : '1000',
                        'secondRetryTimeout' : '6000',
                        'retries' : '30'
                    }
                ],
                'servers' : 
                [
                    {
                        'serverId' : 'vmax',
                        'url' : '"+ element + @"'
                    }
                ]
            }
        }";

        var expConverter = new ExpandoObjectConverter();
        dynamic deserializedObject = JsonConvert.DeserializeObject<ExpandoObject>(jsonBreakers, expConverter);           
        var serializer = new Serializer();
        string JSONContent = serializer.Serialize(deserializedObject);

        var streamLoad = new StringReader(JSONContent);
        var stream = new YamlStream();
        stream.Load(streamLoad);

        using (TextWriter writer = File.CreateText("application.yml"))
        {
            stream.Save(writer, false);
        }
    }

And here is the output:

watchers:
  timer: 10
  watcherPool: 5
  s3fileExtension: .avr.gz
  maxRetriesTask: 3
  telemetryFolder: /data
  telemetryProcessor:
    url: TELEMETRYPROCESSORURL
  breakers:
  - breakerId: COMMANDER
    firstRetryTimeout: 1000
    secondRetryTimeout: 6000
    retries: 5
  - breakerId: PROCESSOR
    firstRetryTimeout: 1000
    secondRetryTimeout: 6000
    retries: 30
  servers:
  - serverId: vmax
    url: TELEMETRYWATCHERVMAXURL
...

Feel free to write me about this.

Upvotes: -1

Ed Ball
Ed Ball

Reputation: 2069

You can convert the JObject to a simpler object that YamlDotNet can serialize:

class Program
{
    static void Main(string[] args)
    {
        var json = "{\"swagger\":\"2.0\",\"info\":{\"title\":\"UberAPI\",\"description\":\"MoveyourappforwardwiththeUberAPI\",\"version\":\"1.0.0\"},\"host\":\"api.uber.com\",\"schemes\":[\"https\"],\"basePath\":\"/v1\",\"produces\":[\"application/json\"]}";
        var swaggerDocument = ConvertJTokenToObject(JsonConvert.DeserializeObject<JToken>(json));

        var serializer = new YamlDotNet.Serialization.Serializer();

        using (var writer = new StringWriter())
        {
            serializer.Serialize(writer, swaggerDocument);
            var yaml = writer.ToString();
            Console.WriteLine(yaml);
        }
    }

    static object ConvertJTokenToObject(JToken token)
    {
        if (token is JValue)
            return ((JValue)token).Value;
        if (token is JArray)
            return token.AsEnumerable().Select(ConvertJTokenToObject).ToList();
        if (token is JObject)
            return token.AsEnumerable().Cast<JProperty>().ToDictionary(x => x.Name, x => ConvertJTokenToObject(x.Value));
        throw new InvalidOperationException("Unexpected token: " + token);
    }
}

Upvotes: 7

Related Questions