alec1o / Byter

Byter: C# library for serializing and deserializing data to and from bytes, supporting unlimited complexity and depth.⚡
MIT License
6 stars 2 forks source link

Big struct is null when serialize #29

Open nvh2001 opened 3 months ago

nvh2001 commented 3 months ago

Test: Unity .NET Standartd 2.1 & .NET 8 Console

 var playerSessionModel = JsonConvert.DeserializeObject<PlayerSessionModel>("{\"statePlayer\":1,\"name\":\"vanhaodev\",\"id\":\"6683564e64fb47b6286e10a2\",\"point\":20,\"webUserInfo\":{\"phone\":\"null\",\"_id\":\"6683564e64fb47b6286e10a2\",\"name\":\"Baby Monster\",\"email\":\"vanhao.dev@gmail.com\",\"username\":\"vanhaodev\",\"telegram_id\":\"\",\"avatar\":{\"_id\":\"66272f0450c3064eceb18fa3\",\"name\":\"avt 4\",\"key\":\"games/66272f0450c3064eceb18fa3/avt-4.png\",\"full_url\":\"https://ntada.s3.ap-southeast-2.amazonaws.com/games/66272f0450c3064eceb18fa3/avt-4.png\",\"id\":\"66272f0450c3064eceb18fa3\"},\"banner\":\"null\",\"is_active\":true,\"invite_code\":\"r2n42raxgy\",\"balance\":20,\"titles\":\"newbie\",\"wallets\":[],\"is_online\":true,\"is_email_verified\":false,\"is_phone_verified\":false,\"is_telegram_verified\":false,\"is_wallet_connect\":false,\"tickets\":0,\"usdt_balance\":0,\"last_online_at\":\"2024-07-04T07:16:23.3Z\",\"about\":\"\",\"is_use_password\":true,\"id\":\"6683564e64fb47b6286e10a2\",\"telegram_ref\":\"https://google.com\"}}");

        Debug.Log(JsonConvert.SerializeObject(playerSessionModel)); //WORK
        Primitive p = new Primitive();
        p.Add.Struct(playerSessionModel);

        Primitive p2 = new Primitive(p.GetBytes());
        var model = p2.Get.Struct<PlayerSessionModel>();

        Debug.Log(JsonConvert.SerializeObject(model)); //NULL

The structs

    public struct PlayerSessionModel
    {
        public TypeStatePlayer statePlayer { get; set; }
        public string name { get; set; }
        public string id { get; set; }
        public long point { get; set; }
        public WebUserInfo webUserInfo { get; set; }
    }

public struct WebUserInfo
{
    [JsonProperty("phone")]
    public string phone { get; set; }
    [JsonProperty("_id")]
    public string _id { get; set; }
    [JsonProperty("name")]
    public string name { get; set; }
    [JsonProperty("email")]
    public string email { get; set; }
    [JsonProperty("username")]
    public string username { get; set; }
    [JsonProperty("telegram_id")]
    public string telegram_id { get; set; }
    [JsonProperty("avatar")]
    public Avatar avatar { get; set; }
    [JsonProperty("banner")]
    public object banner { get; set; }
    [JsonProperty("is_active")]
    public bool is_active { get; set; }
    [JsonProperty("invite_code")]
    public string invite_code { get; set; }
    [JsonProperty("balance")]
    public long balance { get; set; }
    [JsonProperty("titles")]
    public string titles { get; set; }
    [JsonProperty("wallets")]
    public object wallets { get; set; }
    [JsonProperty("is_online")]
    public bool is_online { get; set; }
    [JsonProperty("is_email_verified")]
    public bool is_email_verified { get; set; }
    [JsonProperty("is_phone_verified")]
    public bool is_phone_verified { get; set; }
    [JsonProperty("is_telegram_verified")]
    public bool is_telegram_verified { get; set; }
    [JsonProperty("is_wallet_connect")]
    public bool is_wallet_connect { get; set; }
    [JsonProperty("tickets")]
    public int tickets { get; set; }
    [JsonProperty("usdt_balance")]
    public int usdt_balance { get; set; }
    [JsonProperty("last_online_at")]
    public DateTime last_online_at { get; set; }
    [JsonProperty("about")]
    public string about { get; set; }
    [JsonProperty("is_use_password")]
    public bool is_use_password { get; set; }
    [JsonProperty("id")]
    public string id { get; set; }
    [JsonProperty("telegram_ref")]
    public string telegram_ref { get; set; }
}

public struct Avatar
{
    [JsonProperty("_id")]public string _id { get; set; }
    [JsonProperty("name")] public string name { get; set; }
    [JsonProperty("key")]public string key { get; set; }
    [JsonProperty("full_url")]public string full_url { get; set; }
    [JsonProperty("id")]public string id { get; set; }
}

SEND

