Skip to content

Commit 6434ecf

Browse files
committed
Add custom type serialization and versioning guide to README
1 parent e3ca525 commit 6434ecf

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ The purpose of SQLiteSharp is to provide improvements to the original library, w
1717

1818
## Example
1919

20+
First, declare your object with optional annotations:
2021
```cs
2122
public class ShopItem {
2223
[PrimaryKey, AutoIncrement]
@@ -32,6 +33,7 @@ public class ShopItem {
3233
}
3334
```
3435

36+
Second, open a connection to your database:
3537
```cs
3638
// Open a database connection
3739
using SQLiteConnection Connection = new("database.db");
@@ -62,4 +64,43 @@ Connection.Delete(Apple);
6264
// Find several items in the table
6365
List<ShopItem> Bananas = Connection.Table<ShopItem>().Where(ShopItem => ShopItem.ItemName == "Banana").ToList();
6466
Assert.Single(Bananas);
65-
```
67+
```
68+
69+
## Custom Type Serialization
70+
71+
SQLiteSharp supports serialization for a set of common types, but custom types must be registered.
72+
73+
Type serialization is polymorphic, so you can register `object` as a fallback for all missing types.
74+
75+
```cs
76+
public class SweetWrapper {
77+
public Sweet? Sweet { get; set; } // custom type
78+
}
79+
public class Sweet(string Flavour) {
80+
public string? Flavour { get; set; } = Flavour;
81+
}
82+
```
83+
84+
```cs
85+
// Open a database connection
86+
using SQLiteConnection Connection = new(":memory:");
87+
88+
// Register custom type
89+
Connection.Orm.RegisterType<Sweet>(
90+
SqliteType.Text,
91+
serialize: (Sweet Sweet) => JsonSerializer.Serialize(Sweet),
92+
deserialize: (SqliteValue Value, Type ClrType) => JsonSerializer.Deserialize(Value.AsText, ClrType)
93+
);
94+
```
95+
96+
## Versioning Guide
97+
98+
SQLiteSharp uses versions like "1.0" and "2.4".
99+
100+
#### For developers:
101+
- Increment the major version when adding new features or making breaking changes.
102+
- Increment the minor version when fixing bugs or making small improvements.
103+
104+
#### For users:
105+
- You usually want the latest major version, although it may require some changes to your project.
106+
- You always want the latest minor version, and there should not be any issues upgrading.

SQLiteSharp.Tests/ReadMeTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public void Test2() {
4141
// Register custom type
4242
Connection.Orm.RegisterType<Sweet>(
4343
SqliteType.Text,
44-
serialize: (Sweet sweet) => System.Text.Json.JsonSerializer.Serialize(sweet),
45-
deserialize: (SqliteValue value, Type clrType) => System.Text.Json.JsonSerializer.Deserialize(value.AsText, clrType)
44+
serialize: (Sweet Sweet) => System.Text.Json.JsonSerializer.Serialize(Sweet),
45+
deserialize: (SqliteValue Value, Type ClrType) => System.Text.Json.JsonSerializer.Deserialize(Value.AsText, ClrType)
4646
);
4747

4848
// Create a table for a class

0 commit comments

Comments
 (0)