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()); } } }
static void Test2() { string yaml = @" gc: - clean: - location: blah blah pattern: blah - location: blah blah pattern: blah dependencies: location: blah - solution: results: blah framework: blah test: configuration: blah results: blah blah "; using (var r = ChoYamlReader.LoadText(yaml)) { foreach (var rec in r) { Console.WriteLine(rec.Dump()); } } }
static void WriteDataTableTest() { string csv = @"Id, Name 1, Tom 2, Mark"; StringBuilder yaml = new StringBuilder(); using (var r = ChoCSVReader.LoadText(csv) .WithFirstLineHeader() ) { var dt = r.AsDataTable(); dt.Print(); using (var w = new ChoYamlWriter(yaml) //.ReuseSerializerObject(false) .UseYamlSerialization(true) ) { w.Write(dt); } } using (var r = ChoYamlReader.LoadText(yaml.ToString()) ) { r.AsDataTable().Print(); } Console.WriteLine(yaml.ToString()); }
static void TestTwoLists() { string yaml = @" creation: 2020-12-26 author: YannZeRookie people: - id: 1 name: Tom - id: 2 name: Mark cities: - id: 1 city: San Francisco state: CA country: USA - id: 2 city: Palo Alto state: CA country: USA - id: 3 city: Minneapolis state: MN country: USA "; using (var r = ChoYamlReader <Root> .LoadText(yaml) ) { foreach (var rec in r) { Console.WriteLine(rec.Dump()); } } }
static void List2DictTest() { string yaml = @" jedis: - Yoda - Qui-Gon Jinn - Obi-Wan Kenobi - Luke Skywalker "; using (var r = ChoYamlReader <IDictionary> .LoadText(yaml) .WithYamlPath("$.jedis") .Configure(c => c.CustomNodeSelecter = o => { dynamic d = o as dynamic; var x = ((IList)d.Value).OfType <object>().ToDictionary(kvp => kvp.ToString(), kvp => kvp); return(x); }) ) { foreach (var rec in r) { Console.WriteLine(rec.Dump()); } } }
static void Issue142() { string yaml = @" version: 1.00 test: cnf.assit: Test1 cnf.language: English1 cnf.enable: Default1 test2: cnf.assit: Test2 cnf.language: English2 cnf.enable: Default2 config: cnf.assit: CCC cnf.language: English cnf.enable: Default "; ChoYamlRecordConfiguration config = new ChoYamlRecordConfiguration(); config.ErrorMode = ChoErrorMode.ReportAndContinue; config.Encoding = Encoding.UTF8; using (var parser = ChoYamlReader <Root142> .LoadText(yaml, config)) { foreach (var e in parser) { Console.WriteLine(e.Dump()); } } }
static void SelectiveNodeTestX() { string yaml = @" users: - name: 1 teamname: Tom email: [email protected] players: [1, 2] scores: - id: 1 value: 100 - id: 2 value: 200 "; using (var r = ChoYamlReader <UserInfoX> .LoadText(yaml) .WithYamlPath("$.users[*]") .WithField(f => f.scores, itemConverter: o => o) ) { foreach (var rec in r) { Console.WriteLine(rec.Dump()); } } }
static void ReadDynamicData() { string yaml = @" possessions: - type: car description: color: blue doors: 4 - type: computer description: disk: 1 TB memory: 16 MB "; using (var parser = ChoYamlReader <OwnerData> .LoadText(yaml)) { foreach (var e in parser) { string carColor = (string)e.Possessions[0].Description["color"]; // blue foreach (var p in e.Possessions) { Console.WriteLine(p.Description.Dump()); } } } }
static void SelectiveNodeLoad() { string yaml = @" config_one: name: foo stuff: value config_two: name: bar random: value "; Console.WriteLine(ChoYamlReader.DeserializeText <ConfigOne>(yaml, "config_one").FirstOrDefault().Dump()); return; using (var r = ChoYamlReader <ConfigOne> .LoadText(yaml) .WithYamlPath("config_one") ) { foreach (var rec in r) { Console.WriteLine(rec.Dump()); } } }
static void NestedDictTest() { string yaml = @" requests: # first item of `requests` list is just a string - http://example1.com/ # second item of `requests` list is a dictionary - url: http://example.com/ method: GET "; using (var r = ChoYamlReader.LoadText(yaml) .WithYamlPath("$.requests[*]") .WithField("Value") .WithField("url") .WithField("method") ) { foreach (var rec in r) { Console.WriteLine(rec.Dump()); } } }
static void ToComplexDataTableTest() { using (var r = ChoYamlReader.LoadText(yamlText2) ) { var dt = r.AsDataTable(); Console.WriteLine(dt.Dump()); } }
static void DeserializeDictTest() { string yaml = @" id: 1 name: Tom "; Console.WriteLine(ChoYamlReader.DeserializeText <Dictionary <string, object> >(yaml).FirstOrDefault().Dump()); }
static void POCOTest() { StringBuilder json = new StringBuilder(); using (var r = ChoYamlReader <Customer> .LoadText(yamlText2)) { using (var w = new ChoJSONWriter(json)) w.Write(r); } Console.WriteLine(json.ToString()); }
static void DeserializeCollectioTest() { string yaml = @" emps: - Tom - Mark "; var emps = ChoYamlReader.DeserializeText <string>(yaml, "$.emps[*]").ToList(); Console.WriteLine(emps.Dump()); }
static void DefaultValueTest() { string yaml = @"name: buddhika"; using (var r = ChoYamlReader <A> .LoadText(yaml)) { foreach (var rec in r) { Console.WriteLine(rec.Dump()); } } }
static void Test1() { using (var r = ChoYamlReader <MyModel> .LoadText(yamlText1)) { foreach (var rec in r) { Console.WriteLine(rec.Dump()); } return; Console.WriteLine(ChoJSONWriter <MyModel> .ToTextAll(r)); } }
static void Yaml2CSV() { StringBuilder csv = new StringBuilder(); using (var r = ChoYamlReader.LoadText(yamlText2)) { using (var w = new ChoCSVWriter(csv) .WithFirstLineHeader() ) w.Write(r); } Console.WriteLine(csv.ToString()); }
static void Test10() { string yaml = @" %YAML 1.1 %TAG !u! tag:unity3d.com,2011: --- !u!1 &1679279224369103304 GameObject: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} serializedVersion: 6 m_Component: - component: {fileID: 4601260661584130271} - component: {fileID: 935130553128839326} - component: {fileID: 9068261088558206342} - component: {fileID: 5522740863684393372} m_Layer: 0 m_Name: Cube (1) m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 m_IsActive: 1 --- !u!4 &4601260661584130271 Transform: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1679279224369103304} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: - {fileID: 6194676242869225411} m_Father: {fileID: 0} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}"; using (var r = ChoYamlReader.LoadText(yaml) .Configure(c => c.YamlTagMapResolver = (s) => { return(s); }) ) { r.Print(); } }
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()); } } }
static void ArrayOfIntTest() { string yaml = @"episodes: [1, 2, 3, 4, 5, 6, 7]"; using (var r = ChoYamlReader <int> .LoadText(yaml) .WithYamlPath("$.episodes[*]") ) { foreach (var rec in r) { Console.WriteLine(rec.Dump()); } } }
static void DeserializeEscapedChar() { string yaml = @"0.1 : Value 1 0.2 : Value 2 0.3 : Value 3"; using (var r = ChoYamlReader.LoadText(yaml) ) { foreach (var rec in r) { Console.WriteLine(rec.Dump()); } } }
static void NestedObject() { string yaml = @"Blah: !Str Template: ""My {0} says {1}"" Args: [""dog"", !Str { Template: ""{0} and {1}"", Args: [""woof"", ""arf""] }]"; using (var r = ChoYamlReader <StrInterp> .LoadText(yaml) .WithYamlPath("$Blah") .ErrorMode(ChoErrorMode.IgnoreAndContinue) .WithTagMapping("!Str", typeof(StrInterp)) ) { var x = r.FirstOrDefault(); x.Print(); } }
static void SelectiveNodeTest() { StringBuilder json = new StringBuilder(); using (var r = ChoYamlReader.LoadText(yamlText2) .WithField("receipt") .WithField("date", fieldType: typeof(DateTimeOffset)) ) { //foreach (var rec in r) // Console.WriteLine(rec.Dump()); using (var w = new ChoJSONWriter(json)) w.Write(r); } Console.WriteLine(json.ToString()); }
static void DeserializeTypedYaml() { string yaml = @" !Item Name: fuel_cansiter DisplayName: Fuel Canister Components: - !Explosive Damage: 10 Range: 25 - !Burnable FlameSize: 10 HealthThreshold: 6 "; List <Thing1> list = null; using (var r = ChoYamlReader <Thing1> .LoadText(yaml) .WithTagMapping("!Item", typeof(Thing1)) .WithTagMapping("!Explosive", typeof(Explosive)) .WithTagMapping("!Burnable", typeof(Burnable)) ) { list = r.ToList(); foreach (var rec in list) { Console.WriteLine(rec.Dump()); } } StringBuilder yamlOut = new StringBuilder(); using (var w = new ChoYamlWriter <Thing1>(yamlOut) .WithTagMapping("!Item", typeof(Thing1)) .WithTagMapping("!Explosive", typeof(Explosive)) .WithTagMapping("!Burnable", typeof(Burnable)) .ErrorMode(ChoErrorMode.IgnoreAndContinue) .Configure(c => c.YamlSerializerSettings.EmitTags = true) .UseYamlSerialization() ) { w.Write(list); } Console.WriteLine(yamlOut.ToString()); }
static void SecondaryTagTest() { string yaml = @" !!ControlGroup name: myGroup "; //settings.RegisterAssembly(typeof(Author).Assembly); using (var r = ChoYamlReader <ControlGroup> .LoadText(yaml) //.WithTagMapping("tag:yaml.org,2002:ControlGroup", typeof(ControlGroup)) //.Configure(c => c.TurnOffAutoRegisterTagMap = true) .UseYamlSerialization() ) { r.Print(); } }
static void DifferentDateFormatTest() { CultureInfo newCulture = CultureInfo.CreateSpecificCulture("en-GB"); //Thread.CurrentThread.CurrentCulture = newCulture; StringBuilder json = new StringBuilder(); using (var r = ChoYamlReader <CustomerWithDate> .LoadText(yamlText3)) { r.First().Date.ToString("yyyy-MMM-dd").Print(); return; using (var w = new ChoJSONWriter(json)) w.Write(r); } Console.WriteLine(json.ToString()); }
static void LoadDictKeysTest() { string yaml = @" Age: 15 Gender: Male "; using (var r = ChoYamlReader.LoadText(yaml) .WithYamlPath("$.^") ) { foreach (var rec in r) { Console.WriteLine(rec.Dump()); } } }
static void YamlDocTest() { YamlStream sr = new YamlStream(); sr.Load(new StringReader(yamlText2)); StringBuilder json = new StringBuilder(); using (var r = new ChoYamlReader(sr.Documents.First()) .WithField("receipt") .WithField("date", fieldType: typeof(DateTime)) ) { using (var w = new ChoJSONWriter(json)) w.Write(r); } Console.WriteLine(json.ToString()); }
static void ToDataTableTest() { string yaml = @" emps: - id: 1 name: Tom - id: 2 name: Mark "; using (var r = ChoYamlReader.LoadText(yaml) .WithYamlPath("$.emps[*]") ) { var dt = r.AsDataTable(); Console.WriteLine(dt.Dump()); } }
static void YamlPathTest() { StringBuilder json = new StringBuilder(); using (var r = ChoYamlReader <Item> .LoadText(yamlText2) .WithYamlPath("$items[*]") .IgnoreFieldValueMode(ChoIgnoreFieldValueMode.Empty) //.Configure(c => c.StringComparer = StringComparer.CurrentCulture) //.WithField("part_no", yamlPath: ".items[0].part_no") //.WithField("bill_street", yamlPath: ".bill-to.Street") //.WithField("price", fieldType: typeof(Double)) ) { using (var w = new ChoJSONWriter(json)) w.Write(r); } Console.WriteLine(json.ToString()); }