You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
I'm targetting AOT compilation and using YamlDotNet.
The restriction to use only dotnet runtime types is fine in this particular context and since the (de)serialized structure(s) is not known upfront, using only List, Dictionary<object,object> and primitives+string is ok.
Main issue is that out of the box the yamlcontext generators don't work for this use case and there is no default implementation.
Describe the solution you'd like
Ideally it should work by building a default (De)Serializer - instead of testing Dictionary<,> it can test with object too (same for lists) and it should be close to work.
Describe alternatives you've considered
Implement a custom static context (mainly a draft since it doesn't cover all cases):
usingYamlDotNet.Serialization;usingYamlDotNet.Serialization.ObjectFactories;usingYamlDotNet.Serialization.TypeInspectors;namespaceWorkaround.YamlDotNet;// note: aot generator doesn't work with generic containers so let's do it outselves// StaticDeserializerBuilder has fallbacks (Dictionary<object, object>) and List<object>)// in its constructor so let's just use them and only support thatpublicclassYamlContext:StaticContext{publicstaticreadonlyYamlContextInstance=new();publicoverrideboolIsKnownType(Typetype){returnfalse;}publicoverrideITypeResolverGetTypeResolver()=>SimpleTypeResolver.Instance;publicoverrideStaticObjectFactoryGetFactory()=>SimpleStaticObjectFactory.Instance;publicoverrideITypeInspectorGetTypeInspector()=>SimpleTypeInspector.Instance;}internalclassSimpleTypeInspector:TypeInspectorSkeleton{internalstaticreadonlySimpleTypeInspectorInstance=new();publicoverrideIEnumerable<IPropertyDescriptor>GetProperties(Typetype,object?container)=>[];publicoverridestringGetEnumName(TypeenumType,stringname)=>name;publicoverridestringGetEnumValue(objectenumValue)=>enumValue.ToString()!;}internalclassSimpleStaticObjectFactory:StaticObjectFactory{internalstaticreadonlySimpleStaticObjectFactoryInstance=new();publicoverrideobjectCreate(Typetype){if(typeof(Dictionary<object,object>)==type||typeof(IDictionary<object,object>)==type){returnnewOrderedDictionary<object,object>();}if(typeof(Dictionary<string,object>)==type||typeof(IDictionary<string,object>)==type){returnnewOrderedDictionary<string,object>();}if(typeof(List<object>)==type||typeof(IList<object>)==type){returnnewList<object>();}if(typeof(IList<IDictionary<string,object>>)==type){returnnewList<IDictionary<string,object>>();}thrownewInvalidOperationException($"Unknown type: '{type.FullName}'");}publicoverrideArrayCreateArray(Typetype,intcount){thrownewNotImplementedException("shoudn't be called");}publicoverrideboolIsDictionary(Typetype){returntype==typeof(Dictionary<object,object>)||type==typeof(IDictionary<object,object>)||type==typeof(IDictionary<string,object>)||type==typeof(IDictionary<string,string>)||type==typeof(Dictionary<string,string>)||type==typeof(IDictionary<string,int>)||type==typeof(Dictionary<string,int>)||type==typeof(IDictionary<string,bool>)||type==typeof(Dictionary<string,bool>)||type==typeof(IDictionary<string,double>)||type==typeof(Dictionary<string,double>)||type==typeof(IDictionary<object,string>)||type==typeof(Dictionary<object,string>)||type==typeof(IDictionary<object,int>)||type==typeof(Dictionary<object,int>)||type==typeof(IDictionary<object,bool>)||type==typeof(Dictionary<object,bool>)||type==typeof(IDictionary<object,double>)||type==typeof(Dictionary<object,double>);}publicoverrideboolIsArray(Typetype){returnfalse;}publicoverrideboolIsList(Typetype){returntype==typeof(List<object>)||type==typeof(IList<object>)||type==typeof(List<string>)||type==typeof(IList<string>)||type==typeof(List<bool>)||type==typeof(IList<bool>)||type==typeof(List<int>)||type==typeof(IList<int>);}publicoverrideTypeGetKeyType(Typetype){if(type==typeof(IDictionary<object,object>)||type==typeof(IDictionary<object,int>)||type==typeof(IDictionary<object,double>)||type==typeof(IDictionary<object,bool>)||type==typeof(Dictionary<object,object>)||type==typeof(Dictionary<object,int>)||type==typeof(Dictionary<object,double>)||type==typeof(Dictionary<object,bool>)){returntypeof(object);}if(type==typeof(IDictionary<string,object>)||type==typeof(IDictionary<string,int>)||type==typeof(IDictionary<string,double>)||type==typeof(IDictionary<string,bool>)||type==typeof(Dictionary<string,object>)||type==typeof(Dictionary<string,int>)||type==typeof(Dictionary<string,double>)||type==typeof(Dictionary<string,bool>)){returntypeof(string);}returntypeof(string);// normally it is always that so it is a safe default for us}publicoverrideTypeGetValueType(Typetype){if(type==typeof(Dictionary<object,object>)||type==typeof(List<object>)||type==typeof(IDictionary<object,object>)||type==typeof(IList<object>)){returntypeof(object);}if(type==typeof(List<IDictionary<string,object>>)||type==typeof(IList<IDictionary<string,object>>)){returntypeof(IDictionary<string,object>);}if(type==typeof(IDictionary<string,string>)||type==typeof(IDictionary<string,string>)||type==typeof(List<string>)||type==typeof(IList<string>)){returntypeof(string);}if(type==typeof(IDictionary<string,int>)||type==typeof(IDictionary<string,int>)||type==typeof(List<int>)||type==typeof(IList<int>)){returntypeof(int);}if(type==typeof(IDictionary<string,double>)||type==typeof(IDictionary<string,double>)||type==typeof(List<double>)||type==typeof(IList<double>)){returntypeof(double);}if(type==typeof(IDictionary<string,bool>)||type==typeof(IDictionary<string,bool>)||type==typeof(List<bool>)||type==typeof(IList<bool>)){returntypeof(bool);}thrownewInvalidOperationException($"Unknown type: '{type.FullName}'");}publicoverridevoidExecuteOnDeserializing(objectvalue){}publicoverridevoidExecuteOnDeserialized(objectvalue){}publicoverridevoidExecuteOnSerializing(objectvalue){}publicoverridevoidExecuteOnSerialized(objectvalue){}}internalclassSimpleTypeResolver:ITypeResolver{publicstaticreadonlySimpleTypeResolverInstance=new();publicTypeResolve(TypestaticType,object?value){returnstaticType;}}
The text was updated successfully, but these errors were encountered:
For the object data type, I’m going off of 2 maybe 3 year old memory, aot throws warnings when doing list and dictionary’s using the object type due to not being able to trim them down. That may not be the case anymore. We can for sure add defaults like you propose, I wonder how that would actually work in the end though. AoT really likes to know the type and what is used on that type to do any trimming, otherwise you’ll end up with wildly interests in your published binary. It’ll be a bit before I can revisit the static analyzer but I am willing to see what can be done. I’ve also thought about creating a composite static context that can reference multiple static contexts so things like this would be easier.
My understanding would be to do like json does, ie you register converters for types (list, string, ..., with generics) and when hit object you try to find the closest registered type or you go with YamlValue models but both options solve the issue to load in mem in aot mode an unknown structure.
So it is not about handling object itself but using missing object type as a kind of flag IMHO.
Is your feature request related to a problem? Please describe.
I'm targetting AOT compilation and using YamlDotNet.
The restriction to use only dotnet runtime types is fine in this particular context and since the (de)serialized structure(s) is not known upfront, using only List, Dictionary<object,object> and primitives+string is ok.
Main issue is that out of the box the yamlcontext generators don't work for this use case and there is no default implementation.
Describe the solution you'd like
Ideally it should work by building a default (De)Serializer - instead of testing Dictionary<,> it can test with object too (same for lists) and it should be close to work.
Describe alternatives you've considered
Implement a custom static context (mainly a draft since it doesn't cover all cases):
The text was updated successfully, but these errors were encountered: