Skip to content

Commit

Permalink
getting the Serializable class from the console
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohamed Ali committed Sep 15, 2021
1 parent 2dfb61f commit 137674e
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 7 deletions.
2 changes: 1 addition & 1 deletion J2dConsole/ConsoleUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static string readConsoleMultiline()
while(true)
{
ConsoleKeyInfo key = Console.ReadKey();
if (!(key.Key == ConsoleKey.Enter && key.Modifiers == ConsoleModifiers.Control))
if (key.Key == ConsoleKey.Enter && key.Modifiers == ConsoleModifiers.Control)
{
break;
}
Expand Down
48 changes: 42 additions & 6 deletions J2dConsole/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using JsonToDart;
using J2dLib;
using JsonToDart;
using Selim.Json;
using System;
using System.IO;
Expand All @@ -11,7 +12,7 @@ public class Program
{
public static async Task Main(string[] args)
{
args = new string[] {"-i" };
//args = new string[] {"-i" };
Console.WriteLine("DartToJson version 1.0.0");
//
if(args.Length == 0)
Expand All @@ -30,6 +31,10 @@ public static async Task Main(string[] args)
{
await runInteractiveModeAsync();
}
else if (args[0] == "-s")
{
await provideTheSerializableCodeAsync();
}
else writeHelp();
//
Console.WriteLine("Press any key to exit");
Expand All @@ -38,6 +43,32 @@ public static async Task Main(string[] args)

}

private static async Task provideTheSerializableCodeAsync()
{
var dart = Resource1.Serializable;

Console.WriteLine("How do you want to take the Serializable class dart code?");
Console.WriteLine("[f] file [c] clipboard [h] here in the console [x] exit:");
var option = Console.ReadLine();
if (option == "f")
{
Console.WriteLine("Write the file path and hit Enter:");
FileUtil.writeFile(Console.ReadLine(), dart);
}
else if (option == "c")
{
await ClipboardUtil.setTextAsync(dart);
}
else if (option == "h")
{
Console.WriteLine(dart);
}
else if (option == "x")
{
return;
}
}

private static async Task runInteractiveModeAsync()
{
string input = null, className = null;
Expand All @@ -47,7 +78,7 @@ private static async Task runInteractiveModeAsync()
do
{
Console.WriteLine("How do you want to input your json ?");
Console.WriteLine("[f] file [c] clipboard [h] here in the console [x] exit");
Console.WriteLine("[f] file [c] clipboard [h] here in the console [x] exit:");
var option = Console.ReadLine();
if(option == "f")
{
Expand All @@ -60,6 +91,7 @@ private static async Task runInteractiveModeAsync()
}
else if(option == "h")
{
Console.WriteLine("Write your json and press CTRL+Enter to finish:");
input = ConsoleUtil.readConsoleMultiline();
}
else if(option == "x")
Expand All @@ -71,7 +103,7 @@ private static async Task runInteractiveModeAsync()

// take the class name

Console.WriteLine("What is the name you prefer for your generated class? Leave empty for default (RootClass)");
Console.WriteLine("What is the name you prefer for your generated class? Leave empty for default (RootClass):");
var cn = Console.ReadLine();
className = string.IsNullOrWhiteSpace(cn)? "RootClass" : cn;

Expand All @@ -85,7 +117,7 @@ private static async Task runInteractiveModeAsync()
do
{
Console.WriteLine("How do you want to take the generated dart code?");
Console.WriteLine("[f] file [c] clipboard [h] here in the console [x] exit");
Console.WriteLine("[f] file [c] clipboard [h] here in the console [x] exit:");
var option = Console.ReadLine();
if (option == "f")
{
Expand Down Expand Up @@ -140,7 +172,8 @@ public static void runFileMode(string[] args)

if (dart == null) return;

if(! FileUtil.writeFile(args[2], dart)) return;
var outFile = args.Length < 3 ? args[1] + ".dart" : args[2];
if(! FileUtil.writeFile(outFile, dart)) return;

Console.WriteLine("Done");

Expand All @@ -155,6 +188,9 @@ private static void writeHelp()
+"to take input from the clipboard and write to the clipbord\r\n");

Console.WriteLine(" -i \r\n for the interactive mode\r\n");

Console.WriteLine(" -s \r\n "
+ "to get the Serializable class code\r\n");
}

static string createDartClass(string jsonText, string className = "RootClass")
Expand Down
19 changes: 19 additions & 0 deletions J2dLib/J2dLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,23 @@
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Update="Resource1.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resource1.resx</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Resource1.resx">
<Generator>PublicResXFileCodeGenerator</Generator>
<LastGenOutput>Resource1.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<Folder Include="Resources\" />
</ItemGroup>

</Project>
89 changes: 89 additions & 0 deletions J2dLib/Resource1.Designer.cs

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

124 changes: 124 additions & 0 deletions J2dLib/Resource1.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="Serializable" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>Resources\Serializable.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
</root>
18 changes: 18 additions & 0 deletions J2dLib/Resources/Serializable.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'dart:convert';

abstract class Serializable {

String serialize() {
return json.encode(this.toMap());
}

void deserialize(String text) {
this.fromMap(json.decode(text));
}

//abstract
Map<String, dynamic> toMap();

//abstract
void fromMap(Map<String, dynamic> map);
}

0 comments on commit 137674e

Please sign in to comment.