Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a binary serializer exist in WIN RT?

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?

like image 258
Darf Zon Avatar asked Jul 20 '12 18:07

Darf Zon


2 Answers

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();
        }
like image 179
Feng Yuan Avatar answered Nov 17 '22 04:11

Feng Yuan


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.

like image 2
Reacher Gilt Avatar answered Nov 17 '22 04:11

Reacher Gilt