-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathJavaCodeWriter.cs
203 lines (173 loc) · 8.38 KB
/
JavaCodeWriter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Xamasoft.JsonClassGenerator.CodeWriters
{
public class JavaCodeWriter : ICodeWriter
{
public string FileExtension
{
get { return ".java"; }
}
public string DisplayName
{
get { return "Java"; }
}
public string GetTypeName(JsonType type, IJsonClassGeneratorConfig config)
{
var arraysAsLists = !config.ExplicitDeserialization;
switch (type.Type)
{
case JsonTypeEnum.Anything: return "Object";
case JsonTypeEnum.Array: return arraysAsLists ? "List<" + GetTypeName(type.InternalType, config) + ">" : GetTypeName(type.InternalType, config) + "[]";
case JsonTypeEnum.Dictionary: return "Dictionary<string, " + GetTypeName(type.InternalType, config) + ">";
case JsonTypeEnum.Boolean: return "boolean";
case JsonTypeEnum.Float: return "double";
case JsonTypeEnum.Integer: return "int";
case JsonTypeEnum.Long: return "long";
case JsonTypeEnum.Date: return "Date";
case JsonTypeEnum.NonConstrained: return "Object";
case JsonTypeEnum.NullableBoolean: return "bool?";
case JsonTypeEnum.NullableFloat: return "double?";
case JsonTypeEnum.NullableInteger: return "int?";
case JsonTypeEnum.NullableLong: return "long?";
case JsonTypeEnum.NullableDate: return "DateTime?";
case JsonTypeEnum.NullableSomething: return "object";
case JsonTypeEnum.Object: return type.AssignedName;
case JsonTypeEnum.String: return "String";
default: throw new System.NotSupportedException("Unsupported json type");
}
}
public void WriteClass(IJsonClassGeneratorConfig config, TextWriter sw, JsonType type)
{
var visibility = config.InternalVisibility ? "" : "public";
//if (config.UseNestedClasses)
//{
// if (!type.IsRoot)
// {
// if (config.PropertyAttribute == "DataMember")
// {
// sw.WriteLine(" [DataContract]");
// }
// if (ShouldApplyNoRenamingAttribute(config)) sw.WriteLine(" " + NoRenameAttribute);
// if (ShouldApplyNoPruneAttribute(config)) sw.WriteLine(" " + NoPruneAttribute);
// sw.WriteLine(" {0} class {1}", visibility, type.AssignedName);
// sw.WriteLine(" {");
// }
//}
//else
//{
// if (config.PropertyAttribute == "DataMember")
// {
// sw.WriteLine(" [DataContract]");
// }
// if (ShouldApplyNoRenamingAttribute(config)) sw.WriteLine(" " + NoRenameAttribute);
// if (ShouldApplyNoPruneAttribute(config)) sw.WriteLine(" " + NoPruneAttribute);
sw.WriteLine("{0} class {1}", visibility, type.AssignedName);
sw.WriteLine("{");
//}
var prefix = config.UseNestedClasses && !type.IsRoot ? "" : " ";
var shouldSuppressWarning = config.InternalVisibility && !config.UseProperties && !config.ExplicitDeserialization;
if (shouldSuppressWarning)
{
sw.WriteLine("#pragma warning disable 0649");
if (!config.UsePascalCase) sw.WriteLine();
}
//if (type.IsRoot && config.ExplicitDeserialization) WriteStringConstructorExplicitDeserialization(config, sw, type, prefix);
//if (config.ExplicitDeserialization)
//{
// if (config.UseProperties) WriteClassWithPropertiesExplicitDeserialization(sw, type, prefix);
// else WriteClassWithFieldsExplicitDeserialization(sw, type, prefix);
//}
//else
//{
WriteClassMembers(config, sw, type, prefix);
//}
if (shouldSuppressWarning)
{
sw.WriteLine();
sw.WriteLine("#pragma warning restore 0649");
sw.WriteLine();
}
if (config.UseNestedClasses && !type.IsRoot)
sw.WriteLine(" }");
if (!config.UseNestedClasses)
sw.WriteLine("}");
sw.WriteLine();
}
public void WriteFileStart(IJsonClassGeneratorConfig config, TextWriter sw)
{
foreach (var line in JsonClassGenerator.FileHeader)
{
sw.WriteLine("// " + line);
}
}
public void WriteFileEnd(IJsonClassGeneratorConfig config, TextWriter sw)
{
throw new NotImplementedException();
}
public void WriteNamespaceStart(IJsonClassGeneratorConfig config, TextWriter sw, bool root)
{
sw.WriteLine();
sw.WriteLine("package {0};", root && !config.UseNestedClasses ? config.Namespace : (config.SecondaryNamespace ?? config.Namespace));
sw.WriteLine();
}
public void WriteNamespaceEnd(IJsonClassGeneratorConfig config, TextWriter sw, bool root)
{
sw.WriteLine();
}
private void WriteClassMembers(IJsonClassGeneratorConfig config, TextWriter sw, JsonType type, string prefix)
{
foreach (var field in type.Fields)
{
//if (config.UsePascalCase || config.ExamplesInDocumentation) sw.WriteLine();
//if (config.ExamplesInDocumentation)
//{
// sw.WriteLine(prefix + "/// <summary>");
// sw.WriteLine(prefix + "/// Examples: " + field.GetExamplesText());
// sw.WriteLine(prefix + "/// </summary>");
//}
//if (config.UsePascalCase || config.PropertyAttribute != "None")
//{
// if (config.UsePascalCase && config.PropertyAttribute == "None")
// sw.WriteLine(prefix + "@JsonProperty(\"{0}\")", field.JsonMemberName);
// else
// {
// //if (config.PropertyAttribute == "DataMember")
// // sw.WriteLine(prefix + "[" + config.PropertyAttribute + "(Name=\"{0}\")]", field.JsonMemberName);
// if (config.PropertyAttribute == "JsonProperty")
// sw.WriteLine(prefix + "@" + config.PropertyAttribute + "(\"{0}\")", field.JsonMemberName);
// }
//}
if (config.UseProperties)
{
sw.WriteLine(prefix + "@JsonProperty" + "(\"{0}\")", field.JsonMemberName);
sw.WriteLine(prefix + "public {0} get{1}() {{ \r\t\t return this.{2} \r\t}}", field.Type.GetTypeName(), ChangeFirstChar(field.MemberName), ChangeFirstChar(field.MemberName, false));
sw.WriteLine(prefix + "public void set{1}({0} {2}) {{ \r\t\t this.{2} = {2} \r\t}}", field.Type.GetTypeName(), ChangeFirstChar(field.MemberName), ChangeFirstChar(field.MemberName, false));
sw.WriteLine(prefix + "{0} {1};", field.Type.GetTypeName(), ChangeFirstChar(field.MemberName, false));
sw.WriteLine();
}
else
{
string memberName = ChangeFirstChar(field.MemberName, false);
if(field.JsonMemberName != memberName)
sw.WriteLine(prefix + "@JsonProperty" + "(\"{0}\")", field.JsonMemberName);
sw.WriteLine(prefix + "public {0} {1};", field.Type.GetTypeName(), memberName);
}
}
}
private static string ChangeFirstChar(string value, bool toCaptial = true)
{
if (value == null)
throw new ArgumentNullException("value");
if (value.Length == 0)
return value;
StringBuilder sb = new StringBuilder();
sb.Append(toCaptial ? char.ToUpper(value[0]) : char.ToLower(value[0]));
sb.Append(value.Substring(1));
return sb.ToString();
}
}
}