Skip to content

Commit

Permalink
improved json entering to the console
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamed ali committed Sep 24, 2021
1 parent 137674e commit ecc86f0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 30 deletions.
4 changes: 2 additions & 2 deletions J2dConsole/ClipboardUtil.cs
Expand Up @@ -17,7 +17,7 @@ public static async Task<string> getTextAsync()
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
ConsoleUtil.writeError(ex.Message);
return null;
}
}
Expand All @@ -32,7 +32,7 @@ public static async Task<bool> setTextAsync(string text)
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
ConsoleUtil.writeError(ex.Message);
return false;
}
}
Expand Down
31 changes: 28 additions & 3 deletions J2dConsole/ConsoleUtil.cs
Expand Up @@ -8,20 +8,45 @@ public static class ConsoleUtil
public static string readConsoleMultiline()
{
StringBuilder output = new StringBuilder();

Console.BackgroundColor = ConsoleColor.DarkBlue;

while(true)
{
ConsoleKeyInfo key = Console.ReadKey();
if (key.Key == ConsoleKey.Enter && key.Modifiers == ConsoleModifiers.Control)
string line = Console.ReadLine();
if (line == "end")
{
Console.ResetColor();
break;
}
//
output.Append(key.KeyChar);
output.AppendLine(line);
}

//
return output.ToString();
}

public static void writeText(string text)
{
Console.BackgroundColor = ConsoleColor.DarkBlue;
//
Console.Write(text);
//
Console.ResetColor();
Console.WriteLine();
}

public static void writeError(string text)
{
Console.BackgroundColor = ConsoleColor.DarkRed;
//
Console.Write(text);
//
//
Console.ResetColor();
Console.WriteLine();
}

}
}
8 changes: 4 additions & 4 deletions J2dConsole/FileUtil.cs
Expand Up @@ -17,8 +17,8 @@ public static string readFile(string path)
}
catch (Exception ex)
{
Console.WriteLine($"Failed to read file: {path}");
Console.WriteLine(ex.Message);
ConsoleUtil.writeError($"Failed to read file: {path}");
ConsoleUtil.writeError(ex.Message);
return null;
}
}
Expand All @@ -33,8 +33,8 @@ public static bool writeFile(string path, string content)
}
catch (Exception ex)
{
Console.WriteLine($"Failed to write file: {path}");
Console.WriteLine(ex.Message);
ConsoleUtil.writeError($"Failed to write file: {path}");
ConsoleUtil.writeError(ex.Message);
return false;
}
}
Expand Down
19 changes: 10 additions & 9 deletions J2dConsole/Program.cs
Expand Up @@ -13,7 +13,7 @@ public class Program
public static async Task Main(string[] args)
{
//args = new string[] {"-i" };
Console.WriteLine("DartToJson version 1.0.0");
Console.WriteLine("DartToJson version 1.0.1");
//
if(args.Length == 0)
{
Expand All @@ -37,8 +37,8 @@ public static async Task Main(string[] args)
}
else writeHelp();
//
Console.WriteLine("Press any key to exit");
Console.ReadKey();
//Console.WriteLine("Press any key to exit");
//Console.ReadKey();


}
Expand All @@ -61,7 +61,7 @@ private static async Task provideTheSerializableCodeAsync()
}
else if (option == "h")
{
Console.WriteLine(dart);
ConsoleUtil.writeText(dart);
}
else if (option == "x")
{
Expand Down Expand Up @@ -91,7 +91,7 @@ private static async Task runInteractiveModeAsync()
}
else if(option == "h")
{
Console.WriteLine("Write your json and press CTRL+Enter to finish:");
Console.WriteLine("Write your json, then write 'end' in a seperate line to finish:");
input = ConsoleUtil.readConsoleMultiline();
}
else if(option == "x")
Expand Down Expand Up @@ -130,7 +130,7 @@ private static async Task runInteractiveModeAsync()
}
else if (option == "h")
{
Console.WriteLine(dart);
ConsoleUtil.writeText(dart);
}
else if (option == "x")
{
Expand Down Expand Up @@ -184,7 +184,7 @@ private static void writeHelp()
Console.WriteLine("\r\n -f [input file] [(optional) output file] [(optional) class name]\r\n"
+" to take json input file and write output dart file \r\n");

Console.WriteLine(" -c [class name]\r\n "
Console.WriteLine(" -c [(optional) class name]\r\n "
+"to take input from the clipboard and write to the clipbord\r\n");

Console.WriteLine(" -i \r\n for the interactive mode\r\n");
Expand All @@ -200,11 +200,12 @@ static string createDartClass(string jsonText, string className = "RootClass")
try
{
var jp = new JsonParser();

var json = jp.Parse(jsonText);

if (!(json is JsonObject))
{
Console.WriteLine("must be json object");
ConsoleUtil.writeError("must be json object");
return null;
}
//
Expand All @@ -216,7 +217,7 @@ static string createDartClass(string jsonText, string className = "RootClass")
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
ConsoleUtil.writeError(ex.Message);
return null;
}
}
Expand Down
18 changes: 6 additions & 12 deletions J2dLib/Json/JsonParser.cs
Expand Up @@ -33,17 +33,7 @@ public JsonValue Parse(string text)
{
switch (state)
{
//case 0:
// if (Regex.IsMatch(token.Value, "{"))
// {
// state = 1;
// stack.Push(new JsonObject());
// }
// else
// {
// parsingError();
// }
// break;

case 1:
if ( token.Value == "}")
{
Expand Down Expand Up @@ -224,6 +214,10 @@ public JsonValue Parse(string text)
break;
}
}//end foreach
if (stack.Count == 0)
{
parsingError();
}
return stack.Pop();

}
Expand All @@ -232,7 +226,7 @@ public JsonValue Parse(string text)

private void parsingError()
{
throw new Exception("Parsing Error");
throw new Exception("Json Parsing Error");
}


Expand Down

0 comments on commit ecc86f0

Please sign in to comment.