alec1o / Byter

Byter is a C# serializer. It can serialize and deserialize from primitive type e.g (class, struct, list, array, string...)
MIT License
3 stars 1 forks source link

Class and No Head Support #8

Open KFKMan opened 2 months ago

KFKMan commented 2 months ago

Can you add Class support? With Two Types Of Reading; 1- With Prop Index (you can create a Attribute for that like Index(0) İndex(1)) 2- With Head Data => With Prop Name or if class has no duplicate property type Reading With only property type

On Read Ended => Result class need to be returned.

.Result => Serialize/Deserialize Result .IsSuccesfull => Simply check for any big error accoured if not check for Errors ant have element .Errors => A dictionary like a <Property, Error> for giving which properties are not readed/writed.

And can you add Extension methods like ReadInt(), WriteInt(), ReadBool().... etc. This will help developers to detect problem.

alec1o commented 2 months ago

lol... bro you read my mind... look version 3 example; but byter will be very good in version 3;

I started develop version 3, hours before you send open this Issue and i had same thing on mind :rofl: I hope done this version 3 in this weeks. 7 day of development and tests, Will release soon because i wanna use this new feature to Netly

Writer Example

using Byter;

By b = new By();

List<DateTime> times = new List<DateTime>();
Video video = new Video();
Player player = new Player();

b.Add(times);
b.Add(video);
b.Add(plater);
byte[] MY_BUFFER = b.Buffer;

Reader Example

using Byter;

By b = new By(MY_BUFFER);

List<DateTime> times = b.Get<List<DateTime>>();
Video video = b.Get<video>();
Player player = b.Get<Player>();

if (b.IsValid == false)
{
     // error on parser
}

Encoding

using Byter;

string a = "ABC";
byte[] b = a.GetBytes(); // UTF8 is default;
byte[] c = a.GetBytes(Encoding.UTF32);
using Byter;

byte[] a =  new byte[] { 1, 2, 3, 4, 5, 6 };
byte[] b = a.GetString(); // UTF8 is default;
byte[] c = a.GetString(Encoding.UTF32);
// update default from utf8 to utf32 encoding
StringExtension.Default = Encoding.UTF32;

// string props
string example = "aLe";

string a = example.ToCapitalize(); // output: Ale
string a = example.ToLowerCase(); // output: ale
string a = example.ToUpperCase(); // output: ALE

Template

public class Video
{
    public string name;
    public int Likes;
    public DateTime UploadTime;
    public string[] Tags;
    public Metadata Meta;
}

public class Metadata
{
     public byte[] buffer;
     public Vector3 Something;
}

public class Player
{
   public long Id;
   public Vector3 position;
   public Quaternion rotation;
   public Vector3 scale;
   public bool running;
   puublic int HP;
}

Supported Types

// 1 byte
Null = TypesPrefix + 0,
Sbyte = TypesPrefix + 1,
Byte = TypesPrefix + 2,
Bool = TypesPrefix + 3,

// 2 bytes
Char = TypesPrefix + 4,
Short = TypesPrefix + 5,
Ushort = TypesPrefix + 6,

// 4 bytes
Uint = TypesPrefix + 7,
Int = TypesPrefix + 8,
Float = TypesPrefix + 9,
Enum = TypesPrefix + 10,

// 8 bytes
Long = TypesPrefix + 11,
Ulong = TypesPrefix + 12,
Double = TypesPrefix + 13,

// 16 bytes
Decimal = TypesPrefix + 14,

// dynamic
Bytes = TypesPrefix + 15,
String = TypesPrefix + 16,
BigInteger = TypesPrefix + 18,

// Symbols
Class = TypesPrefix + 19,
Struct = TypesPrefix + 20,
Array = TypesPrefix + 21,
DateTime = TypesPrefix + 22,
List = TypesPrefix + 23,
KFKMan commented 2 months ago

it's looks very good. You can add Int names versions of Short, Long etc.

alec1o commented 2 months ago

Yes sir! like code Below, write and read int, short, long... Encapsulated in class it work too.

By b = new By();

b.Add((int)100);
b.Add((long)100);
b.Add((short)400);
b.Add((decimal)500);

int i = b.Get<int>();
long l = b.Get<long>();
....

bool sucess = b.IsValid;

Now don't have logs or error handles. Just (IsValid) it return false if some data is not get.


My recommendation you also can use dynamic read technical, I'll documents is soon. Supported with all Byter version, Looks like:

v1, v2 example:

Reader r = new Reader (<buffer>);

int v = r.Read<int>();
MyEnum t = (MyEnum)v;

if (t == MyEnum.ChatBuffer)
{
    string messageId = r.Read<string>();
    ....
}
else if (t == MyEnum.PositionBuffer)
{
    int playerId = r.Read<int>();
    ....
}

v3 will use By instead of Writer/Reader, but Writer and Reader will removed because each use different prefix and protocol, e.g Writer buffer isn't compatible with By.Get buffer.

By  r = new By (<buffer>);

int v = r.Get<int>();
MyEnum t = (MyEnum)v;

if (t == MyEnum.ChatBuffer)
{
    string messageId = r.Get<string>();
    ....
}
else if (t == MyEnum.PositionBuffer)
{
    int playerId = r.Get<int>();
    ....
}

Thankful for feedback in version 4 I'll implement your feature, it'll look like code Below


b.OnSucess(Type t, string message) =>
{
    // message: sucess in read <long> type. Buffer Index <32>. current element <4>
});

b.OnError((Type t, string message) =>
{
    // message: error in read <long> type. Buffer Index <32>. past element <3>
});