/// <summary> /// Construct YAML node tree that represents a given C# object. /// </summary> /// <param name="node"><see cref="YamlNode"/> to be converted to C# object.</param> /// <param name="expected">Expected type for the root object.</param> /// <param name="config"><see cref="YamlConfig"/> to customize serialization.</param> /// <returns></returns> public object NodeToObject(YamlNode node, Type expected, SerializerContext context) { this.context = context; this.config = context.Config; var appeared = new Dictionary<YamlNode, object>(TypeUtils.EqualityComparerByRef<YamlNode>.Default); return NodeToObjectInternal(node, expected, appeared); }
public void Add(string anchor_name, YamlNode node) { if ( Items.ContainsKey(anchor_name) ) { // override an existing anchor ItemsToRewind.Push(new RewindInfo(anchor_name, this[anchor_name])); Items[anchor_name] = node; } else { ItemsToRewind.Push(new RewindInfo(anchor_name, null)); Items.Add(anchor_name, node); } }
object NodeToObjectInternal(YamlNode node, Type expected, Dictionary<YamlNode, object> appeared) { if ( appeared.ContainsKey(node) ) return appeared[node]; object obj = null; // Type resolution Type type = expected == typeof(object) ? null : expected; Type fromTag = config.TagResolver.TypeFromTag(node.Tag); if ( fromTag == null ) fromTag = TypeFromTag(node.Tag); if ( fromTag != null && type != fromTag && fromTag.IsClass && fromTag != typeof(string) ) type = fromTag; if ( type == null ) type = fromTag; // try TagResolver if ( type == fromTag && fromTag != null ) if (node is YamlScalar && config.TagResolver.Decode((YamlScalar)node, out obj)) return obj; if (node.Tag == YamlNode.DefaultTagPrefix + "null") { obj = null; } else { if (node is YamlScalar) { obj = ScalarToObject((YamlScalar) node, type); } else if (node is YamlMapping) { obj = MappingToObject((YamlMapping) node, type, null, appeared); } else if (node is YamlSequence) { obj = SequenceToObject((YamlSequence) node, type, null, appeared); } else throw new NotImplementedException(); } if ( !appeared.ContainsKey(node) ) if(obj != null && obj.GetType().IsClass && ( !(obj is string) || ((string)obj).Length >= 1000 ) ) appeared.Add(node, obj); return obj; }
private void Process(string name, YamlNode node) { var mappingNode = node as YamlMappingNode; var scalarNode = node as YamlScalarNode; var sequenceNode = node as YamlSequenceNode; if (scalarNode != null) { long result; var isLong = long.TryParse(scalarNode.Value, out result); double doubleResult; var isDouble = double.TryParse(scalarNode.Value, out doubleResult); var type = "string"; if (isDouble) { type = "double"; } if (isLong) { type = "long"; } Console.WriteLine(name); if (name == "CarNumber") { type = "string"; } #line default #line hidden #line 79 "C:\Users\dean\projs\iRacingReplayOverlay.net\iRacingSDK.Net\GenerateDataModels\SessionInfoTemplate.tt" this.Write(" public "); #line default #line hidden #line 79 "C:\Users\dean\projs\iRacingReplayOverlay.net\iRacingSDK.Net\GenerateDataModels\SessionInfoTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(type)); #line default #line hidden #line 79 "C:\Users\dean\projs\iRacingReplayOverlay.net\iRacingSDK.Net\GenerateDataModels\SessionInfoTemplate.tt" this.Write(" "); #line default #line hidden #line 79 "C:\Users\dean\projs\iRacingReplayOverlay.net\iRacingSDK.Net\GenerateDataModels\SessionInfoTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(name)); #line default #line hidden #line 79 "C:\Users\dean\projs\iRacingReplayOverlay.net\iRacingSDK.Net\GenerateDataModels\SessionInfoTemplate.tt" this.Write(" { get; set; }\r\n"); #line default #line hidden #line 80 "C:\Users\dean\projs\iRacingReplayOverlay.net\iRacingSDK.Net\GenerateDataModels\SessionInfoTemplate.tt" } else if (sequenceNode != null) { #line default #line hidden #line 84 "C:\Users\dean\projs\iRacingReplayOverlay.net\iRacingSDK.Net\GenerateDataModels\SessionInfoTemplate.tt" this.Write(" public partial class _"); #line default #line hidden #line 85 "C:\Users\dean\projs\iRacingReplayOverlay.net\iRacingSDK.Net\GenerateDataModels\SessionInfoTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(name)); #line default #line hidden #line 85 "C:\Users\dean\projs\iRacingReplayOverlay.net\iRacingSDK.Net\GenerateDataModels\SessionInfoTemplate.tt" this.Write("\r\n {\r\n"); #line default #line hidden #line 87 "C:\Users\dean\projs\iRacingReplayOverlay.net\iRacingSDK.Net\GenerateDataModels\SessionInfoTemplate.tt" var nodes = (YamlMappingNode)sequenceNode.First(); foreach (var kv in nodes) { Process(kv.Key.ToString(), kv.Value); } #line default #line hidden #line 94 "C:\Users\dean\projs\iRacingReplayOverlay.net\iRacingSDK.Net\GenerateDataModels\SessionInfoTemplate.tt" this.Write(" }\r\n\r\n public _"); #line default #line hidden #line 96 "C:\Users\dean\projs\iRacingReplayOverlay.net\iRacingSDK.Net\GenerateDataModels\SessionInfoTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(name)); #line default #line hidden #line 96 "C:\Users\dean\projs\iRacingReplayOverlay.net\iRacingSDK.Net\GenerateDataModels\SessionInfoTemplate.tt" this.Write("[] "); #line default #line hidden #line 96 "C:\Users\dean\projs\iRacingReplayOverlay.net\iRacingSDK.Net\GenerateDataModels\SessionInfoTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(name)); #line default #line hidden #line 96 "C:\Users\dean\projs\iRacingReplayOverlay.net\iRacingSDK.Net\GenerateDataModels\SessionInfoTemplate.tt" this.Write(" { get; set; }\r\n"); #line default #line hidden #line 97 "C:\Users\dean\projs\iRacingReplayOverlay.net\iRacingSDK.Net\GenerateDataModels\SessionInfoTemplate.tt" } else if (mappingNode != null) { #line default #line hidden #line 100 "C:\Users\dean\projs\iRacingReplayOverlay.net\iRacingSDK.Net\GenerateDataModels\SessionInfoTemplate.tt" this.Write("\r\n public partial class _"); #line default #line hidden #line 102 "C:\Users\dean\projs\iRacingReplayOverlay.net\iRacingSDK.Net\GenerateDataModels\SessionInfoTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(name)); #line default #line hidden #line 102 "C:\Users\dean\projs\iRacingReplayOverlay.net\iRacingSDK.Net\GenerateDataModels\SessionInfoTemplate.tt" this.Write("\r\n {\r\n"); #line default #line hidden #line 104 "C:\Users\dean\projs\iRacingReplayOverlay.net\iRacingSDK.Net\GenerateDataModels\SessionInfoTemplate.tt" foreach (var kv in mappingNode) { Process(kv.Key.ToString(), kv.Value); } #line default #line hidden #line 106 "C:\Users\dean\projs\iRacingReplayOverlay.net\iRacingSDK.Net\GenerateDataModels\SessionInfoTemplate.tt" this.Write(" }\r\n\r\n public _"); #line default #line hidden #line 108 "C:\Users\dean\projs\iRacingReplayOverlay.net\iRacingSDK.Net\GenerateDataModels\SessionInfoTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(name)); #line default #line hidden #line 108 "C:\Users\dean\projs\iRacingReplayOverlay.net\iRacingSDK.Net\GenerateDataModels\SessionInfoTemplate.tt" this.Write(" "); #line default #line hidden #line 108 "C:\Users\dean\projs\iRacingReplayOverlay.net\iRacingSDK.Net\GenerateDataModels\SessionInfoTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(name)); #line default #line hidden #line 108 "C:\Users\dean\projs\iRacingReplayOverlay.net\iRacingSDK.Net\GenerateDataModels\SessionInfoTemplate.tt" this.Write(" { get; set; }\r\n"); #line default #line hidden #line 109 "C:\Users\dean\projs\iRacingReplayOverlay.net\iRacingSDK.Net\GenerateDataModels\SessionInfoTemplate.tt" } }
public string[] GetStringArray(YamlNode node) => GetStringArrayInternal(node) ?? new string[0];
private static Exception UnsupportedNodeType(YamlNode node, string path) => new FormatException($"Unsupported YAML node type '{node.GetType().Name} was found. Path '{path}', line {node.Start.Line} position {node.Start.Column}.");
public override object NodeToType(Type type, YamlNode node) { var val = int.Parse(node.ToString()); return(new GridId(val)); }
internal static object NodeToType(Type type, YamlNode node) { // special snowflake string if (type == typeof(String)) { return(node.ToString()); } // val primitives if (type.IsPrimitive) { return(StringToType(type, node.ToString())); } // val enum if (type.IsEnum) { return(Enum.Parse(type, node.ToString())); } // List<T> if (TryGenericListType(type, out var listType)) { var listNode = (YamlSequenceNode)node; var newList = (IList)Activator.CreateInstance(type); foreach (var entryNode in listNode) { var value = NodeToType(listType, entryNode); newList.Add(value); } return(newList); } // Dictionary<K,V> if (TryGenericDictType(type, out var keyType, out var valType)) { var dictNode = (YamlMappingNode)node; var newDict = (IDictionary)Activator.CreateInstance(type); foreach (var kvEntry in dictNode.Children) { var keyValue = NodeToType(keyType, kvEntry.Key); var valValue = NodeToType(valType, kvEntry.Value); newDict.Add(keyValue, valValue); } return(newDict); } // custom TypeSerializer if (_typeSerializers.TryGetValue(type, out var serializer)) { return(serializer.NodeToType(type, node)); } // other val (struct) if (type.IsValueType) { return(_structSerializer.NodeToType(type, (YamlMappingNode)node)); } // ref type that isn't a custom TypeSerializer throw new ArgumentException($"Type {type.FullName} is not supported.", nameof(type)); }
public virtual bool TryNodeToType(YamlNode node, Type type, out object obj) { obj = default; return(false); }
public IPropProvider Resolve(ComponentBuilderContext context, ElementBuilder element, PropTypeInfo prop, YamlNode node) => _resolvers.Select(r => r.Resolve(context, element, prop, node)).FirstOrDefault(p => p != null);
AddNode (YamlNode);
AddNode (YamlNode key, YamlNode value);
public RewindInfo(string anchor_name, YamlNode old_value) { this.anchor_name = anchor_name; this.old_value = old_value; }
public static IXPathNavigable ToNavigable(this YamlNode yaml, XName?rootName = null, string?baseUri = null) => new ExtensibleNavigator(yaml.AsNode(rootName, baseUri));
public bool Resolve(ComponentBuilderContext context, ElementBuilder element, string prop, YamlNode node) => _resolvers.Any(r => r.Resolve(context, element, prop, node));
public abstract object NodeToType(Type type, YamlNode node, YamlObjectSerializer serializer);
public override object NodeToType(Type type, YamlNode node, YamlObjectSerializer serializer) { var val = int.Parse(node.ToString(), CultureInfo.InvariantCulture); return(new MapId(val)); }
/// <summary> /// Called when this object is visiting a key-value pair. /// </summary> /// <param name="key">The left (key) <see cref="YamlNode"/> that is being visited.</param> /// <param name="value">The right (value) <see cref="YamlNode"/> that is being visited.</param> protected virtual void VisitPair(YamlNode key, YamlNode value) { key.Accept(this); value.Accept(this); }
public override object NodeToType(Type type, YamlNode node, YamlObjectSerializer serializer) { return(new GridId(node.AsInt())); }
/// <summary> /// Selects the child node according to this path part. /// Returns null if such node was not found. /// </summary> public abstract YamlNode SelectNode(ref YamlNode node);
public override object NodeToType(Type type, YamlNode node, YamlObjectSerializer serializer) { return(node.AsResourcePath()); }
public abstract object NodeToType(Type type, YamlNode node);
public override object NodeToType(Type type, YamlNode node, YamlObjectSerializer serializer) { return(SpriteSpecifier.FromYaml(node)); }
public override object NodeToType(Type type, YamlNode node) { var val = float.Parse(node.ToString()); return(new Angle(val)); }
public object NodeToType(Type type, YamlNode node) { // special snowflake string if (type == typeof(String)) { return(node.ToString()); } // val primitives if (type.IsPrimitive || type == typeof(decimal)) { return(StringToType(type, node.ToString())); } // array if (type.IsArray) { var listNode = (YamlSequenceNode)node; var newArray = (Array)Activator.CreateInstance(type, listNode.Children.Count) !; var idx = 0; foreach (var entryNode in listNode) { var value = NodeToType(type.GetElementType() !, entryNode); newArray.SetValue(value, idx++); } return(newArray); } // val enum if (type.IsEnum) { return(Enum.Parse(type, node.ToString())); } // IReadOnlyList<T>/IReadOnlyCollection<T> if (TryGenericReadOnlyCollectionType(type, out var collectionType)) { var listNode = (YamlSequenceNode)node; var elems = listNode.Children; // Deserialize to an array because that is much more efficient, and allowed. var newList = (IList)Array.CreateInstance(collectionType, elems.Count); for (var i = 0; i < elems.Count; i++) { newList[i] = NodeToType(collectionType, elems[i]); } return(newList); } // List<T> if (TryGenericListType(type, out var listType)) { var listNode = (YamlSequenceNode)node; var newList = (IList)Activator.CreateInstance(type, listNode.Children.Count) !; foreach (var entryNode in listNode) { var value = NodeToType(listType, entryNode); newList.Add(value); } return(newList); } // Dictionary<K,V>/IReadOnlyDictionary<K,V> if (TryGenericReadDictType(type, out var keyType, out var valType, out var dictType)) { var dictNode = (YamlMappingNode)node; var newDict = (IDictionary)Activator.CreateInstance(dictType, dictNode.Children.Count) !; foreach (var kvEntry in dictNode.Children) { var keyValue = NodeToType(keyType, kvEntry.Key); var valValue = NodeToType(valType, kvEntry.Value); newDict.Add(keyValue, valValue); } return(newDict); } // Hand it to the context. if (_context != null && _context.TryNodeToType(node, type, out var contextObj)) { return(contextObj); } // custom TypeSerializer if (_typeSerializers.TryGetValue(type, out var serializer)) { return(serializer.NodeToType(type, node, this)); } // IExposeData. if (typeof(IExposeData).IsAssignableFrom(type)) { if (!(node is YamlMappingNode mapNode)) { throw new InvalidOperationException("Cannot read from IExposeData on non-mapping node."); } var concreteType = type; if (type.IsAbstract || type.IsInterface) { var tag = node.Tag; if (string.IsNullOrWhiteSpace(tag)) { throw new YamlException($"Type '{type}' is abstract, but there is no yaml tag for the concrete type."); } var args = tag.Split(':'); if (args.Length == 2 && args[0] == "!type") { concreteType = ResolveConcreteType(type, args[1]); } else { throw new YamlException("Malformed type tag."); } } var instance = (IExposeData)Activator.CreateInstance(concreteType) !; // TODO: Might be worth it to cut down on allocations here by using ourselves instead of creating a fork. // Seems doable. if (_context != null) { _context.StackDepth++; } var fork = NewReader(mapNode, _context); if (_context != null) { _context.StackDepth--; } instance.ExposeData(fork); return(instance); } // ISelfSerialize if (typeof(ISelfSerialize).IsAssignableFrom(type)) { var instance = (ISelfSerialize)Activator.CreateInstance(type) !; instance.Deserialize(node.ToString()); return(instance); } // other val (struct) if (type.IsValueType) { return(_structSerializer.NodeToType(type, (YamlMappingNode)node, this)); } // ref type that isn't a custom TypeSerializer throw new ArgumentException($"Type {type.FullName} is not supported.", nameof(type)); }
private static Exception UnsupportedMergeKeys(YamlNode node, YamlNode parent, string path) => new FormatException($"Unsupported YAML merge keys '{node.GetType().Name} was found. Path '{path}', line {parent.Start.Line} position {parent.Start.Column}.");
public virtual bool TryNodeToType(YamlNode node, Type type, [NotNullWhen(true)] out object?obj) { obj = default; return(false); }
public Dictionary <string, string> GetStringDictionary(YamlNode node) => GetStringDictionaryInternal(node) ?? new Dictionary <string, string>();
protected override Dictionary <string, Action> GetActions(StartupModuleParameters target, YamlNode value, YamlParser parser) { return(new Dictionary <string, Action>()); }
public override void Parse(string traceContext, YamlNode node, HashSet <string> ignoredKeys) { var rootNode = DockerComposeUtils.ValidateAndGetMapping(traceContext, "Root", node); foreach (var childItem in rootNode.Children) { var key = childItem.Key.ToString(); switch (key) { case DockerComposeConstants.VersionKey: { this.DockerComposeVersion = childItem.Value.ToString(); if (!this.DockerComposeVersion .StartsWith(DockerComposeConstants.SupportedDockerComposeVersion3) && !this.DockerComposeVersion .StartsWith(DockerComposeConstants.SupportedDockerComposeVersion2)) { throw new FabricComposeException( string.Format( "Docker compose file version not supported. Supported version - '3' input file version - '{0}'", childItem.Value.ToString())); } break; } case DockerComposeConstants.ServicesKey: { this.ParseServices( DockerComposeUtils.GenerateTraceContext(traceContext, DockerComposeConstants.ServicesKey), DockerComposeUtils.ValidateAndGetMapping(traceContext, DockerComposeConstants.ServicesKey, childItem.Value), ignoredKeys); break; } case DockerComposeConstants.VolumesKey: { this.ParseVolumes( DockerComposeUtils.GenerateTraceContext(traceContext, DockerComposeConstants.VolumesKey), DockerComposeUtils.ValidateAndGetMapping(traceContext, DockerComposeConstants.VolumesKey, childItem.Value), ignoredKeys); break; } case DockerComposeConstants.LabelsKey: { this.ParseLabels( DockerComposeUtils.GenerateTraceContext(traceContext, DockerComposeConstants.LabelsKey), DockerComposeUtils.ValidateAndGetMapping(traceContext, DockerComposeConstants.LabelsKey, childItem.Value)); break; } case DockerComposeConstants.NetworksKey: { this.ParseNetworks( DockerComposeUtils.GenerateTraceContext(traceContext, DockerComposeConstants.NetworksKey), DockerComposeUtils.ValidateAndGetMapping(traceContext, DockerComposeConstants.NetworksKey, childItem.Value), ignoredKeys); break; } default: { ignoredKeys.Add(key); break; } } } }
public object NodeToType(Type type, YamlNode node) { // special snowflake string if (type == typeof(String)) { return(node.ToString()); } // val primitives if (type.IsPrimitive) { return(StringToType(type, node.ToString())); } // val enum if (type.IsEnum) { return(Enum.Parse(type, node.ToString())); } // List<T> if (TryGenericListType(type, out var listType)) { var listNode = (YamlSequenceNode)node; var newList = (IList)Activator.CreateInstance(type); foreach (var entryNode in listNode) { var value = NodeToType(listType, entryNode); newList.Add(value); } return(newList); } // Dictionary<K,V> if (TryGenericDictType(type, out var keyType, out var valType)) { var dictNode = (YamlMappingNode)node; var newDict = (IDictionary)Activator.CreateInstance(type); foreach (var kvEntry in dictNode.Children) { var keyValue = NodeToType(keyType, kvEntry.Key); var valValue = NodeToType(valType, kvEntry.Value); newDict.Add(keyValue, valValue); } return(newDict); } // Hand it to the context. if (_context != null && _context.TryNodeToType(node, type, out var contextObj)) { return(contextObj); } // custom TypeSerializer if (_typeSerializers.TryGetValue(type, out var serializer)) { return(serializer.NodeToType(type, node, this)); } // IExposeData. if (typeof(IExposeData).IsAssignableFrom(type)) { if (!(node is YamlMappingNode mapNode)) { throw new InvalidOperationException("Cannot read from IExposeData on non-mapping node."); } var instance = (IExposeData)Activator.CreateInstance(type); // TODO: Might be worth it to cut down on allocations here by using ourselves instead of creating a fork. // Seems doable. if (_context != null) { _context.StackDepth++; } var fork = NewReader(mapNode, _context); if (_context != null) { _context.StackDepth--; } instance.ExposeData(fork); return(instance); } // other val (struct) if (type.IsValueType) { return(_structSerializer.NodeToType(type, (YamlMappingNode)node, this)); } // ref type that isn't a custom TypeSerializer throw new ArgumentException($"Type {type.FullName} is not supported.", nameof(type)); }
protected override Dictionary <string, Action> GetActions(VCppProjectCLIParameters target, YamlNode value, YamlParser parser) { throw new NotImplementedException(); }
public virtual bool TryTypeToNode(object obj, out YamlNode node) { node = null; return(false); }
object NodeToObjectInternal(YamlNode node, Type expected, Dictionary <YamlNode, object> appeared) { if (appeared.ContainsKey(node)) { return(appeared[node]); } object obj = null; // Type resolution Type type = expected == typeof(object) ? null : expected; Type fromTag = TagResolver.TypeFromTag(node.Tag); if (fromTag == null) { fromTag = TypeFromTag(node.Tag); } if (fromTag != null && type != fromTag && fromTag.IsClass && fromTag != typeof(string)) { type = fromTag; } if (type == null) { type = fromTag; } // try TagResolver if (type == fromTag && fromTag != null) { if (node is YamlScalar && TagResolver.Decode((YamlScalar)node, out obj)) { return(obj); } } if (node.Tag == YamlNode.DefaultTagPrefix + "null") { obj = null; } else if (node is YamlScalar) { obj = ScalarToObject((YamlScalar)node, type); } else if (node is YamlMapping) { obj = MappingToObject((YamlMapping)node, type, null, appeared); } else if (node is YamlSequence) { obj = SequenceToObject((YamlSequence)node, type, appeared); } else { throw new NotImplementedException(); } if (!appeared.ContainsKey(node)) { if (obj != null && obj.GetType().IsClass&& (!(obj is string) || ((string)obj).Length >= 1000)) { appeared.Add(node, obj); } } return(obj); }
public override object NodeToType(Type type, YamlNode node, YamlObjectSerializer serializer) { return(node.AsColor()); }
/// <summary> /// Construct YAML node tree that represents a given C# object. /// </summary> /// <param name="node"><see cref="YamlNode"/> to be converted to C# object.</param> /// <param name="config"><see cref="YamlConfig"/> to customize serialization.</param> /// <returns></returns> public object NodeToObject(YamlNode node, YamlConfig config) { return(NodeToObject(node, null, config)); }
private void AppendToAppeared(object obj, YamlNode node) { if ( obj != null && obj.GetType().IsClass && ( !( obj is string ) || ( (string)obj ).Length >= 1000 ) ) if ( !appeared.ContainsKey(obj) ) appeared.Add(obj, node); }
/// <summary> /// Construct YAML node tree that represents a given C# object. /// </summary> /// <param name="node"><see cref="YamlNode" /> to be converted to C# object.</param> /// <param name="context">The context.</param> /// <returns>System.Object.</returns> public object NodeToObject(YamlNode node, SerializerContext context) { return NodeToObject(node, null, context); }