You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SQLite: align JSON and M.D.Sqlite representations #36749
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.
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.csprotectedstaticnewExpression<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).
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).
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
@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.
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
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.
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.
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.
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.
Activity
krisbiradar commentedon Sep 13, 2025
Hi @roji i can take this up.
roji commentedon Sep 15, 2025
@krisbiradar great, but please post a clear plan of the findings and changes before submitting a PR.
krisbiradar commentedon Sep 15, 2025
Sure
krisbiradar commentedon Sep 19, 2025
Hi, @roji — I think I figured it out.
If we look at the
JsonTimeOnlyReaderWriter:we can see that
TimeOnlyis 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:precision is preserved only if the ticks are not divisible by 10,000,000 (i.e., when there are fractional seconds).
roji commentedon Sep 19, 2025
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).
krisbiradar commentedon Sep 19, 2025
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.
krisbiradar commentedon Sep 20, 2025
I dug a little deeper here and wanted to share my findings regarding how EF Core handles serialization for JSON columns, specifically for
byte[]andTimeOnlytypes when using the SQLite provider.byte[]byte[], I confirmed thatSqliteJsonByteArrayReaderWriteris being correctly passed as thejsonValueReaderWriter.byte[]appears to be set up correctly.TimeOnlyTimeOnly, however, the defaultJsonTimeOnlyReaderWriteris used.TimeOnlyhandling for SQLite, especially if milliseconds precision is important.looks like the byte[] issue is because of something or i might be missing something
thoughts ?
roji commentedon Sep 21, 2025
@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.
krisbiradar commentedon Sep 21, 2025
I found the issue with byte [] as well , the issue is despite having
SqliteJsonByteArrayReaderWriteronly theToJsonTypedMethod is getting called which correctly saves the values in the database but while reading it doesnt callFromJsonTypeddue to which the byte array now has more elements that we actually saved and that too numerical ascii value of the hex string characterswill go deeper in this and come up with a solution as well as a PR
krisbiradar commentedon Sep 29, 2025
So upon further investigation i found out the
FromJsonTypedis 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 theSqliteByteArrayTypeMappingthat 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 daysplease correct if i am wrong here.
martijnremmen commentedon Sep 29, 2025
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.krisbiradar commentedon Oct 5, 2025
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.
fix:Align JSON and M.D.Sqlite representations , dotnet#36749
krisbiradar commentedon Oct 10, 2025
Hi @roji 👋, I got this!
Summary of Fixes
1.
JsonValueReaderWriterforTimeOnlyAs you mentioned, the first issue was with the
JsonValueReaderWriterforTimeOnlyin SQLite.I added a custom
SqliteJsonValueReaderWriterthat 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
0xnotation even in string form).To fix this:
FindMappingmethod to detect if the column is a JSON column.byte[], I set the SQLite type to TEXT (it was previouslynullforbyte[]).SqliteValueBinder, I calledbind_textinstead ofbind_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.
fix:Align JSON and M.D.Sqlite representations , dotnet#36749
Align JSON and M.D.Sqlite representations (dotnet#36951)
Align JSON and M.D.Sqlite representations (#36951)