{
  "statePlayer": 1,
  "name": "vanhaodev",
  "id": "6683564e64fb47b6286e10a2",
  "point": 20,
  "webUserInfo": {
    "phone": "null",
    "_id": "6683564e64fb47b6286e10a2",
    "name": "Baby Monster",
    "email": "vanhao.dev@gmail.com",
    "username": "vanhaodev",
    "telegram_id": "",
    "avatar": {
      "_id": "66272f0450c3064eceb18fa3",
      "name": "avt 4",
      "key": "games/66272f0450c3064eceb18fa3/avt-4.png",
      "full_url": "https://ntada.s3.ap-southeast-2.amazonaws.com/games/66272f0450c3064eceb18fa3/avt-4.png",
      "id": "66272f0450c3064eceb18fa3"
    },
    "banner": "null",
    "is_active": true,
    "invite_code": "r2n42raxgy",
    "balance": 20,
    "titles": "newbie",
    "wallets": "null",
    "is_online": true,
    "is_email_verified": false,
    "is_phone_verified": false,
    "is_telegram_verified": false,
    "is_wallet_connect": false,
    "tickets": 0,
    "usdt_balance": 0,
    "last_online_at": "2024-07-04T07:16:23.3Z",
    "about": "",
    "is_use_password": true,
    "id": "6683564e64fb47b6286e10a2",
    "telegram_ref": "https://google.com"
  }
}

RECEIVE

{
  "statePlayer": 0,
  "name": null,
  "id": null,
  "point": 0,
  "webUserInfo": {
    "phone": null,
    "_id": null,
    "name": null,
    "email": null,
    "username": null,
    "telegram_id": null,
    "avatar": {
      "_id": null,
      "name": null,
      "key": null,
      "full_url": null,
      "id": null
    },
    "banner": null,
    "is_active": false,
    "invite_code": null,
    "balance": 0,
    "titles": null,
    "wallets": null,
    "is_online": false,
    "is_email_verified": false,
    "is_phone_verified": false,
    "is_telegram_verified": false,
    "is_wallet_connect": false,
    "tickets": 0,
    "usdt_balance": 0,
    "last_online_at": "0001-01-01T00:00:00",
    "about": null,
    "is_use_password": false,
    "id": null,
    "telegram_ref": null
  }
}

My deadline is killing me haha

alec1o commented 3 months ago

Chill I'll respond you again at 1h or less.

nvh2001 commented 3 months ago

More issue: If list of struct is length = 0 (not null), will error when deserialize

 Idol idol = new Idol();
 idol.name = "Karina";
 idol.birthdayYear = 2000;
 idol.group = new Group
 {
     name = "aespa",
     birthdayYear = 2019,
     songs = new List<Song>()
     {
         //new Song{name = "Supernova", birthdayYear = 2024},
         //new Song{name = "Armageddon", birthdayYear = 2024}
     }
 };

 Idol[] list = new Idol[1];
 list[0] = (idol);
 Primitive send = new Primitive();
 send.Add.Array<Idol>(list);

 Primitive recive = new Primitive(send.GetBytes());
 var reIdol = recive.Get.Array<Idol>();

 Debug.Log(reIdol[0].name);
 Debug.Log(reIdol[0].birthdayYear);
 Debug.Log(reIdol[0].group.name);
 Debug.Log(reIdol[0].group.birthdayYear);

 //foreach (var song in reIdol[0].group.songs)
 //{
 //    Debug.Log(song.name + " | " + song.birthdayYear);
 //}
alec1o commented 3 months ago

More issue: If list of struct is length = 0 (not null), will error when deserialize

true, Primitive don't allow write null information

nvh2001 commented 3 months ago

To avoid error On Byter, my funny way, huhu i crying

public async Task<PlayerSessionModel> OnGetUserInfo(string accessToken)
{
    PlayerSessionModel pModel = new PlayerSessionModel();
    var url = ServerURL.GetUrlByTypeGetRequest(TypeGetRequest.UserInfo);
    var result = await ServerAPI.GetAPI<WebUserInfo>(url, accessToken);
    if (result.Item2)
    {
        var webUserInfo = result.Item1;
        if (webUserInfo.id != null)
        {
            pModel = new PlayerSessionModel
            {
                statePlayer = TypeStatePlayer.Idle,
                id = webUserInfo.id,
                name = webUserInfo.username,
                point = webUserInfo.balance,
                webUserInfo = new WebUserInfo
                {
                    _id = webUserInfo._id ?? "null",
                    phone = webUserInfo.phone ?? "null",
                    name = webUserInfo.name ?? "null",
                    email = webUserInfo.email ?? "null",
                    username = webUserInfo.username ?? "null",
                    telegram_id = webUserInfo.telegram_id ?? "null",
                    avatar = new Avatar
                    {
                        _id = webUserInfo.avatar._id ?? "null",
                        name = webUserInfo.avatar.name ?? "null",
                        key = webUserInfo.avatar.key ?? "null",
                        full_url = webUserInfo.avatar.full_url ?? "null",
                        id = webUserInfo.avatar.id ?? "null",
                    },
                    banner = webUserInfo.banner ?? "null",
                    is_active = webUserInfo.is_active,
                    invite_code = webUserInfo.invite_code ?? "null",
                    balance = webUserInfo.balance,
                    titles = webUserInfo.titles ?? "null",
                    wallets = webUserInfo.wallets ?? "null",
                    is_online = webUserInfo.is_online,
                    is_email_verified = webUserInfo.is_email_verified,
                    is_phone_verified = webUserInfo.is_phone_verified,
                    is_telegram_verified = webUserInfo.is_telegram_verified,
                    is_wallet_connect = webUserInfo.is_wallet_connect,
                    tickets = webUserInfo.tickets,
                    usdt_balance = webUserInfo.usdt_balance,
                    last_online_at = webUserInfo.last_online_at,
                    about = webUserInfo.about ?? "null",
                    is_use_password = webUserInfo.is_use_password,
                    id = webUserInfo.id ?? "null",
                    telegram_ref = webUserInfo.telegram_ref ?? "null"
                }
            };
        }
    }
    return pModel;
}
alec1o commented 3 months ago

To avoid error On Byter, my funny way, huhu i crying

Do you fixed?

nvh2001 commented 3 months ago

More issue: If list of struct is length = 0 (not null), will error when deserialize

true, Primitive don't allow write null information

To avoid error On Byter, my funny way, huhu i crying

Do you fixed?

no, it's just set for default custom for null but still error

alec1o commented 3 months ago

Ok, Tell me what result do you expect

alec1o commented 3 months ago

what is TypeStatePlayer object, can send me?

nvh2001 commented 3 months ago

Ok, Tell me what result do you expect

Can byter send null value?

nvh2001 commented 3 months ago

what is TypeStatePlayer object, can send me?

It's Enum

public enum TypeStatePlayer
{
    None,
    Idle,
    Ready,
    Play,
}
alec1o commented 3 months ago

https://github.com/alec1o/Byter/issues/28#issuecomment-2210260632 WebUserInfo->wallet, WebUserInfo->banner, is a object! I told you before Primitive not support object will make error đŸ¤Ŗ

alec1o commented 3 months ago

Can byter send null value?

Do you mean I implement a way to serialize and deserialize null objects??? đŸ¤¯đŸ˜¨

nvh2001 commented 3 months ago

Can byter send null value?

Do you mean I implement a way to serialize and deserialize null objects??? đŸ¤¯đŸ˜¨

yep, because my struct request from api web so it have somany null field, and sometime i create struct and just use only a few internal variables to achieve the goal

alec1o commented 3 months ago

yep, because my struct request from api web so it have somany null field, and sometime i create struct and just use only a few internal variables to achieve the goal

😭 😭 😭 😭 😭 😭 😭 😭 😭 😭 😭 😭 😭

nvh2001 commented 3 months ago

yep, because my struct request from api web so it have somany null field, and sometime i create struct and just use only a few internal variables to achieve the goal

😭 😭 😭 😭 😭 😭 😭 😭 😭 😭 😭 😭 😭

If it's difficult then skip it, but lists and arrays should be able to fix it

alec1o commented 3 months ago

I'll implement it, To allow any null data. But have a big problem about the way.

How Skip null value in Primitive.Add?

  1. Use null suffix/prefix, (Bug when object value contain same value then prefix/suffix). ❓ (Conflict)
  2. Update overhead of Primitive from 1 byte to bytes. ❓ (Force incress data)
  3. ❓

Have a idea smart boys? (Nguyen Van Hao: @nvh2001 @vanhaodev), (Alecio Furanze: @alec1o)

alec1o commented 3 months ago

I'll implement it, To allow any null data. But have a big problem about the way.

How Skip null value in Primitive.Add?

  1. Use null suffix/prefix, (Bug when object value contain same value then prefix/suffix). ❓ (Conflict)
  2. Update overhead of Primitive from 1 byte to bytes. ❓ (Force incress data)
  3. ❓

Have a idea smart boys? (Nguyen Van Hao: @nvh2001 @vanhaodev), (Alecio Furanze: @alec1o)

Do you have a ideia about?

alec1o commented 3 months ago

Bro can you test if null object is skipped by primitive. (Use current updated sources) from main branch

alec1o commented 3 months ago

It's working?

nvh2001 commented 3 months ago

It's working?

sorry i have some problem in my life, in today, i'll check this

alec1o commented 3 months ago

I'm waiting for you feedback

alec1o commented 3 months ago

Hi @nvh2001 @vanhaodev

alec1o commented 3 months ago

Can you give me a review? ☚ī¸đŸ˜­

alec1o commented 3 months ago

😭 bro!!! Talk something, are you okay? @nvh2001 @vanhaodev

alec1o commented 3 months ago

☕ Hello! @nvh2001 @vanhaodev

vanhaodev commented 4 weeks ago

☕ Hello! @nvh2001 @vanhaodev

sorry, I have some issue in my life. Now im here to continue my project.

alec1o commented 4 weeks ago

Good! I was worried about you a lot. Welcome.

alec1o commented 2 weeks ago

Hey @nvh2001 @vanhaodev Did you test it? I'm waiting for you feedback update it to v4