How to save a List or a Collection on a NFC tag
As I am currently working again on my NFC app, I needed to save an ObservableCollection to a tag. My first attempts resulted in a heavily overlong string that I wasn’t able to save.
Anyways, after a short convo on Twitter, I went for the right way – serialize to JSON.
The first thing you’ll need for that is the JSON.NET library, which you can get here or via the NuGet package manager in Visual Studio.
After that, you will be able to save your List or ObservableCollection in a few easy steps.
One thing I need to recommend is to make the names in your class/viewmodel as short as possible. Here is my example:
1
2
3
4
5
6
7
public class ListItems
{
//ListItems
public string i { get; set; }
//isChecked
public bool iC { get; set; }
}
You will need every space you can get on your tag, so you really should go for a similar way like I did above.
After you are done with that, you only need one line of code to convert your List/Collection for writing on your tag:
1
var ListToSave = JsonConvert.SerializeObject(ItemsList);
Now you will be able to save it as a Text record or whatever record type you need to save it to.
Deserializing the JSON string works also with only one line of code:
1
ItemsList = JsonConvert.DeserializeObject<ObservableCollection<ListItems>>(StringFromYourTag);
Then you set the ItemsSource of your ListBox to that (or whatever else the List/Collection is for).
I my case, I was able to save a Collection of 25 items to my tag (writable size 716 bytes) with having still about 200 bytes left in this way.
As always, I hope this will be helpful for some of you.
Happy coding!