Skip to content

SQLite: align JSON and M.D.Sqlite representations #36749

Closed

Description

@roji

We seem to have some discrepancies between the string representation produced by our client-side JSON infrastructure (JsonValueReaderWriter), and the textual representation that M.D.Sqlite serializes to. For example, it seems like our JSON representation preserves zeros (the database contains 12:30:45.0000000), while Microsoft.Data.SQLite does not (we send a parameter with 12:30:45).

See TimeOnlyTypeTest.Query_property_within_json, ByteArrayTypeTest.Query_property_within_json. Also consider increasing type test coverage for any other supported types to make sure this is covered.

Activity

  1. added this to the Backlog milestone on Sep 11, 2025
  2. added theissue type on Sep 11, 2025
  3. krisbiradar commented on Sep 13, 2025

    @krisbiradar
    Contributor

    Hi @roji i can take this up.

  4. roji commented on Sep 15, 2025

    @roji
    MemberAuthor

    @krisbiradar great, but please post a clear plan of the findings and changes before submitting a PR.

  5. krisbiradar commented on Sep 15, 2025

    @krisbiradar
    Contributor

    Sure

  6. krisbiradar commented on Sep 19, 2025

    @krisbiradar
    Contributor

    Hi, @roji — I think I figured it out.

    If we look at the JsonTimeOnlyReaderWriter:

    // JsonTimeOnlyReaderWriter.cs
    writer.WriteStringValue(value.ToString("o", CultureInfo.InvariantCulture));

    we can see that TimeOnly is converted to string using the round-trip format specifier ("o"),
    so it preserves full precision irrespective of whether or not it has any milliseconds.

    Whereas in the StringTimeOnlyConverter:

    // StringTimeOnlyConverter.cs
    protected static new Expression<Func<TimeOnly, string>> ToString()
        => v => v.Ticks % 10000000 == 0
            ? string.Format(CultureInfo.InvariantCulture, @"{0:HH\:mm\:ss}", v)
            : v.ToString("o");

    precision is preserved only if the ticks are not divisible by 10,000,000 (i.e., when there are fractional seconds).

  7. roji commented on Sep 19, 2025

    @roji
    MemberAuthor

    Right, that's pretty much what I wrote in the original OP (note also that this is only about TimeOnly, but we're also aware of a problem with byte array).

  8. krisbiradar commented on Sep 19, 2025

    @krisbiradar
    Contributor

    Got you , will check the byte array one and submit a pr containing the fixes and tests so as to validate this issue going further.

  9. krisbiradar commented on Sep 20, 2025

    @krisbiradar
    Contributor

    I dug a little deeper here and wanted to share my findings regarding how EF Core handles serialization for JSON columns, specifically for byte[] and TimeOnly types when using the SQLite provider.


    byte[]

    • For byte[], I confirmed that SqliteJsonByteArrayReaderWriter is being correctly passed as the jsonValueReaderWriter.
    • This seems correct, as it implements the provider-specific logic (hex encoding/decoding) expected by SQLite.
    • Conclusion: The mapping for byte[] appears to be set up correctly.

    TimeOnly

    • For TimeOnly, however, the default JsonTimeOnlyReaderWriter is used.
    • This implementation does not account for milliseconds when serializing/deserializing, which could potentially lead to subtle data loss or comparison mismatches.
    • Conclusion: There may be an opportunity to improve or override the default TimeOnly handling for SQLite, especially if milliseconds precision is important.

    looks like the byte[] issue is because of something or i might be missing something

    thoughts ?

  10. roji commented on Sep 21, 2025

    @roji
    MemberAuthor

    @krisbiradar I don't really have time to spend time on this issue right now (if I spend time investigating it I might as well just fix it). What I do know that is that we have a failing test - ByteArrayTypeTest.Query_property_within_json - which indicates a problem that needs to be fixed.

  11. krisbiradar commented on Sep 21, 2025

    @krisbiradar
    Contributor

    I found the issue with byte [] as well , the issue is despite having SqliteJsonByteArrayReaderWriter only the ToJsonTyped Method is getting called which correctly saves the values in the database but while reading it doesnt call FromJsonTyped due to which the byte array now has more elements that we actually saved and that too numerical ascii value of the hex string characters

    will go deeper in this and come up with a solution as well as a PR

  12. krisbiradar commented on Sep 29, 2025

    @krisbiradar
    Contributor

    So upon further investigation i found out the FromJsonTyped is indeed getting called when we try to query the data with non json column filter or with no filter at all and when we select a full entity. The same doesn't work when we filter data with a json column check. i tried passing a global converter to the SqliteByteArrayTypeMapping that converted byte array to hex and back which seemed to pass the failing test you mentioned but it led to an issue that even non json columns were using it so now i am onto finding the correct spot to put the converter into which i should be done with in a couple of days
    please correct if i am wrong here.

  13. martijnremmen commented on Sep 29, 2025

    @martijnremmen

    I'm running into this issue when querying on a TimeOnly column where the TimeOnly value is in a list .Where(x => y.Contains(x.SomeTime))

    When looking at the DbCommand debug it seems to convert the TimeOnly[] to [Parameters=[@__filters_SomeTimeOnlyFilter_0='["15:00:00.0000000"]'. This ofcourse doesn't match with the Sqlite Text column with value "15:00:00". So I don't get any results, when swapping to Sql Server this works fine.

  14. krisbiradar commented on Oct 5, 2025

    @krisbiradar
    Contributor

    I'm running into this issue when querying on a TimeOnly column where the TimeOnly value is in a list .Where(x => y.Contains(x.SomeTime))

    When looking at the DbCommand debug it seems to convert the TimeOnly[] to [Parameters=[@__filters_SomeTimeOnlyFilter_0='["15:00:00.0000000"]'. This ofcourse doesn't match with the Sqlite Text column with value "15:00:00". So I don't get any results, when swapping to Sql Server this works fine.

    This is for json columns or non json columns as well ? because when i tried to do it worked fine in non json columns and will put in a pr soon to fix it with json columns along with similar issue for other datatypes.

  15. krisbiradar commented on Oct 10, 2025

    @krisbiradar
    Contributor

    Hi @roji 👋, I got this!

    Summary of Fixes

    1. JsonValueReaderWriter for TimeOnly

    As you mentioned, the first issue was with the JsonValueReaderWriter for TimeOnly in SQLite.
    I added a custom SqliteJsonValueReaderWriter that truncates the milliseconds in the string representation when there are no milliseconds present.

    2. SQLite Parameter Binding for byte[]

    The second issue was related to SQLite parameter binding.
    When querying data from a JSON column (i.e., a text column) for a field of type byte[], the parameter was sent as a BLOB instead of TEXT.

    I believe the issue was due to how SQLite compares blobs (using the 0x notation even in string form).
    To fix this:

    • Implemented the FindMapping method to detect if the column is a JSON column.
    • If it is a JSON column and the parameter is a byte[], I set the SQLite type to TEXT (it was previously null for byte[]).
    • Then, in the SqliteValueBinder, I called bind_text instead of bind_blob.

    3. Updated Tests

    Lastly, I updated the test cases so that the new JSON representations are validated instead of the old ones.
    After updating multiple test JSON representations and running the full project test suite — ✅ everything worked fine.

  16. modified the milestones: Backlog, 12.0.0 on Sep 9, 2026
  17. added a commit that references this issue on Sep 10, 2026
    1c6471a
  18. added a commit that references this issue on Sep 25, 2026
    dc5f936
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions