Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DateOnly Json Converter #6667

Closed
totpero opened this issue Mar 3, 2023 · 4 comments
Closed

Add DateOnly Json Converter #6667

totpero opened this issue Mar 3, 2023 · 4 comments
Milestone

Comments

@totpero
Copy link
Contributor

totpero commented Mar 3, 2023

Related to this #6643 issue, the same is if you have DateOnly property in BackEnd and you want to send Date from FrontEnd.
You need to add also DateOnlyJsonConverter like this:

public class DateOnlyJsonConverter : JsonConverter<DateOnly>
{
    public override void WriteJson
        (JsonWriter writer, DateOnly value, JsonSerializer serializer)
    {
        writer.WriteValue(value.ToString("O"));
    }

    public override DateOnly ReadJson
    (JsonReader reader, Type objectType, DateOnly existingValue,
        bool hasExistingValue, JsonSerializer serializer)
    {
        var v = reader.Value;
        if (v == null)
        {
            return DateOnly.MinValue;
        }

        var vType = v.GetType();
        if (vType == typeof(DateTimeOffset)) //when the object is from a property 
            //in POST body. When used in service, 
            //better to have 
            //options.SerializerSettings.DateParseHandling = 
            //Newtonsoft.Json.DateParseHandling.DateTimeOffset;
        {
            return DateOnly.FromDateTime(((DateTimeOffset)v).DateTime);
        }

        if (vType == typeof(string))
        {
            return DateOnly.Parse((string)v); //DateOnly can parse 00001-01-01
        }

        if (vType == typeof(DateTime)) //when the object is from a property 
            //in POST body from a TS client
        {
            return DateOnly.FromDateTime((DateTime)v);
        }

        throw new NotSupportedException
            ($"Not yet support {vType} in {this.GetType()}.");
    }
}
@ismcagdas
Copy link
Member

Thanks @totpero if you want, you can send a PR because this is your code :)

@ismcagdas ismcagdas added this to the v8.2 milestone Mar 3, 2023
@totpero
Copy link
Contributor Author

totpero commented Mar 3, 2023

Hi @ismcagdas,
I have create PR white this feature,
I name this converter DateOnlyJsonConverter because I also use DateOnly EntityFramework ValueConverter to map and store DateOnly properties to database column and I name this convertor: DateOnlyConverter.
Check if is ok my PR.

Nice job with aspboilerplate and abp projects 👍
Thanks

@ismcagdas
Copy link
Member

Thank you very much 🙏

@ismcagdas
Copy link
Member

resolved via #6669

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants