Click here to Skip to main content
12,949,083 members (68,161 online)
Click here to Skip to main content
Add your own
alternative version

Stats

9.4K views
160 downloads
5 bookmarked
Posted 10 May 2014

ATL with COM Native C++ JSON (de)serializer

, 12 Mar 2017 CPOL
Rate this:
Please Sign up or sign in to vote.
Deserialization and serialization classes for JSON objects in native C++ with COM and ATL

Introduction

Microsoft provides JSON parsing capability in .NET but those wishing to do ATL Server programming will have to roll their own. These classes address that issue.  They solve the generic types of JavaScript using COM objects including VARIANT and SAFEARRAY.

Background

One should be familiar with ATL, COM, C++, JSON and server-side programming.

Using the Code

Deserialization example (assuming in a CRequestHandlerT<> derived class):

_tavariant_t varDeserialized;
CAtlMap<CStringW, VARIANT, CStringElementTraitsI<CStringW>>* dictionary;
JavaScriptObjectDeserializer::BasicDeserialize(varDeserialized, 
((CStringA)m_HttpRequest.FormVars.GetKeyAt(pos)).GetBuffer(), c_DefaultRecursionLimit);
if (varDeserialized.vt == (VT_BYREF | VT_UNKNOWN)) {
    dictionary = &((_tavariant_t::DictionaryVariant*)V_BYREF(&varDeserialized))->Dictionary;
}

Serialization example (assuming in a CRequestHandlerT<> derived class):

_tavariant_t varDeserialized;
CAtlArray<CStringW> varArray;
if (AtlArrayToVariant<CStringW>(varDeserialized, varArray) {
    m_HttpResponse << JavaScriptSerializer::SerializeInternal(varDeserialized);
}

Points of Interest

This is based off older .NET 2.0 JSON serialization code and could be updated with any improvements since then. Reflector and decompilation of the .NET libraries allowed for a decent translation though many of the native .NET objects had to use C++ equivalents and the translation is quite large given the nature of C++ is quite different.

History

  • Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Gregory Morse
Software Developer (Senior)
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralNot too much Pin
Marius Bancila12-Mar-17 12:25
professionalMarius Bancila12-Mar-17 12:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web01 | 2.8.170524.1 | Last Updated 12 Mar 2017
Article Copyright 2014 by Gregory Morse
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid