Im trying to convert my List code lines to Dictionary since as i understand they are faster, and more easy to manage, like i can get its value with its key value rather than just index.

Thus, im trying to modify my json-deserializing code (which actually,Newtonsoft's), but i have no idea how can i convert raw json to dictionary with my own format.

Dictionary <string,Tag>

Tag class is my own class with member variables, contains tag informations. it doesnt includes functions for sure.

String should be Tag.Name, a string member variable.

            //TODO: Fix "Hacky" solution to deserialized lines into Dictionary
            //Which means : Raw Json -> Deserialized data -> List -> Dictionary is very hacky in my perspective
            //Should be : Raw Json -> Deserialized data -> Dictionary, but i have no idea to convert directly like that

            else if (filename.Contains(".json")) // very janky method to detect json file. dont depend on file extension - rather depend on actual file format.
            {
                string line = String.Join("", Lines);

                List<Tag> lv_list = new List<Tag>();
                Dictionary<string,Tag> lv_dict = new Dictionary<string, Tag>();

                lv_list = JsonConvert.DeserializeObject<List<Tag>>(line);

                gVar.Tags.AddRange(lv_list);
                gVar.gTagCount += lv_list.Count();

                for (int i = 0; i < lv_list.Count(); ++i)
                {
                    gVar.Tags2.Add(lv_list[i].Name, lv_list[i]);
                }

                int lv_tagcount = 1;
                for (int i = 0; i < gVar.gTagCount; ++i)
                {
                    gVar.Tags[i].TagCount = lv_tagcount;
                    ++lv_tagcount;
                }

1 Answers

1
StriplingWarrior On Best Solutions

I wouldn't bother deserializing directly to a Dictionary: if the JSON is formatted like an array, go ahead and deserialize it to a List first. There's nothing "hacky" about the two-step process.

Use LINQ's ToDictionary() method to get a dictionary from a list:

Dictionary<string,Tag> lv_dict = JsonConvert.DeserializeObject<List<Tag>>(line)
    .ToDictionary(tag => tag.Name);

You seem to have a habit of initializing a variable with an empty collection that never gets used, and then setting the variable to some other value. This isn't helpful: just declare and set the variable at the same time. But based on this pattern I'm wondering if gVar is an object you're trying to initialize even though your code looks like it's trying to add things to that variable. Do you really want to do something more like this?

gVar.Tags2 = JsonConvert.DeserializeObject<List<Tag>>(line)
    .ToDictionary(tag => tag.Name);

I'd also question whether the TagCount property is a good pattern. It looks like you're trying to make each Tag explicitly aware of its own position in the collection that it's found in. This is a code smell. Perhaps now that you're using a Dictionary that won't be necessary because you can look up the tag by its name in the Dictionary?