Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate key #407

Closed
nukadelic opened this issue May 20, 2019 · 3 comments
Closed

Duplicate key #407

nukadelic opened this issue May 20, 2019 · 3 comments
Labels

Comments

@nukadelic
Copy link

Using a dropdown UI i try and load a yaml file each time the value is changing, running in Unity 2019 via simple system IO:

            var fileStream = new FileStream( file.FullName, FileMode.Open, FileAccess.Read);
            using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
            {
                        var deserializer = new DeserializerBuilder()
                                .WithNamingConvention( new CamelCaseNamingConvention() )
                                .Build();
                            var yaml = new YamlStream();
                           yaml.Load( streamReader );
... 

Here is the error i am getting :

ArgumentException: An item with the same key has already been added. Key: sequence_length
System.Collections.Generic.Dictionary`2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior)
System.Collections.Generic.Dictionary`2[TKey,TValue].Add (TKey key, TValue value)
YamlDotNet.RepresentationModel.YamlMappingNode.Load (YamlDotNet.Core.IParser parser, YamlDotNet.RepresentationModel.DocumentLoadingState state)
Rethrow as YamlException: (Line: 26, Col: 5, Idx: 590) - (Line: 26, Col: 20, Idx: 605): Duplicate key
YamlDotNet.RepresentationModel.YamlMappingNode.Load (YamlDotNet.Core.IParser parser, YamlDotNet.RepresentationModel.DocumentLoadingState state)
YamlDotNet.RepresentationModel.YamlMappingNode..ctor (YamlDotNet.Core.IParser parser, YamlDotNet.RepresentationModel.DocumentLoadingState state)
YamlDotNet.RepresentationModel.YamlNode.ParseNode (YamlDotNet.Core.IParser parser, YamlDotNet.RepresentationModel.DocumentLoadingState state)
YamlDotNet.RepresentationModel.YamlMappingNode.Load (YamlDotNet.Core.IParser parser, YamlDotNet.RepresentationModel.DocumentLoadingState state)
YamlDotNet.RepresentationModel.YamlMappingNode..ctor (YamlDotNet.Core.IParser parser, YamlDotNet.RepresentationModel.DocumentLoadingState state)
YamlDotNet.RepresentationModel.YamlNode.ParseNode (YamlDotNet.Core.IParser parser, YamlDotNet.RepresentationModel.DocumentLoadingState state)
YamlDotNet.RepresentationModel.YamlDocument..ctor (YamlDotNet.Core.IParser parser)
YamlDotNet.RepresentationModel.YamlStream.Load (YamlDotNet.Core.IParser parser)
YamlDotNet.RepresentationModel.YamlStream.Load (System.IO.TextReader input)
@aaubry
Copy link
Owner

aaubry commented May 20, 2019

As indicated in the exception message, there must be a duplicate key in your YAML file, on line 26, column 5. If you share your file, I may be able to provide more help.

@nukadelic
Copy link
Author

Thanks for the response, found an alternative way to solve my problem.
I think It was an error on the load level, looks like it wasn't able to handle repeated loading, it could be that my code wasn't disposing the stream properly, but didn't end up finding how to do that on the YamlSteam class. And since the file that needed to be parsed does have a very basic structure, I ended up using the Deserializer only, like so:

var deserializer = new DeserializerBuilder()
    .WithNamingConvention( new CamelCaseNamingConvention() )
    .Build();

Dictionary<string, object> raw = deserializer.Deserialize<Dictionary<string, object>>( streamReader );

string[] categories = raw.Keys.ToArray();

var data = raw.Values.Select( config => ( Dictionary<object, object> ) config ).ToArray();

List<SortedDictionary<string, string>> output = new List<SortedDictionary<string, string>>();

foreach( var d in data )
{
    SortedDictionary<string, string> dict = new SortedDictionary<string, string>();

    foreach( var k in d.Keys ) dict.Add( k.ToString(), d[ k ].ToString() );

    output.Add( dict );
}

return output;

If you want i can try and replicate the problem, its just that I'm using your code in Unity 2019 (.NET 4.x)

@aaubry
Copy link
Owner

aaubry commented May 21, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants