c# - Using custom type resolver -
i'm trying deserialize yaml document standard .net objects, such string
scalar values , dictionary<string, object>
mappings, using yamldotnet library.
i guess deserializer
class best option, output object
, dictionary<object>
. tried implementing custom inodetyperesolver
this:
class mynodetypereslover : inodetyperesolver { bool inodetyperesolver.resolve(nodeevent nodeevent, ref type currenttype) { if (currenttype == typeof(object)) { if (nodeevent sequencestart) currenttype = typeof(list<object>); else if (nodeevent mappingstart) currenttype = typeof(dictionary<string, object>); else if (nodeevent scalar) currenttype = typeof(string); return true; } return false; } }
and using this:
deserializer deserializer = new deserializer(); deserializer.typeresolvers.add(new mynodetypereslover()); var res = deserializer.deserialize(input);
but doesn't seem have effect. there way change type of objects produced deserializer
?
afaik, deserialize takes type parameter, nice
%yaml 1.1 %tag !namespace! _mynamespace.nestedclass.whatever. --- entry_0: !namespace!mymessage format: alert desc: "entry one! uses exact string representation of desired type. (a bit fragile, imho)" entry_1: !!message format: default desc: "entry two! uses type registered beforehand." entry_2: format: default desc: "entry three! winging it, yamldotnet exceedingly clever." ...
can deserialized
var dict = new deserializer().deserialize<dictionary<string,mymessage>>( new stringreader(that_doc_up_there));
provided mymessage has format , desc property, , provided it's not in namespace. if is, can either register deserializer beforehand, or create new tag it. %tag alias seems eat first character of tag, put underscore. maybe bug. other way register it,
deserializer.registertagmapping( "tag:yaml.org,2002:message", typeof(mymessage));
Comments
Post a Comment