neuecc / Utf8Json

Definitely Fastest and Zero Allocation JSON Serializer for C#(NET, .NET Core, Unity, Xamarin).
MIT License
2.36k stars 267 forks source link

Is it possible to serialize long member as string? #248

Closed AachoLoya closed 3 years ago

AachoLoya commented 3 years ago

I have c# models with ID field defined as a long. Need to serialize the data into json, but since JavaScript doesn't support 64-bit values, I need to serialize only ID field as string, but in c# model keep it as long.

public class Profile { public long ID; public string name; public long userData; }

Anyway to mark the ID field so its serialized as string? Hoping there is an attribute that I can use.

AachoLoya commented 3 years ago

Does anyone know?

tomsoftware commented 3 years ago

May you could try to proxy your value for Json using a getter / setter

    public class Test
    {
        [IgnoreDataMember]
        public long Id { get; set; }

        [DataMember(Name = "Id")]
        public string IdAsString
        {
            get
            {
                return Id.ToString();
            }

            set
            {
                this.Id = long.Parse(value);
            }
        }
    }
AachoLoya commented 3 years ago

I think that's the best way to go about doing this.