Is there a way to deserialize a specific property of a YAML document?

754 views Asked by At

In JSON.Net I can read a JSON file into a data structure and then only convert the properties that I'm interested in to objects. For example, if I have a JSON file like

{
    "Bob": {
               "Steve": 5
           }
}

I can get the object like so:

class SteveObject {
    public int Steve;
}

var jsonSerializer = JsonSerializer.Create()
var jsonFile = JObject.Parse(text);
vat steveObject = jsonFile["Bob"].ToObject<SteveObject>(jsonSerializer)

How do I do the same thing with YAMLDotNet or SharpYaml? I.e. if I have a YAML file like

Bob:
  Steve: 5

How do I reconstruct the SteveObject from that without having to create the outer object?

1

There are 1 answers

0
reveazure On

Ok, I've written some code using SharpYaml that takes a simplified JPath query and returns the requested object.

    public static T ReadPath<T>(Serializer serializer, string yaml, string path) {
        var eventReader = new EventReader(new Parser(new StringReader(yaml)));

        var streamReader = new MemoryStream(Encoding.UTF8.GetBytes(path ?? ""));
        if ((char)streamReader.ReadByte() != '$')
            throw new Exception();

        if (streamReader.Position == streamReader.Length)
            return serializer.Deserialize<T>(eventReader);

        eventReader.Expect<StreamStart>();
        eventReader.Expect<DocumentStart>();

        while (streamReader.Position < streamReader.Length) {
            if ((char)streamReader.ReadByte() != '.')
                throw new Exception();

            eventReader.Expect<MappingStart>();

            var currentDepth = eventReader.CurrentDepth;

            var nextKey = ReadPropertyName(streamReader);
            if (string.IsNullOrEmpty(nextKey))
                throw new Exception();

            while (eventReader.Peek<Scalar>() == null || eventReader.Peek<Scalar>().Value != nextKey) {
                eventReader.Skip();

                // We've left the current mapping without finding the key.
                if (eventReader.CurrentDepth < currentDepth)
                    throw new Exception();
            }

            eventReader.Expect<Scalar>();
        }

        return serializer.Deserialize<T>(eventReader);
    }

    public static string ReadPropertyName(MemoryStream stream) {
        var sb = new StringBuilder();

        while (stream.Position < stream.Length) {
            var nextChar = (char) stream.ReadByte();
            if (nextChar == '.') {
                stream.Position--;
                return sb.ToString();
            }

            sb.Append(nextChar);
        }

        return sb.ToString();
    }

Given the above YAML the following works:

var t = "Bob:\r\n  Point: [1.0, 2.0, 3.0]\r\n  Steve: 5\r\n";
var serializer = new Serializer(settings);
var s = ReadPath<SteveObject>(serializer, t, "$.Bob"); // { "Steve": 5 }
var s2 = ReadPath<int>(serializer, t, "$.Bob.Steve"); // 5

Sure wish there were a library for this, though.