-
-
Notifications
You must be signed in to change notification settings - Fork 117
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
Serialize class to local storage #71
Comments
You don't need to serialise the object, this library does that for you. The reason you're seeing the error is because your object has been serialised twice but only deserialised once. |
If I write the class directly, all it stores is {} for the item, and no exception is thrown. |
Yes. Does that answer your question? |
So the SetItemAsync can't write a class ? |
I'm sorry I'm not following you. |
Here's a test:
The result: I know that I don't need to do this, but it proves that the class is intact |
Thanks for clarifying this issue. I've just run your code and replicated what you're seeing. If I change the fields in your class to properties, everything works as expected. I believe you're running Newtonsoft.Json to do your manual serialization. We use the new System.Text.Json library and unfortunately right now it doesn't support serialisation of fields (dotnet/runtime#876). |
Yes, I have it working now as well, also with a List<> of classes inside the class which is also what I am needing to use:
My original test was using Newtonsoft.Json. Thankyou for helping with this issue. |
Glad we got it sorted 🙂 |
I'm having the same problem. I literally want the string back from local storage. I'm not sure why it can't do that since what it stored was a string. Thoughts? string serialized_appState; {"User":{"UserID":"9b02-431f-ac46-cf116fa90cf9","UserName":"","FullName":"Bob D","IsAuthenticated":true,"AuthenticatedDateTime":"2020-05-06T21:08:19.6653581+00:00","CompanyName":"Production","TenantName":"PROD","IsActive":true,"Initials":"BD","Phone":"","IsSystemAdmin":true,"IsSystemDeveloper":true,"IsTenantAdmin":true,"ConnectionString":"","Token":"7de34799-9b26-47bb-9353-185a7ffeda0c"},"PageLoadTime":"0.304"} I'm not sure what about this value cannot be returned from localStorage as a string. Bob |
From what I found the Set/Get operations find the "{" as part of the first character as an indicator that it is to be a json string so has to be deserilaized into json. You are best to try to read/write strings as a basic test. NOTE: |
This is great information. At one time this worked. I'm not sure when it stopped working because the app still functions without it. Or am I imagining that? Did it work with { at one time? |
I could not get it to work when the string had a { in it, but I am not sure if that was my class definition or not. |
There has got to be a way to get a string value out of it even if it is a JSON string. I'm trying a few things without luck. I can't even believe it's an issue. Seems like something that should just work. |
If I figure it out I'll post it here. |
This seems to work but other things are breaking. I'll check that out. Just wanted to share that using object seems to help.
|
That did the trick. I can get the JSON string as object and when I'm ready to use it I can deserialize it into the class I need. The Blazored.LocalStorage has been a great tool. Thanks. |
@robertdunaway Can you please open a separate issue for your problem. |
I have a class I would like to store:
to store it, I need to serilaize to string:
await localStorage.SetItemAsync("mydata",JsonConvert.SerializeObject(mydata));
this is fine, it works ok.
but when I read it back as a string, I get an error
The JSON value could not be converted to System.String. Path: $ | LineNumber: 0 | BytePositionInLine: 1. address name
Is their anyway I can read the item back as a string and deserialize myself ?
The text was updated successfully, but these errors were encountered: