From f074d944017864b662b92ac8e6ce4aec9dd81115 Mon Sep 17 00:00:00 2001 From: Hunter Goodin Date: Fri, 16 Sep 2022 20:25:08 -0700 Subject: [PATCH] v1.1 Update Changed the following: - Fixed typos in errors - Added remove functions - Made it so you can't name multiple variables of the same type the same name - Returns an error if you try --- JSONSerializerPackage/Assets/Code/JSON.cs | 119 +++++++++++++++++++--- 1 file changed, 107 insertions(+), 12 deletions(-) diff --git a/JSONSerializerPackage/Assets/Code/JSON.cs b/JSONSerializerPackage/Assets/Code/JSON.cs index 95e6bdc..de7b9c0 100644 --- a/JSONSerializerPackage/Assets/Code/JSON.cs +++ b/JSONSerializerPackage/Assets/Code/JSON.cs @@ -174,7 +174,7 @@ public bool GetBool(string variableName) } else { - Debug.LogError($"ERROR: No variable names {variableName} found! returning 'false' instead"); + Debug.LogError($"ERROR: No variable named {variableName} found! returning 'false' instead"); return false; } } @@ -199,7 +199,7 @@ public int GetInt(string variableName) } else { - Debug.LogError($"ERROR: No variable names {variableName} found! returning '0' instead"); + Debug.LogError($"ERROR: No variable named {variableName} found! returning '0' instead"); return 0; } } @@ -224,7 +224,7 @@ public float GetFloat(string variableName) } else { - Debug.LogError($"ERROR: No variable names '{variableName}' found! returning '0.0f' instead"); + Debug.LogError($"ERROR: No variable named '{variableName}' found! returning '0.0f' instead"); return 0.0f; } } @@ -249,7 +249,7 @@ public string GetString(string variableName) } else { - Debug.LogError($"ERROR: No variable names '{variableName}' found! returning NULL instead"); + Debug.LogError($"ERROR: No variable named '{variableName}' found! returning NULL instead"); return null; } } @@ -273,7 +273,7 @@ public void SetBool(string variableName, bool newBool) if (!found) { - Debug.LogError($"ERROR: No variable names {variableName} found!"); + Debug.LogError($"ERROR: No variable named {variableName} found!"); } } @@ -292,7 +292,7 @@ public void SetInt(string variableName, int newInt) if (!found) { - Debug.LogError($"ERROR: No variable names {variableName} found!"); + Debug.LogError($"ERROR: No variable named {variableName} found!"); } } @@ -311,7 +311,7 @@ public void SetFloat(string variableName, float newFloat) if (!found) { - Debug.LogError($"ERROR: No variable names {variableName} found!"); + Debug.LogError($"ERROR: No variable named {variableName} found!"); } } @@ -330,7 +330,7 @@ public void SetString(string variableName, string newString) if (!found) { - Debug.LogError($"ERROR: No variable names {variableName} found!"); + Debug.LogError($"ERROR: No variable named {variableName} found!"); } } @@ -340,31 +340,126 @@ public void SetString(string variableName, string newString) public void AddBool(string newItemName, bool newItemValue) { + for (int i = 0; i < boolList.Count; i++) + { + if (boolList[i].name == newItemName) + { + Debug.LogError($"ERROR: There is already a boolean named {newItemValue} in {jsonFile.name}.json!"); + return; + } + } + boolList.Add(new JSONBoolean(newItemName, newItemValue)); } public void AddInt(string newItemName, int newItemValue) { + for (int i = 0; i < intList.Count; i++) + { + if (intList[i].name == newItemName) + { + Debug.LogError($"ERROR: There is already a integer named {newItemValue} in {jsonFile.name}.json!"); + return; + } + } + intList.Add(new JSONInteger(newItemName, newItemValue)); } public void AddFloat(string newItemName, float newItemValue) { + for (int i = 0; i < floatList.Count; i++) + { + if (floatList[i].name == newItemName) + { + Debug.LogError($"ERROR: There is already a float named {newItemValue} in {jsonFile.name}.json!"); + return; + } + } + floatList.Add(new JSONFloat(newItemName, newItemValue)); } public void AddString(string newItemName, string newItemValue) { + for (int i = 0; i < stringList.Count; i++) + { + if (stringList[i].name == newItemName) + { + Debug.LogError($"ERROR: There is already a string named {newItemValue} in {jsonFile.name}.json!"); + return; + } + } + stringList.Add(new JSONString(newItemName, newItemValue)); } #endregion Add Functions + + #region Remove Functions + + public void RemoveBool(string variableName) + { + for (int i = 0; i < boolList.Count; i++) + { + if (boolList[i].name == variableName) + { + boolList.Remove(boolList[i]); + return; + } + } + + Debug.LogError($"ERROR: No variable named {variableName} found!"); + } + public void RemoveInt(string variableName) + { + for (int i = 0; i < intList.Count; i++) + { + if (intList[i].name == variableName) + { + intList.Remove(intList[i]); + return; + } + } + + Debug.LogError($"ERROR: No variable named {variableName} found!"); + } + + public void RemoveFloat(string variableName) + { + for (int i = 0; i < floatList.Count; i++) + { + if (floatList[i].name == variableName) + { + floatList.Remove(floatList[i]); + return; + } + } + + Debug.LogError($"ERROR: No variable named {variableName} found!"); + } + + public void RemoveString(string variableName) + { + for (int i = 0; i < stringList.Count; i++) + { + if (stringList[i].name == variableName) + { + stringList.Remove(stringList[i]); + return; + } + } + + Debug.LogError($"ERROR: No variable named {variableName} found!"); + } + + #endregion Remove Functions } #region JSON Variables - [System.Serializable] + [Serializable] public class JSONBoolean { public string name; @@ -377,7 +472,7 @@ public JSONBoolean(string name, bool value) } } - [System.Serializable] + [Serializable] public class JSONInteger { public string name; @@ -390,7 +485,7 @@ public JSONInteger(string name, int value) } } - [System.Serializable] + [Serializable] public class JSONFloat { public string name; @@ -403,7 +498,7 @@ public JSONFloat(string name, float value) } } - [System.Serializable] + [Serializable] public class JSONString { public string name;