|
| 1 | +using Newtonsoft.Json; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | + |
| 8 | +namespace Xamasoft.JsonClassGenerator.CodeWriters |
| 9 | +{ |
| 10 | + public class CSharpCodeWriter : ICodeWriter |
| 11 | + { |
| 12 | + public string FileExtension |
| 13 | + { |
| 14 | + get { return ".cs"; } |
| 15 | + } |
| 16 | + |
| 17 | + public string DisplayName |
| 18 | + { |
| 19 | + get { return "C#"; } |
| 20 | + } |
| 21 | + |
| 22 | + |
| 23 | + private const string NoRenameAttribute = "[Obfuscation(Feature = \"renaming\", Exclude = true)]"; |
| 24 | + private const string NoPruneAttribute = "[Obfuscation(Feature = \"trigger\", Exclude = false)]"; |
| 25 | + |
| 26 | + public string GetTypeName(JsonType type, IJsonClassGeneratorConfig config) |
| 27 | + { |
| 28 | + var arraysAsLists = !config.ExplicitDeserialization; |
| 29 | + |
| 30 | + switch (type.Type) |
| 31 | + { |
| 32 | + case JsonTypeEnum.Anything: return "object"; |
| 33 | + case JsonTypeEnum.Array: return arraysAsLists ? "IList<" + GetTypeName(type.InternalType, config) + ">" : GetTypeName(type.InternalType, config) + "[]"; |
| 34 | + case JsonTypeEnum.Dictionary: return "Dictionary<string, " + GetTypeName(type.InternalType, config) + ">"; |
| 35 | + case JsonTypeEnum.Boolean: return "bool"; |
| 36 | + case JsonTypeEnum.Float: return "double"; |
| 37 | + case JsonTypeEnum.Integer: return "int"; |
| 38 | + case JsonTypeEnum.Long: return "long"; |
| 39 | + case JsonTypeEnum.Date: return "DateTime"; |
| 40 | + case JsonTypeEnum.NonConstrained: return "object"; |
| 41 | + case JsonTypeEnum.NullableBoolean: return "bool?"; |
| 42 | + case JsonTypeEnum.NullableFloat: return "double?"; |
| 43 | + case JsonTypeEnum.NullableInteger: return "int?"; |
| 44 | + case JsonTypeEnum.NullableLong: return "long?"; |
| 45 | + case JsonTypeEnum.NullableDate: return "DateTime?"; |
| 46 | + case JsonTypeEnum.NullableSomething: return "object"; |
| 47 | + case JsonTypeEnum.Object: return type.AssignedName; |
| 48 | + case JsonTypeEnum.String: return "string"; |
| 49 | + default: throw new System.NotSupportedException("Unsupported json type"); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + private bool ShouldApplyNoRenamingAttribute(IJsonClassGeneratorConfig config) |
| 55 | + { |
| 56 | + return config.ApplyObfuscationAttributes && !config.ExplicitDeserialization && !config.UsePascalCase; |
| 57 | + } |
| 58 | + private bool ShouldApplyNoPruneAttribute(IJsonClassGeneratorConfig config) |
| 59 | + { |
| 60 | + return config.ApplyObfuscationAttributes && !config.ExplicitDeserialization && config.UseProperties; |
| 61 | + } |
| 62 | + |
| 63 | + public void WriteFileStart(IJsonClassGeneratorConfig config, TextWriter sw) |
| 64 | + { |
| 65 | + if (config.UseNamespaces) |
| 66 | + { |
| 67 | + foreach (var line in JsonClassGenerator.FileHeader) |
| 68 | + { |
| 69 | + sw.WriteLine("// " + line); |
| 70 | + } |
| 71 | + sw.WriteLine(); |
| 72 | + sw.WriteLine("using System;"); |
| 73 | + sw.WriteLine("using System.Collections.Generic;"); |
| 74 | + if (ShouldApplyNoPruneAttribute(config) || ShouldApplyNoRenamingAttribute(config)) |
| 75 | + sw.WriteLine("using System.Reflection;"); |
| 76 | + if (!config.ExplicitDeserialization && config.UsePascalCase) |
| 77 | + sw.WriteLine("using Newtonsoft.Json;"); |
| 78 | + sw.WriteLine("using Newtonsoft.Json.Linq;"); |
| 79 | + if (config.ExplicitDeserialization) |
| 80 | + sw.WriteLine("using JsonCSharpClassGenerator;"); |
| 81 | + if (config.SecondaryNamespace != null && config.HasSecondaryClasses && !config.UseNestedClasses) |
| 82 | + { |
| 83 | + sw.WriteLine("using {0};", config.SecondaryNamespace); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if (config.UseNestedClasses) |
| 88 | + { |
| 89 | + sw.WriteLine(" {0} class {1}", config.InternalVisibility ? "internal" : "public", config.MainClass); |
| 90 | + sw.WriteLine(" {"); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public void WriteFileEnd(IJsonClassGeneratorConfig config, TextWriter sw) |
| 95 | + { |
| 96 | + if (config.UseNestedClasses) |
| 97 | + { |
| 98 | + sw.WriteLine(" }"); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + public void WriteNamespaceStart(IJsonClassGeneratorConfig config, TextWriter sw, bool root) |
| 104 | + { |
| 105 | + sw.WriteLine(); |
| 106 | + sw.WriteLine("namespace {0}", root && !config.UseNestedClasses ? config.Namespace : (config.SecondaryNamespace ?? config.Namespace)); |
| 107 | + sw.WriteLine("{"); |
| 108 | + sw.WriteLine(); |
| 109 | + } |
| 110 | + |
| 111 | + public void WriteNamespaceEnd(IJsonClassGeneratorConfig config, TextWriter sw, bool root) |
| 112 | + { |
| 113 | + sw.WriteLine("}"); |
| 114 | + } |
| 115 | + |
| 116 | + public void WriteClass(IJsonClassGeneratorConfig config, TextWriter sw, JsonType type) |
| 117 | + { |
| 118 | + var visibility = config.InternalVisibility ? "internal" : "public"; |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + if (config.UseNestedClasses) |
| 123 | + { |
| 124 | + if (!type.IsRoot) |
| 125 | + { |
| 126 | + if (ShouldApplyNoRenamingAttribute(config)) sw.WriteLine(" " + NoRenameAttribute); |
| 127 | + if (ShouldApplyNoPruneAttribute(config)) sw.WriteLine(" " + NoPruneAttribute); |
| 128 | + sw.WriteLine(" {0} class {1}", visibility, type.AssignedName); |
| 129 | + sw.WriteLine(" {"); |
| 130 | + } |
| 131 | + } |
| 132 | + else |
| 133 | + { |
| 134 | + if (ShouldApplyNoRenamingAttribute(config)) sw.WriteLine(" " + NoRenameAttribute); |
| 135 | + if (ShouldApplyNoPruneAttribute(config)) sw.WriteLine(" " + NoPruneAttribute); |
| 136 | + sw.WriteLine(" {0} class {1}", visibility, type.AssignedName); |
| 137 | + sw.WriteLine(" {"); |
| 138 | + } |
| 139 | + |
| 140 | + var prefix = config.UseNestedClasses && !type.IsRoot ? " " : " "; |
| 141 | + |
| 142 | + |
| 143 | + var shouldSuppressWarning = config.InternalVisibility && !config.UseProperties && !config.ExplicitDeserialization; |
| 144 | + if (shouldSuppressWarning) |
| 145 | + { |
| 146 | + sw.WriteLine("#pragma warning disable 0649"); |
| 147 | + if (!config.UsePascalCase) sw.WriteLine(); |
| 148 | + } |
| 149 | + |
| 150 | + if (type.IsRoot && config.ExplicitDeserialization) WriteStringConstructorExplicitDeserialization(config, sw, type, prefix); |
| 151 | + |
| 152 | + if (config.ExplicitDeserialization) |
| 153 | + { |
| 154 | + if (config.UseProperties) WriteClassWithPropertiesExplicitDeserialization(sw, type, prefix); |
| 155 | + else WriteClassWithFieldsExplicitDeserialization(sw, type, prefix); |
| 156 | + } |
| 157 | + else |
| 158 | + { |
| 159 | + WriteClassMembers(config, sw, type, prefix); |
| 160 | + } |
| 161 | + |
| 162 | + if (shouldSuppressWarning) |
| 163 | + { |
| 164 | + sw.WriteLine(); |
| 165 | + sw.WriteLine("#pragma warning restore 0649"); |
| 166 | + sw.WriteLine(); |
| 167 | + } |
| 168 | + |
| 169 | + |
| 170 | + if (config.UseNestedClasses && !type.IsRoot) |
| 171 | + sw.WriteLine(" }"); |
| 172 | + |
| 173 | + if (!config.UseNestedClasses) |
| 174 | + sw.WriteLine(" }"); |
| 175 | + |
| 176 | + sw.WriteLine(); |
| 177 | + |
| 178 | + |
| 179 | + } |
| 180 | + |
| 181 | + |
| 182 | + |
| 183 | + private void WriteClassMembers(IJsonClassGeneratorConfig config, TextWriter sw, JsonType type, string prefix) |
| 184 | + { |
| 185 | + foreach (var field in type.Fields) |
| 186 | + { |
| 187 | + if (config.UsePascalCase || config.ExamplesInDocumentation) sw.WriteLine(); |
| 188 | + |
| 189 | + if (config.ExamplesInDocumentation) |
| 190 | + { |
| 191 | + sw.WriteLine(prefix + "/// <summary>"); |
| 192 | + sw.WriteLine(prefix + "/// Examples: " + field.GetExamplesText()); |
| 193 | + sw.WriteLine(prefix + "/// </summary>"); |
| 194 | + } |
| 195 | + |
| 196 | + if (config.UsePascalCase) |
| 197 | + { |
| 198 | + |
| 199 | + sw.WriteLine(prefix + "[JsonProperty(\"{0}\")]", field.JsonMemberName); |
| 200 | + } |
| 201 | + |
| 202 | + if (config.UseProperties) |
| 203 | + { |
| 204 | + sw.WriteLine(prefix + "public {0} {1} {{ get; set; }}", field.Type.GetTypeName(), field.MemberName); |
| 205 | + } |
| 206 | + else |
| 207 | + { |
| 208 | + sw.WriteLine(prefix + "public {0} {1};", field.Type.GetTypeName(), field.MemberName); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + } |
| 213 | + |
| 214 | + |
| 215 | + |
| 216 | + |
| 217 | + |
| 218 | + |
| 219 | + |
| 220 | + #region Code for (obsolete) explicit deserialization |
| 221 | + private void WriteClassWithPropertiesExplicitDeserialization(TextWriter sw, JsonType type, string prefix) |
| 222 | + { |
| 223 | + |
| 224 | + sw.WriteLine(prefix + "private JObject __jobject;"); |
| 225 | + sw.WriteLine(prefix + "public {0}(JObject obj)", type.AssignedName); |
| 226 | + sw.WriteLine(prefix + "{"); |
| 227 | + sw.WriteLine(prefix + " this.__jobject = obj;"); |
| 228 | + sw.WriteLine(prefix + "}"); |
| 229 | + sw.WriteLine(); |
| 230 | + |
| 231 | + foreach (var field in type.Fields) |
| 232 | + { |
| 233 | + |
| 234 | + string variable = null; |
| 235 | + if (field.Type.MustCache) |
| 236 | + { |
| 237 | + variable = "_" + char.ToLower(field.MemberName[0]) + field.MemberName.Substring(1); |
| 238 | + sw.WriteLine(prefix + "[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]"); |
| 239 | + sw.WriteLine(prefix + "private {0} {1};", field.Type.GetTypeName(), variable); |
| 240 | + } |
| 241 | + |
| 242 | + |
| 243 | + sw.WriteLine(prefix + "public {0} {1}", field.Type.GetTypeName(), field.MemberName); |
| 244 | + sw.WriteLine(prefix + "{"); |
| 245 | + sw.WriteLine(prefix + " get"); |
| 246 | + sw.WriteLine(prefix + " {"); |
| 247 | + if (field.Type.MustCache) |
| 248 | + { |
| 249 | + sw.WriteLine(prefix + " if ({0} == null)", variable); |
| 250 | + sw.WriteLine(prefix + " {0} = {1};", variable, field.GetGenerationCode("__jobject")); |
| 251 | + sw.WriteLine(prefix + " return {0};", variable); |
| 252 | + } |
| 253 | + else |
| 254 | + { |
| 255 | + sw.WriteLine(prefix + " return {0};", field.GetGenerationCode("__jobject")); |
| 256 | + } |
| 257 | + sw.WriteLine(prefix + " }"); |
| 258 | + sw.WriteLine(prefix + "}"); |
| 259 | + sw.WriteLine(); |
| 260 | + |
| 261 | + } |
| 262 | + |
| 263 | + } |
| 264 | + |
| 265 | + |
| 266 | + private void WriteStringConstructorExplicitDeserialization(IJsonClassGeneratorConfig config, TextWriter sw, JsonType type, string prefix) |
| 267 | + { |
| 268 | + sw.WriteLine(); |
| 269 | + sw.WriteLine(prefix + "public {1}(string json)", config.InternalVisibility ? "internal" : "public", type.AssignedName); |
| 270 | + sw.WriteLine(prefix + " : this(JObject.Parse(json))"); |
| 271 | + sw.WriteLine(prefix + "{"); |
| 272 | + sw.WriteLine(prefix + "}"); |
| 273 | + sw.WriteLine(); |
| 274 | + } |
| 275 | + |
| 276 | + private void WriteClassWithFieldsExplicitDeserialization(TextWriter sw, JsonType type, string prefix) |
| 277 | + { |
| 278 | + |
| 279 | + |
| 280 | + sw.WriteLine(prefix + "public {0}(JObject obj)", type.AssignedName); |
| 281 | + sw.WriteLine(prefix + "{"); |
| 282 | + |
| 283 | + foreach (var field in type.Fields) |
| 284 | + { |
| 285 | + sw.WriteLine(prefix + " this.{0} = {1};", field.MemberName, field.GetGenerationCode("obj")); |
| 286 | + |
| 287 | + } |
| 288 | + |
| 289 | + sw.WriteLine(prefix + "}"); |
| 290 | + sw.WriteLine(); |
| 291 | + |
| 292 | + foreach (var field in type.Fields) |
| 293 | + { |
| 294 | + sw.WriteLine(prefix + "public readonly {0} {1};", field.Type.GetTypeName(), field.MemberName); |
| 295 | + } |
| 296 | + } |
| 297 | + #endregion |
| 298 | + |
| 299 | + } |
| 300 | +} |
0 commit comments