Reputation: 6378
I'm looking for a binary serializer because in my app, the user can discover many items as they want. Imagine that the user has discover more than 100 items (these items was downloaded from internet) and when the app is suspended the app cannot find the last item because does not exist.
In the app happens this because I always load the first 10 items. But in metro principles said, the app needs to restore everything.. so I was thinking using Binary serializer to save this objects rapidly. But I can't find any class which can help me.
EDIT:
public abstract class BaseItem
{
...
public BaseGroup Group { get; set;}
}
public abstract class BaseGroup
{
public IEnumerable<BaseItem> Items { get; set; }
}
public sealed class FeedDataGroup
{
...
}
public sealed class FeedItem
{
...
}
I plan to serialize a ObservableCollection. If I use JSON, will there be any problem by the way I have structured my classes?
Upvotes: 5
Views: 4023
Reputation: 429
An option is SharpSerializer for binary serialization. Its usage is extremely simple.
Upvotes: 0
Reputation: 1
There is a Json serializer in the using System.Runtime.Serialization.Json namespace that serializes to JSON if this is what you wanted. To use the serializers you should mark any data that will be serialized as a datacontract
[DataContract]
public class Serializable
{
[DataMember]
Public string SerializableMember{get;set;}
Public string NonSerializedMember{get;set;}
}
You can now use the DataContractJsonSerializer to serialize the Object to a JSON stream. the JSON structure created will look something like this: {"SerializableMember" : {value of data} } there is no class information stored, but both member-names and values will be storred from serializable members of the class to the json Object. JSON is a bit more compact than xml, so it's better for large sets of data. I suspect that Microsoft did omit the Binary serializer from Metro since ARM processors can be both Big and little Endian and the programmer does not have control over this matter.
Upvotes: 0
Reputation: 707
Microsoft sample code uses DataContractSerializer
// Serialize the session state synchronously to avoid asynchronous access to shared
// state
MemoryStream sessionData = new MemoryStream();
DataContractSerializer serializer = new DataContractSerializer(typeof(Dictionary<string, object>), _knownTypes);
serializer.WriteObject(sessionData, _sessionState);
// Get an output stream for the SessionState file and write the state asynchronously
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(sessionStateFilename, CreationCollisionOption.ReplaceExisting);
using (Stream fileStream = await file.OpenStreamForWriteAsync())
{
sessionData.Seek(0, SeekOrigin.Begin);
await sessionData.CopyToAsync(fileStream);
await fileStream.FlushAsync();
}
Upvotes: 6
Reputation: 1813
There's the BinaryFormatter class in System.Runtime.Serialization that does what you describe. I don't have experience specifically with WinRT, but the class is available in .Net 4.5, so believe you could make use of it.
The criticism below is fair: Not only did I omit the word "core" after .Net 4.5 above (changing the meaning of my message pretty severely), I'd based my comment on the fact that other members of System.Runtime.Serialization made it into .Net 4.5 Core. Looking at a list of differences between 4.5 and core, I see that the BinaryFormatter specifically is excluded.
Upvotes: 2