C# Library for the Fjord Config Format
at master 6.2 kB view raw
1namespace libfcf; 2 3public static class Serializer { 4 public static string SerializeObjectToJson(Dictionary<string, object> obj) 5 { 6 string output = ""; 7 8 int i = -1; 9 if (obj.GetType() == typeof(Dictionary<string, object>)) 10 { 11 output += "{"; 12 foreach (KeyValuePair<string, object> kvp in obj) 13 { 14 i++; 15 if (kvp.Value.GetType() == typeof(List<object>) || kvp.Value.GetType() == typeof(Dictionary<string, object>)) 16 { 17 output += $"\"{kvp.Key}\": {SerializeObjectToJson((Dictionary<string, object>)kvp.Value)}"; 18 } 19 else 20 { 21 if (kvp.Value.GetType() == typeof(string)) 22 { 23 output += $"\"{kvp.Key}\": \"{kvp.Value}\""; 24 } 25 else if (kvp.Value.GetType() == typeof(bool)) 26 { 27 var boolval = ((bool)kvp.Value).ToString().ToLower(); 28 output += $"\"{kvp.Key}\": {boolval}"; 29 } 30 else 31 { 32 output += $"\"{kvp.Key}\": {kvp.Value}"; 33 } 34 } 35 if (i != obj.Count - 1) 36 { 37 output += ", "; 38 } 39 } 40 output += "}"; 41 } 42 else if (obj.GetType() == typeof(List<object>)) 43 { 44 output += "["; 45 foreach (object val in obj) 46 { 47 i++; 48 if (val.GetType() == typeof(List<object>)) 49 { 50 output += $"{SerializeObjectToJson((List<object>)val)}"; 51 } else if(val.GetType() == typeof(Dictionary<string, object>)) 52 { 53 output += $"{SerializeObjectToJson((Dictionary<string, object>)val)}"; 54 } 55 else 56 { 57 if (val.GetType() == typeof(string)) 58 { 59 output += $"\"{val}\""; 60 } 61 else if (val.GetType() == typeof(bool)) 62 { 63 output += ((bool)val).ToString().ToLower(); 64 } 65 else 66 { 67 output += val; 68 } 69 } 70 if (i != obj.Count - 1) 71 { 72 output += ", "; 73 } 74 } 75 output += "]"; 76 } 77 78 return output; 79 } 80 81 public static string SerializeObjectToJson(List<object> obj) 82 { 83 string output = ""; 84 85 int i = -1; 86 87 output += "["; 88 foreach (object val in obj) 89 { 90 i++; 91 if (val.GetType() == typeof(List<object>)) 92 { 93 output += $"{SerializeObjectToJson((List<object>)val)}"; 94 } 95 else if(val.GetType() == typeof(Dictionary<string, object>)) 96 { 97 output += $"{SerializeObjectToJson((Dictionary<string, object>)val)}"; 98 } 99 else 100 { 101 if (val.GetType() == typeof(string)) 102 { 103 output += $"\"{val}\""; 104 } 105 else if (val.GetType() == typeof(bool)) 106 { 107 output += ((bool)val).ToString().ToLower(); 108 } 109 else 110 { 111 output += val; 112 } 113 } 114 if (i != obj.Count - 1) 115 { 116 output += ", "; 117 } 118 } 119 output += "]"; 120 121 return output; 122 } 123 124 public static string SerializeObject(Dictionary<string, object> obj) 125 { 126 string output = ""; 127 128 int i = -1; 129 130 output += "{"; 131 foreach (KeyValuePair<string, object> kvp in obj) 132 { 133 i++; 134 if (kvp.Value.GetType() == typeof(List<object>)) 135 { 136 output += $"{kvp.Key} = {SerializeObject((List<object>)kvp.Value)}"; 137 } 138 else if(kvp.Value.GetType() == typeof(Dictionary<string, object>)) 139 { 140 output += $"{kvp.Key} = {SerializeObject((Dictionary<string, object>)kvp.Value)}"; 141 } 142 else 143 { 144 if (kvp.Value.GetType() == typeof(string)) 145 { 146 output += $"{kvp.Key} = \"{kvp.Value}\""; 147 } 148 else if (kvp.Value.GetType() == typeof(bool)) 149 { 150 var boolval = ((bool)kvp.Value).ToString().ToLower(); 151 output += $"{kvp.Key} = {boolval}"; 152 } 153 else 154 { 155 output += $"{kvp.Key} = {kvp.Value}"; 156 } 157 } 158 if (i != obj.Count - 1) 159 { 160 output += ", "; 161 } 162 } 163 output += "}"; 164 165 return output; 166 } 167 168 public static string SerializeObject(List<object> obj) 169 { 170 string output = ""; 171 172 int i = -1; 173 174 output += "["; 175 foreach (object val in obj) 176 { 177 i++; 178 if (val.GetType() == typeof(List<object>)) 179 { 180 output += $"{SerializeObject((List<object>)val)}"; 181 } else if(val.GetType() == typeof(Dictionary<string, object>)) 182 { 183 output += $"{SerializeObject((Dictionary<string, object>)val)}"; 184 } 185 else 186 { 187 if (val.GetType() == typeof(string)) 188 { 189 output += $"\"{val}\""; 190 } 191 else if (val.GetType() == typeof(bool)) 192 { 193 output += ((bool)val).ToString().ToLower(); 194 } 195 else 196 { 197 output += val; 198 } 199 } 200 if (i != obj.Count - 1) 201 { 202 output += ", "; 203 } 204 } 205 output += "]"; 206 207 return output; 208 } 209}