Json.Net revert complex dict

59 Views Asked by At

I'm working with a big Json file (with Json.NET). I converted my JObject to a Dictionnary. Now i'm trying to revert it to be able to get keys by values. The problem is that the Json file is complex and i don't know how to deal with it. Here is a sample of the json file :

{
"MASTER_MODE": {
    "SET_VTSA": {
        "id": 10,
        "position": 24,
        "elements": {
            "SET_CAV_Vol": {
                "id": 0,
                "formatted_lenght": 2,
                "format_divider": 1,
                "is_included_in_integrity_checksum": true,
                "is_signed": false
            },
            "SET_ALARM_VOLTAGE_MAX": {
                "id": 29,
                "formatted_lenght": 1,
                "format_divider": 1,
                "is_included_in_integrity_checksum": true,
                "is_signed": false
            }
        }
    }
},
"MASTER_BASE": {
    "SET_MAIN": {
        "id": 11,
        "elements": {
            "SET_Speed": {
                "id": 0,
                "display_unit": "UNIT_RPM",
                "position": 101,
                "is_included_in_integrity_checksum": true,
                "is_signed": false
            },
            "Master_Test": {
                "id": 3,
                "position": 104,
                "is_included_in_integrity_checksum": true,
                "is_signed": false
            }
        }
    },
    "GLOBAL": {
        "id": 14,
        "elements": {
            "SET_Is_Alarm": {
                "id": 0,
                "type": "bool",
                "is_included_in_integrity_checksum": true,
                "is_signed": false
            },
            "SET_Is_Sync": {
                "id": 1,
                "type": "bool",
                "available_in_mode": [
                    "SET_Vone",
                    "SET_VdO"
                ],
                "is_included_in_integrity_checksum": true,
                "is_signed": false
            }
        }
    },

"MASTER_COMMAND": {
    "CMD_GET_COMM_API": {
        "id": 1
    },
    "CMD_START": {
        "id": 2
    }
},
"MASTER_CONSTANTS": {
    "NB_OF_RX_FRAME_BYTES_MIN": 2,
    "NB_OF_RX_FRAME_BYTES_MAX": 140,
    "FIRST_ANSWER_ID": 120,
    "LAST_ANSWER_ID": 135,
    "FIRST_EVENT_ID": 195,
    "LAST_EVENT_ID": 205,
    "NB_OF_SYSTEM_EVENT_LOG_BYTES": 64,
    "MAX_SYSTEM_EVENT_LOG_INDEX": 4598
}

}

I already did it with Python :

def revert_dictionnary(self, normal_dictionnary):
    reverted_dictonnary = {}
    try:
        for category in normal_dictionnary.items():
            reverted_dictonnary[category[0]] = {}
            for element in category[1].items():
                if category[0] != "DRV_EVENT_SYSTEM" :
                    reverted_dictonnary[category[0]][
                        element[1]["id"]] = {"label": element[0]}
                    if category[0] in ("DRV_SLAVE"):
                        reverted_dictonnary[category[0]][
                            element[1]["id"]]["elements"] = {}
                        for base_element in element[1]["elements"].items():
                            reverted_dictonnary[category[0]][element[1]["id"]]["elements"][ base_element[1]["id"]] = {"label": base_element[0]}

But i guess i'm missing something into Json.NET or the .Net framework.

How would you do it in C# ?

i tried 2 way :

        public static TKey FindKeyByValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TValue value)
    {
        if (dictionary == null)
            throw new ArgumentNullException("dictionary");

        foreach (KeyValuePair<TKey, TValue> pair in dictionary)
            if (value.Equals(pair.Value)) return pair.Key;

        throw new Exception("the value is not found in the dictionary");
    }


    public static IDictionary<string, object> RevertDictionary<TKey, TValue>(this IDictionary<TKey, TValue> dict)
    {
        IDictionary<TKey, TValue> newDict = new Dictionary<TKey, TValue>();

        IDictionary<TKey, TValue> emptyDict = new Dictionary<TKey, TValue>();

        foreach (KeyValuePair<TKey, TValue> pair in dict)
        {
            newDict.Add(pair.Key, (TValue)emptyDict);

           // foreach (KeyValuePair<TKey, TValue> elements in pair.Value)
           // {

            //}

        }
        return new Dictionary<string, object>();
    }

the first return alway null, the second is invalid because i can't iterate over a KeyValueMap

0

There are 0 best solutions below