Coder Social home page Coder Social logo

Comments (4)

EdwardCooke avatar EdwardCooke commented on June 11, 2024

With the current implementation of mapping this is probably going to get you as close as you can get. With a little more work you could probably get the first element in your value object list to output without a single quote another type converter. Maybe something like an argument object or something. But, here you go to get you started.

using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;

var serializer = new SerializerBuilder()
    .WithTagMapping("!FindInMap", typeof(List<object>))
    .WithTagMapping("!Ref", typeof(Baz))
    .WithDefaultScalarStyle(ScalarStyle.DoubleQuoted)
    .WithTypeConverter(new BazTypeConverter())
    .Build();

var list = new List<object>();
list.AddRange(new[] { "FOO", "BAR" });
list.Add(new Baz());

var outer = new Outer
{
    Value =
    [
        "-Djob.enebled=true",
        list
    ]
};

var yaml = serializer.Serialize(outer);
Console.WriteLine(yaml);

var deserializer = new DeserializerBuilder()
    .WithTagMapping("!FindInMap", typeof(List<object>))
    .WithTagMapping("!Ref", typeof(Baz))
    .WithTypeConverter(new BazTypeConverter())
    .Build();

Console.WriteLine("=== Deserialized");
var result = deserializer.Deserialize<Outer>(yaml);
foreach (var o in result.Value)
{
    if (o is List<object> list1)
    {
        foreach (var o2 in list1)
        {
            Console.WriteLine($"  {o2}");
        }
    }
    else
    {
        Console.WriteLine(o);
    }
}


class Outer
{
    public object[] Value { get; set; }
}


class BazTypeConverter : IYamlTypeConverter
{
    public bool Accepts(Type type) => type == typeof(Baz);

    public object? ReadYaml(IParser parser, Type type)
    {
        var valid = parser.TryConsume<Scalar>(out var value);
        if (!valid || value == null)
        {
            throw new Exception($"Invalid type, expected scalar got {value?.GetType()}");
        }
        if (value.Value != "Baz")
        {
            throw new Exception("Invalid Scalar Value");
        }
        return new Baz();
    }

    public void WriteYaml(IEmitter emitter, object? value, Type type)
    {
        emitter.Emit(new Scalar(null, "!Ref", "Baz", ScalarStyle.Plain, false, false));
    }
}
class Baz
{
    public override string ToString()
    {
        return "I'm a Baz";
    }
}

Results in

Value:
- "-Djob.enebled=true"
- !FindInMap
  - "FOO"
  - "BAR"
  - !Ref Baz

=== Deserialized
-Djob.enebled=true
  FOO
  BAR
  I'm a Baz

from yamldotnet.

EdwardCooke avatar EdwardCooke commented on June 11, 2024

Did the above answer your question?

from yamldotnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.