locked
How to serialize a class in C++/CX by using DataContractSerializer or other ways?

    Question

  • Hi, I am using C++/CX to develop a window APP. But here comes a serialization question.

    With C# we can use the DataContractSerializer easily to serialize a class, for emaple:

    [DataContract]
    public sealed class test : Itest
    {
     [DataMember]
     public int NumOne{ get; set; }
     [DataMember]
     public int NumTwo{ get; set; }
    }

    However, DataContractSerializer understands only .NET types and the built-in types such as int.

    So what can I do with C++/CX???

    Saturday, July 12, 2014 1:46 PM

Answers

  • Hi,

    See if Windows::Data::Json would work for your needs. The Push and periodic notifications client-side sample demonstrates the use in Helper.cpp.

    And There aren't any serialization specific API for C++ in Windows Store apps.

    If you are looking for a .NET style solution, you may be want to check out Boost's Serialization solution.

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    Monday, July 14, 2014 1:31 AM

All replies

  • Hi,

    See if Windows::Data::Json would work for your needs. The Push and periodic notifications client-side sample demonstrates the use in Helper.cpp.

    And There aren't any serialization specific API for C++ in Windows Store apps.

    If you are looking for a .NET style solution, you may be want to check out Boost's Serialization solution.

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    Monday, July 14, 2014 1:31 AM
  • How about use  DataContractSerializer in a C# runtime component and return the data you need, then reference this project in a C++/CX project.

    In addition, when we create new BasicPage.xaml page, we can see that the VS will add other relative files(SuspensionManager.cs,NavigationHelper.cs...). In the SuspensionManager.cs, it use DataContractSerializer to serialize _kownTypes in SaveAsync method. Following this way, we can also create new BasicPage in C++/CX project. Then find SaveAsync method. It may be helpful for you.

    task<void> SuspensionManager::SaveAsync(void)
    {
    	// Save the navigation state for all registered frames
    	for (auto && weakFrame : _registeredFrames)
    	{
    		auto frame = weakFrame.Resolve<Frame>();
    		if (frame != nullptr) SaveFrameNavigationState(frame);
    	}
    
    	// Serialize the session state synchronously to avoid asynchronous access to shared
    	// state
    	auto sessionData = ref new InMemoryRandomAccessStream();
    	auto sessionDataWriter = ref new DataWriter(sessionData->GetOutputStreamAt(0));
    	ObjectSerializeHelper::WriteObject(sessionDataWriter, _sessionState);
    
    	// Once session state has been captured synchronously, begin the asynchronous process
    	// of writing the result to disk
    	return task<unsigned int>(sessionDataWriter->StoreAsync()).then([=](unsigned int)
    	{
    		return ApplicationData::Current->LocalFolder->CreateFileAsync(StringReference(sessionStateFilename),
    			CreationCollisionOption::ReplaceExisting);
    	})
    		.then([=](StorageFile^ createdFile)
    	{
    		return createdFile->OpenAsync(FileAccessMode::ReadWrite);
    	})
    		.then([=](IRandomAccessStream^ newStream)
    	{
    		return RandomAccessStream::CopyAsync(
    			sessionData->GetInputStreamAt(0), newStream->GetOutputStreamAt(0));
    	})
    		.then([=](UINT64 copiedBytes)
    	{
    		(void) copiedBytes; // Unused parameter
    		return;
    	});
    }


    • Edited by halcyons Wednesday, July 16, 2014 2:13 AM
    Tuesday, July 15, 2014 10:14 AM
  • I will have a try about that. I thinck C++/CX should have more .net API. But anyway I must understand the serialization process. Thanck you^-^
    Friday, July 18, 2014 3:04 AM
  • I Have made a C# component and it work well. But sometimes I need pure C++ realization. So I need to learn more.
    Friday, July 18, 2014 3:07 AM