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

[Question] Using System.Text.Json.JsonSerializer.SerializeToUtf8Bytes #45

Closed
adrianwright109 opened this issue May 14, 2021 · 3 comments
Labels
Question Question about this project Resolved: Answered The question raised by the author has been answered

Comments

@adrianwright109
Copy link

adrianwright109 commented May 14, 2021

Hi,

I have a list of complex objects that are rendered in a Telerik Blazor Grid. When I select items in the grid the grid's OnItemSelected event sends anIEnumerable<T> of all the selected items in the grid.

I can save this to a property in the Blazor component.

What I want to be able to do is persist that data so that if a user navigates away from the page that has the grid and then returns back I can populate the grid and select the items in the grid that were selected before.

I thought I would try and use System.Text.Json.JsonSerializer.SerializeToUtf8Bytes as it's suppose to be quicker than storing as a JSON string and therefore I'm also assuming it might take up less storage space in the browser's session storage.

Would I be able to use System.Text.Json.JsonSerializer.SerializeToUtf8Bytes to create a byte[] then store and retrieve that byte[] array like this:

// Local page property with selected grid items
public IEnumerable<ComplexObj> SelectedItems { get; set; }

// Convert selected items of type ComplexObj to
var jsonUtf8Bytes = JsonSerializer.SerializeToUtf8Bytes<IEnumerable<ComplexObj>>(SelectedItems, new JsonSerializerOptions());

// Store byte array to browser session storage
await sessionStorage.SetItemAsync("selectedItems", jsonUtf8Bytes);

// Retrieve byte array from session storage
var jsonUtf8Bytes = await sessionStorage.GetItemAsync<byte[]>("selectedItems");

// Convert byte array to readonly span of bytes
var readOnlySpan = new ReadOnlySpan<byte>(jsonUtf8Bytes);

// DeSerialize span back to list of ComplexObj type
var obj = JsonSerializer.Deserialize<IEnumerable<ComplexObj>>(readOnlySpan);
@adrianwright109 adrianwright109 added Question Question about this project Triage Issue needs to be triaged labels May 14, 2021
@chrissainty
Copy link
Member

Hi @adrianwright109. The browsers session storage is only capable of storing string values. So that is the only thing you need to factor in here. I'm curious if you've hit any speed issues with the default serialization or if you're just expecting them?

@adrianwright109
Copy link
Author

I'm more worried about exceeding the browser's session storage limit as these Telerik grids can have up to 10,000 rows in them. Potentially 10,000 rows could be selected and serializing a list of that many into a JSON string could exceed the limit. Unfortunately the grid needs the full objects to set the selected items, not just an ID.

Other option which I might have to do is store a unique ID into session storage then retrieve them from session storage and the use them to query the database to build up the list of selected items from the IDs.

@chrissainty
Copy link
Member

I see what you're saying. Session storage will hold up to around 5MB. I think storing the ID's with a call back to the DB is probably a more robust option.

As there isn't anything to do here I'm going to close this issue.

@chrissainty chrissainty added Resolved: Answered The question raised by the author has been answered and removed Triage Issue needs to be triaged labels May 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Question about this project Resolved: Answered The question raised by the author has been answered
Projects
None yet
Development

No branches or pull requests

2 participants