Skip to content

Generic cookies #22

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

Merged
merged 3 commits into from
Mar 3, 2025
Merged

Generic cookies #22

merged 3 commits into from
Mar 3, 2025

Conversation

YuriyDurov
Copy link
Member

@YuriyDurov YuriyDurov commented Mar 3, 2025

Allows setting and retrieving generic cookies, automatically serializing and deserializing their respective values as needed. E.g.

public class MyClass
{
    public int MyValue { get; set; }
}

// new Cookie<T> class usage examples
public void ExampleCookieUsage()
{
    var data = new MyClass
    {
        MyValue = 1
    };

    var cookie = Cookie.FromValue("key", data);
    // or
    var cookie = new Cookie<MyClass>("key", data);
    // or
    var stringValue = JsonSerializer.Serialize(data);
    var cookie = new Cookie("key", stringValue).Cast<MyData>();

    // this cookie can then be persisted using ICookieService
}

// New method allows for automatic value deserialization when retrieving a cookie
public void ExampleServiceUsage(ICookieService cookieService)
{
    Cookie<MyData> myCookie = cookieService.Get<MyData>("key");
}

@YuriyDurov YuriyDurov requested review from N31TH3R and ligowsky March 3, 2025 16:02
@YuriyDurov YuriyDurov merged commit 378f924 into main Mar 3, 2025
@YuriyDurov YuriyDurov deleted the generic-cookies branch March 3, 2025 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants