Click here to Skip to main content
Licence 
First Posted 6 Jul 2004
Views 37,305
Bookmarked 12 times

A simple but handy utility for serializing and de-serializing various data

By | 11 Aug 2004 | Article
Interop various data among native, .NET and Java codes.

Introduction

Very often, we like to serialize a set of data into an arbitrary media such as memory, hard disk, and network in a particular format. As long as you work with a real project, you will most likely meet the problem. Classical C++ standard library provides us input and output streams with extensive use of extraction (>>) and insertion (<<) operators for common primitive data types. MFC uses the class CArchive for the similar purpose. In regards to the .NET framework, it has two ways, binary and XML/SOAP to serialize data into a media. You can find more samples available to you. However, you can't interoperate serialization and de-serialization among these frameworks.

Recently, I have been working with my own framework SocketPro and needed a class named as CUQueue to serialize various data into a memory in binary format. Afterwards, I can send the memory of data into a remote machine by raw socket. The data can be easily de-serialized back to original data in the other end. One particular requirement is that the utility must have very good interoperability among native, .NET and Java codes in the future. You can use the utility to serialize various C/C++ data types (including VARIANT) into a memory and de-serialize these data back to .NET types of data (also including object).

The source codes and sample projects

The .NET version CUQueue is inside the project SocketProAdapter. C++ version of CUQueue is in the file uqueue.h. To demonstrate usage of the utility, I created a client sample project written in C# and a server project in C++. When clicking a client sample application, the sample server first receives the memory chunk and de-serializes a .NET object containing different types of data and objects into a corresponding C++ class object. Secondly, the C++ code serializes the object back to .NET framework. At last, the client C# code de-serializes the C++ memory chunk back to a class object.

If you use VC++ 6, you may have a problem in compiling CUQueue against PushVT and PopVT. Under this case, you should use MS platform SDK no earlier than 2001.

To test client sample project, register usocket.dll first.

Highlights of class CUQueue

The CUQueue class supports all of primitive data types for both C/C++ and the .NET framework. It is so simple that I don't want to speak more about serializing and de-serializing these data types.

  • Automatically re-allocate memory if required

    When serializing a primitive data or a data structure into a CUQueue memory by calling the method CUQueue::Push or CUQueue::PushVT for variant in C++, the underlying memory may automatically re-allocate memory if the memory chunk does not have enough space available.

  • Automatically take care of an array of objects and variants

    The CUQueue class fully supports serializing and de-serializing variants in C++ and objects in .NET framework. If an object or variant contains an array of primitive data types or an array of sub-variants or objects, it still works for you, because a variant or object contains the data type information. As a sample, the following code snippet will work.

    C# code:

            CUQueue UQueue = new CUQueue();
            object objMother = null;
            object []objKids = new object [3];
            object []objGrandKids = new object[3];
            objGrandKids[0] = System.DataTime.Now;
            objGrandKids[1] = "This is a grand son";
            //objGrandKids[1] = null;
            objKids[0] = 1234567;
            objKids[1] = "This is not an integer data but a string";
            objKids[2] = objGrandKids;
            objMother = objKids;
            
            //serialize an object which contains 
            //a set of sub-objects into a memory chunk
            UQueue.Push(objMother, false, false); 
    
            //C++ code
            CUQueue            UQueue;
            CComVariant      vtData;
            
            //somewhere you call UQueue.Push((BYTE*)pBuffer, ulBufferLen);
    
            //de-serialize the above dotNet object back into C++ variant
            UQueue.PopVT(vtData);
  • Currency, variant date, variant bool, and system time

    The above four native data types cannot be found in managed .NET framework, but they are close to decimal, DateTime, bool and DateTime, respectively. To enable .NET framework code to interoperate with these native date types, the .NET version of CUQueue also provides the four methods PushCY, PushVariantDate, PushVariantBool, and PushSystemTime for serializing .NET types of data into memory chunk, and PopCY, PopVariantDate, PopVariantBool, and PopSystemTime for de-serializing C++ data into .NET framework types of data.

Enhancements in the future

The current simple utility doesn't have a version for Java yet. I will certainly implement a Java version of CUQueue in some time.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Yuancai (Charlie) Ye

Web Developer

United States United States

Member

Yuancai (Charlie) Ye, an experienced software engineer, lives in Atlanta, Georgia. He is an expert at OLEDB consumer and created a powerful data accessing libarary at the site http://www.udaparts.com. He has been working at SocketPro written from batching, asynchrony and parallel computation for more than three years. Visual C++, C# and ASP.NET are his favorite development environments.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionuqueue.h? Pinmembercxdream9:18 21 Jan '05  
AnswerRe: uqueue.h? Pinmembergreba9:32 21 Jan '05  
GeneralRe: CComVariant <-> CPoint PinmemberRajesh_Parameswaran20:44 17 Oct '04  
GeneralRe: CComVariant <-> CPoint PinmemberYuancai (Charlie) Ye11:42 18 Oct '04  
GeneralCUQueue supports serializarion through dotnet binaryformatter and soapformatter PinmemberYuancai (Charlie) Ye2:00 12 Aug '04  
GeneralRe: CUQueue supports serializarion through dotnet binaryformatter and soapformatter Pinmembervasanthavalli.chandran@ubinetics.com22:16 10 Mar '05  
GeneralRe: CUQueue supports serializarion through dotnet binaryformatter and soapformatter PinmemberYuancai (Charlie) Ye2:52 11 Mar '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 12 Aug 2004
Article Copyright 2004 by Yuancai (Charlie) Ye
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid