Skip to content

Commit

Permalink
converting works with no nested types
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohamed Ali committed Sep 12, 2021
1 parent 3181d60 commit 1ee398b
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 31 additions & 1 deletion Form1.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Selim.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
Expand Down Expand Up @@ -26,5 +27,34 @@ private void tableLayoutPanel2_Paint(object sender, PaintEventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
// copy btn
try
{
Clipboard.SetText(dartTextBox.Text);
}
catch { }
}

private void convertBtn_Click(object sender, EventArgs e)
{
if (String.IsNullOrWhiteSpace(jsonTextBox.Text)) return;
//
var jp = new JsonParser();
var json = jp.Parse(jsonTextBox.Text);

if (!(json is JsonObject))
{
//ShowDialog()
return;
}
//
var jObject = json as JsonObject;

jObject.setClassName(string.IsNullOrWhiteSpace(classNameTextBox.Text)? "RootClass" : classNameTextBox.Text);
dartTextBox.Text = jObject.createDartClass(new StringBuilder());
}
}
}
26 changes: 25 additions & 1 deletion Json/JsonArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public JsonArray addAll(bool[] arr)

public int Length { get { return values.Count; } }


public JsonValue[] getValues()
{
return values.ToArray();
Expand Down Expand Up @@ -215,5 +216,28 @@ public override void toString(StringBuilder sb, int? indents)
}

}
}

public override string dartTypeName => $"List<{((Length == 0 || values[0] is JsonNull)? "Object?" : values[0].dartTypeName)}>";

public override string toDartMapAssignmentExpr(string name)
{
var nullType = (Length == 0 || values[0] is JsonNull);

return $"\t\tif (this.{name} != null) {{\r\n" +
$"\t\t\tdata['{name}'] = this.{name}?.map((v) => v).toList();\r\n" +
$"\t\t}}";
}

public override string toDartMapFetchingExpr(string name)
{
var nullType = (Length == 0 || values[0] is JsonNull);

return $"\t\tif (map['{name}'] != null) {{\r\n" +
$"\t\t\t{name} = [];\r\n" +
$"\t\t\tmap['{name}'].forEach((v) {{\r\n"+
$"\t\t\t\t{name}!.add(v);\r\n" +
$"\t\t\t}});\r\n" +
$"\t\t}}";
}
}
}
14 changes: 12 additions & 2 deletions Json/JsonBoolean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,19 @@ public JsonBoolean(bool value)
this.value = value;
}


public override string dartTypeName => "bool";

public override void toString(StringBuilder sb, int? indents)
public override string toDartMapAssignmentExpr(string name)
{
return $"\t\tdata['{name}'] = this.{name};";
}

public override string toDartMapFetchingExpr(string name)
{
return $"\t\t{name} = map['{name}'];";
}

public override void toString(StringBuilder sb, int? indents)
{
sb.Append(value ? "true" : "false");
}
Expand Down
14 changes: 12 additions & 2 deletions Json/JsonNull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,19 @@ namespace Selim.Json
{
class JsonNull : JsonValue
{
public override string dartTypeName => "Object";


public override void toString(StringBuilder sb, int? indents)
public override string toDartMapAssignmentExpr(string name)
{
return $"\t\tdata['{name}'] = this.{name};";
}

public override string toDartMapFetchingExpr(string name)
{
return $"\t\t{name} = map['{name}'];";
}

public override void toString(StringBuilder sb, int? indents)
{
sb.Append("null");
}
Expand Down
13 changes: 12 additions & 1 deletion Json/JsonNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,19 @@ public JsonNumber(double value)
this.value = value;
}

public override string dartTypeName => value%1==0? "int" : "double";

public override void toString(StringBuilder sb, int? indents)
public override string toDartMapAssignmentExpr(string name)
{
return $"\t\tdata['{name}'] = this.{name};";
}

public override string toDartMapFetchingExpr(string name)
{
return $"\t\t{name} = map['{name}'];";
}

public override void toString(StringBuilder sb, int? indents)
{
sb.Append(value.ToString());
}
Expand Down
73 changes: 72 additions & 1 deletion Json/JsonObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,78 @@ public override void toString(StringBuilder sb, int? indents)
}
}

}

public override string dartTypeName => className ?? throw new Exception("set class name first");


public override string toDartMapAssignmentExpr(string name)
{
return $"\t\tif (this.{name} != null) {{\r\n" +
$"\t\t\tdata['{name}'] = this.{name}?.toMap();\r\n"+
$"\t\t}}";
}

public override string toDartMapFetchingExpr(string name)
{
return $"\t{name} = map['{name}'] != null ? ({dartTypeName}()..fromMap(map['{name}'])) : null;";
}

string className;
public void setClassName(string name) => className = name;

public string createDartClass(StringBuilder sb)
{
sb.AppendLine($"class {dartTypeName} extends Serializable{{");

// declarations
foreach (var item in this.nameValues)
{
sb.AppendLine(item.value.toDartFieldDeclaration(item.name));
}

sb.AppendLine();

// constructor
sb.Append($"\t{dartTypeName}({{");
foreach (var item in this.nameValues)
{
sb.Append(item.value.toDartConstuctorParam(item.name));
}
sb.AppendLine("});");
sb.AppendLine();

// from map method
sb.AppendLine("\t@override");
sb.AppendLine("\tvoid fromMap(Map<String, dynamic> map) {");
//
foreach (var item in this.nameValues)
{
sb.AppendLine(item.value.toDartMapFetchingExpr(item.name));
}
//
sb.AppendLine("\t}");
sb.AppendLine();

// to map method
sb.AppendLine("\t@override");
sb.AppendLine("\tMap<String, dynamic> toMap() {");
sb.AppendLine("\t\tfinal Map<String, dynamic> data = new Map<String, dynamic>();");
//
foreach (var item in this.nameValues)
{
sb.AppendLine(item.value.toDartMapAssignmentExpr(item.name));
}
//
sb.AppendLine("\t\treturn data;");
sb.AppendLine("\t}");


sb.AppendLine("}");
//
return sb.ToString();
}

}


public class NameValue
Expand Down
16 changes: 14 additions & 2 deletions Json/JsonString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,20 @@ public JsonString(string value)
this.value = value;
}


public override void toString(StringBuilder sb, int? indents)
public override string dartTypeName => "String";


public override string toDartMapAssignmentExpr(string name)
{
return $"\t\tdata['{name}'] = this.{name};";
}

public override string toDartMapFetchingExpr(string name)
{
return $"\t\t{name} = map['{name}'];";
}

public override void toString(StringBuilder sb, int? indents)
{
sb.AppendFormat("\"{0}\"", value.Replace("\"","\\\""));
}
Expand Down
28 changes: 28 additions & 0 deletions Json/JsonValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,33 @@ protected void appendMany(StringBuilder sb, string s,int times)
}
}

// dart methods

public string toDartFieldDeclaration(string name)
{
return $"\t{this.dartTypeName}? {name};";
}

public string toDartConstuctorParam(string name)
{
return $"this.{name}, ";
}

/// <summary>
/// to be used in toMap()
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public abstract string toDartMapAssignmentExpr(string name);

/// <summary>
/// to be used in fromMap()
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public abstract string toDartMapFetchingExpr(string name);

public abstract string dartTypeName { get; }

}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# JsonToDart

A Dotnet App to create dart class from json

The dart class extends Serializable base class

0 comments on commit 1ee398b

Please sign in to comment.