Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JsonPropertyNameAttribute to properties and SerializableAttribute to class #44

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ JsonUtils.v12.suo
Deploy/
JsonCSharpClassGeneratorLib/bin/
JsonCSharpClassGeneratorLib/obj/
/.vs/JsonUtils
/UpgradeLog.htm
4 changes: 3 additions & 1 deletion JsonCSharpClassGeneratorLib/CodeWriters/CSharpCodeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public void WriteClass(IJsonClassGeneratorConfig config, TextWriter sw, JsonType
{
sw.WriteLine(" [DataContract]");
}
sw.WriteLine(" [Serializable]");

if (ShouldApplyNoRenamingAttribute(config)) sw.WriteLine(" " + NoRenameAttribute);
if (ShouldApplyNoPruneAttribute(config)) sw.WriteLine(" " + NoPruneAttribute);
Expand All @@ -139,6 +140,7 @@ public void WriteClass(IJsonClassGeneratorConfig config, TextWriter sw, JsonType
{
sw.WriteLine(" [DataContract]");
}
sw.WriteLine(" [Serializable]");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent whitespace in generated code.


if (ShouldApplyNoRenamingAttribute(config)) sw.WriteLine(" " + NoRenameAttribute);
if (ShouldApplyNoPruneAttribute(config)) sw.WriteLine(" " + NoPruneAttribute);
Expand Down Expand Up @@ -206,7 +208,7 @@ private void WriteClassMembers(IJsonClassGeneratorConfig config, TextWriter sw,
{
if (config.PropertyAttribute == "DataMember")
sw.WriteLine(prefix + "[" + config.PropertyAttribute + "(Name=\"{0}\")]", field.JsonMemberName);
else if (config.PropertyAttribute == "JsonProperty")
else if (config.PropertyAttribute == "JsonProperty" || config.PropertyAttribute == "JsonPropertyName")
sw.WriteLine(prefix + "[" + config.PropertyAttribute + "(\"{0}\")]", field.JsonMemberName);
}

Expand Down
34 changes: 18 additions & 16 deletions JsonUtils/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ActionResult Index(string url)
{
vm.JSON = new WebClient().DownloadString(url);
}
catch (Exception ex)
catch (Exception)
{
vm.Error = true;
vm.ErrorNo = 4;
Expand All @@ -58,7 +58,7 @@ public ActionResult Index(string url)
{
vm.CodeObjects = Server.HtmlEncode(Prepare(vm.JSON, vm.ClassName, 1, vm.Nest, false, vm.PropertyAttribute, vm.Namespace));
}
catch (Exception ex)
catch (Exception)
{
vm.Error = true;
vm.ErrorNo = 3;
Expand Down Expand Up @@ -92,7 +92,7 @@ public ActionResult Index(IndexViewModel model)
{
model.JSON = new WebClient().DownloadString(model.JSON);
}
catch (Exception ex)
catch (Exception)
{
model.Error = true;
model.ErrorNo = 4;
Expand All @@ -104,7 +104,7 @@ public ActionResult Index(IndexViewModel model)
{
return View(model);
}

try
{
if (model.Language != 3)
Expand All @@ -117,12 +117,12 @@ public ActionResult Index(IndexViewModel model)
else
model.CodeObjects = "javascript";
}
catch (Exception ex)
catch (Exception)
{
model.Error = true;
model.ErrorNo = 3;
model.ErrorNo = 3;
}

return View(model);
}

Expand All @@ -134,7 +134,7 @@ public ActionResult Index(IndexViewModel model)
new TypeScriptCodeWriter()
};

private string Prepare(string JSON, string classname, int language, bool nest, bool pascal, string propertyAttribute, string nameSpace, bool hasGetSet=false)
private string Prepare(string JSON, string classname, int language, bool nest, bool pascal, string propertyAttribute, string nameSpace, bool hasGetSet = false)
{
if (string.IsNullOrEmpty(JSON))
{
Expand All @@ -149,18 +149,20 @@ private string Prepare(string JSON, string classname, int language, bool nest, b
writer = new VisualBasicCodeWriter();
else if (language == 7)
writer = new TypeScriptCodeWriter();
else if(language == 4)
else if (language == 4)
writer = new SqlCodeWriter();
else if(language == 5)
else if (language == 5)
writer = new JavaCodeWriter();
else
writer = new PhpCodeWriter();

var gen = new JsonClassGenerator();
gen.Example = JSON;
gen.InternalVisibility = false;
gen.CodeWriter = writer;
gen.ExplicitDeserialization = false;
var gen = new JsonClassGenerator
{
Example = JSON,
InternalVisibility = false,
CodeWriter = writer,
ExplicitDeserialization = false
};
if (nest)
gen.Namespace = nameSpace;
else
Expand All @@ -177,7 +179,7 @@ private string Prepare(string JSON, string classname, int language, bool nest, b
gen.ApplyObfuscationAttributes = false;
gen.SingleFile = true;
gen.ExamplesInDocumentation = false;

gen.TargetFolder = null;
gen.SingleFile = true;

Expand Down
3 changes: 2 additions & 1 deletion JsonUtils/ViewModels/IndexViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public List<SelectListItem> PropertyAttributeOptions
return new List<SelectListItem>() {
new SelectListItem() { Text = "None", Value = "None" },
new SelectListItem() { Text = "DataMember", Value = "DataMember" },
new SelectListItem() { Text = "JsonProperty", Value = "JsonProperty" }
new SelectListItem() { Text = "JsonProperty", Value = "JsonProperty" },
new SelectListItem() { Text = "JsonPropertyName", Value = "JsonPropertyName" },
};
}
}
Expand Down