Skip to content

Commit e2623b1

Browse files
committed
JsonDataContextDriver
working Delete ryan.snk strong naming is Bad
1 parent 431dd89 commit e2623b1

33 files changed

+2787
-1
lines changed

.gitattributes

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
###############################################################################
2+
# Set default behavior to automatically normalize line endings.
3+
###############################################################################
4+
* text=auto
5+
6+
###############################################################################
7+
# Set default behavior for command prompt diff.
8+
#
9+
# This is need for earlier builds of msysgit that does not have it on by
10+
# default for csharp files.
11+
# Note: This is only used by command line
12+
###############################################################################
13+
#*.cs diff=csharp
14+
15+
###############################################################################
16+
# Set the merge driver for project and solution files
17+
#
18+
# Merging from the command prompt will add diff markers to the files if there
19+
# are conflicts (Merging from VS is not affected by the settings below, in VS
20+
# the diff markers are never inserted). Diff markers may cause the following
21+
# file extensions to fail to load in VS. An alternative would be to treat
22+
# these files as binary and thus will always conflict and require user
23+
# intervention with every merge. To do so, just uncomment the entries below
24+
###############################################################################
25+
#*.sln merge=binary
26+
#*.csproj merge=binary
27+
#*.vbproj merge=binary
28+
#*.vcxproj merge=binary
29+
#*.vcproj merge=binary
30+
#*.dbproj merge=binary
31+
#*.fsproj merge=binary
32+
#*.lsproj merge=binary
33+
#*.wixproj merge=binary
34+
#*.modelproj merge=binary
35+
#*.sqlproj merge=binary
36+
#*.wwaproj merge=binary
37+
38+
###############################################################################
39+
# behavior for image files
40+
#
41+
# image files are treated as binary by default.
42+
###############################################################################
43+
#*.jpg binary
44+
#*.png binary
45+
#*.gif binary
46+
47+
###############################################################################
48+
# diff behavior for common document formats
49+
#
50+
# Convert binary document formats to text before diffing them. This feature
51+
# is only available from the command line. Turn it on by uncommenting the
52+
# entries below.
53+
###############################################################################
54+
#*.doc diff=astextplain
55+
#*.DOC diff=astextplain
56+
#*.docx diff=astextplain
57+
#*.DOCX diff=astextplain
58+
#*.dot diff=astextplain
59+
#*.DOT diff=astextplain
60+
#*.pdf diff=astextplain
61+
#*.PDF diff=astextplain
62+
#*.rtf diff=astextplain
63+
#*.RTF diff=astextplain

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,5 @@ FakesAssemblies/
194194

195195
# Visual Studio 6 workspace options file
196196
*.opt
197+
198+
*.snk
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
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

Comments
 (0)