示例#1
0
        static void InterfaceTest()
        {
            string yaml = @"
Description: All automobiles
Name: All autos
Vehicles: 
    - type: Car
      make: BMW
    - type: Truck
      make: Volvo
";

            //ChoYamlReader.DeserializeText<Auto>(yaml, configuration:
            //    new ChoYamlRecordConfiguration().Map<Auto, Auto.F>(f => f.Vehicles, m => m))
            using (var parser = ChoYamlReader <Auto> .LoadText(yaml)
                                .WithField(f => f.Vehicles, itemRecordTypeSelector: (o) =>
            {
                dynamic rec = ChoDynamicObject.New(o as IDictionary <object, object>);
                if (rec.type == "Car")
                {
                    return(typeof(Car));
                }
                else
                {
                    return(typeof(Truck));
                }
            })
                   )
            {
                foreach (var e in parser)
                {
                    Console.WriteLine(e.Dump());
                }
            }
        }
示例#2
0
        static void DeserializeSubClassedItems()
        {
            string yaml = @"
Type: Item
Name: fuel_cansiter
DisplayName: Fuel Canister
Sprite: fuel_canister_1
MaxStackSize: 10
MaxHP: 15
Value: 30
Components:
      - Explosive:
        - Damage: 10
        - Range: 25
      - Burnable:
        - FlameSize: 10
        - HealthThreshold: 0.4
";


            using (var r = ChoYamlReader <Thing> .LoadText(yaml)
                           .WithField(f => f.Components, itemConverter: (o) =>
            {
                dynamic rec = ChoDynamicObject.New(o as IDictionary <object, object>);
                if (rec.ContainsKey(nameof(Explosive)))
                {
                    IList list = rec.Explosive.ToArray();
                    var dict = list.OfType <IDictionary <object, object> >().SelectMany(d => d.ToList()).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

                    return(dict.ConvertToObject(typeof(Explosive)));
                }
                else if (rec.ContainsKey(nameof(Burnable)))
                {
                    IList list = rec.Burnable.ToArray();
                    var dict = list.OfType <IDictionary <object, object> >().SelectMany(d => d.ToList()).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

                    return(dict.ConvertToObject(typeof(Burnable)));
                }
                else
                {
                    return(null);
                }
            })
                   )
            {
                foreach (var rec in r)
                {
                    Console.WriteLine(rec.Dump());
                }
            }
        }