Jsonlizer,一个把C++各类数据转成 Json 结构体的玩意儿

news2025/2/8 4:10:53

        这段时间突发奇想,觉得可以弄一个Json和C++各种数据类型互转的工具,因为Json在进行数据储存的时候,有一些先天的优势,传统的C++的序列化方式是将数据序列化到流数据里面,而流数据是典型的串行结构(或则说是一维结构),因此,流数据对数据的位置特别的敏感,一旦序列化的元素有调整,就会导致原来存储的流数据完全失效。这一点在游戏开发时非常的麻烦,我们不得不将各个接口进行拆分,让接口对应其独自的流数据,做到局部的隔离。但是这个办法也不是万能,因为,一旦接口内某些元素顺序因为开始时过于简单而并没有做拆分时,隐藏的风险种子就种下了。随着时间推移,一旦需要更改这些元素时,就会发现被锁死了手脚!

        Json具有结构化的特征,涵盖了原型,数组,对象等数据类型,用来做存储是非常理想的,如果项目中存储的数据用Json存储,然后再序列化到二进制流数据内,这样似乎就结合了两者的优点,结合了速度和可扩展性。而我自己写的CJson类本身就是可以序列化到流数据的。而且从流数据恢复的速度远比解析Json字符串的速度快几倍,那么现在欠缺的就是如何将C++数据转换成CJson类。

        思考了一段时间,我意识到 Jsonlizer 和 Serializer 有本质的区别,一个是构建层次结构,一个是构建一维结构,我本来想尽可能的复用Serializer的代码,但我意识到,这是不可能的。我能利用的,可能仅仅是一部分最基本的,被称为 trait 模板代码。

        而想复用Serialize函数的想法是不可能的。毕竟一个是结构化的数据,一个是一维数据。于是我改变了策略,重新构造了Jsonlizer。首先要做的是重写整个Jsonlizer的模板代码。这部分代码非常的关键,就是用于识别C++的各类数据。

这几个文件的代码如下:

#ifndef TAGPJSONLIZERRIMITIVE_H
#define TAGPJSONLIZERRIMITIVE_H
//Begin section for file tagJsonPrimitive.h
//TODO: Add definitions that you want preserved
//End section for file tagJsonPrimitive.h

namespace atom
{



	//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
	template <class A, class T, bool save>
	struct tagJsonPrimitive
	{
	
	    //Begin section for si::tagJsonPrimitive
	    //TODO: Add attributes that you want preserved
	    //End section for si::tagJsonPrimitive
	    public:
	
	        //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
	        inline static void Invoke(A & node, T & t)
	        {
	        	//TODO Auto-generated method stub
	        	node.Bind( t );
	        }

	

	};  //end struct tagJsonPrimitive



} // end namespace atom



#endif

原型非常简单,没什么好说的。

#ifndef TAGJSONLIZERINVALID_H
#define TAGJSONLIZERINVALID_H
//Begin section for file tagJsonInvalid.h
//TODO: Add definitions that you want preserved
//End section for file tagJsonInvalid.h

namespace atom
{
	


	//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
	template <class A, class T, bool save>
	struct tagJsonInvalid
	{
	
	    //Begin section for si::tagJsonInvalid
	    //TODO: Add attributes that you want preserved
	    //End section for si::tagJsonInvalid
	    public:
	
	        //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
	        inline static void Invoke(A & n, T & t)
	        {
                UNREFERENCED_PARAMETER(n);
                UNREFERENCED_PARAMETER(t);

	        	//TODO Auto-generated method stub
				printf( "Jsonlization NOT support these keyword: mutable\n" );
	        }


	
	};  //end struct tagJsonInvalid



} // end namespace atom



#endif

 不支持的类型就是输出提示。

#ifndef TAGJSONLIZERARRAY_H
#define TAGJSONLIZERARRAY_H
//Begin section for file tagJsonArray.h
//TODO: Add definitions that you want preserved
//End section for file tagJsonArray.h
#include "../../../serialization/trait/array_trait.h"


namespace atom
{
	
	//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
	template <class A, class T, bool save>
	struct tagJsonArray
	{
	
	    //Begin section for si::tagJsonArray
	    //TODO: Add attributes that you want preserved
	    //End section for si::tagJsonArray
	    public:
	
	        //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
	        inline static void Invoke(A & jr, T & t)
	        {
	        	//TODO Auto-generated method stub
	        	// 读取或写入数组的长度;
	        	U32 bound = static_cast<U32>( array_trait<T>::bound );
                U32 limit = bound;
	        	
				CJson node = jr.GetCurrentNode();

				if( save == false ) {
					bound = static_cast<U32>( node.Length() );
				}

                // 确认数组的长度是否越界;
                bound = atom_min( bound, limit );
	        	
	        	// 读取或写入数组的元素;
	        	for( size_t i = 0; i < bound; ++ i )
	        	{
					CJson child( true );
					if( save == false ) {
						child = node[i];
					}

					jr.Push( child );
	        		jr.Bind( t[i] );
					jr.Pop();

					if( save ) {
						node.Push( child );
					}
	        	}
	        }


	
	};  //end struct tagJsonArray



} // end namespace atom



#endif

        数组开始有点东西了,其中能看到 Jsonlizer 有几个函数:Push,Pop,GetCurrentNode。这几个函数其实就是维护一个节点堆栈,堆栈内放入的是被操作的节点列表。顶部的节点就是最新需要被操作的节点。

#ifndef TAGJSONLIZERCLASS_H
#define TAGJSONLIZERCLASS_H
//Begin section for file tagJsonClass.h
//TODO: Add definitions that you want preserved
//End section for file tagJsonClass.h



// 为序列化类而特别准备的缺省模板函数。
template<class A, class T>
inline void Jsonlize(A & jr, T & t, bool save)
{
	t.Jsonlize( jr, save );
}


namespace atom
{



	//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
	template <class A, class T, bool save>
	struct tagJsonClass
	{
	
	    //Begin section for si::tagJsonClass
	    //TODO: Add attributes that you want preserved
	    //End section for si::tagJsonClass
	    public:
	
	        //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
	        inline static void Invoke(A & jr, T & t)
	        {
	        	//TODO Auto-generated method stub
	        	Jsonlize( jr, t, save );
	        }


	
	};  //end struct tagJsonClass


	
} // end namespace atom



#endif

 类的代码其实不复杂,因为类的代码需要用户自己完成。

#ifndef TAGJSONLIZER_H
#define TAGJSONLIZER_H
//Begin section for file tagJsonlizer.h
//TODO: Add definitions that you want preserved
//End section for file tagJsonlizer.h

#include "../../../serialization/trait/is_primitive.h"
#include "../../../serialization/trait/is_array.h"
#include "../../../serialization/trait/is_class.h"
#include "../../../serialization/trait/is_pointer.h"
#include "../../../serialization/trait/if_else.h"
#include "../../../serialization/trait/degradation_trait.h"
#include "tagJsonPrimitive.h"
#include "tagJsonArray.h" 
#include "tagJsonClass.h"
#include "tagJsonInvalid.h"



namespace atom
{



	//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
	template <class A, class T, bool B>
	struct tagJsonlizer
	{
	    //Begin section for si::tagJsonlizer
	    //TODO: Add attributes that you want preserved
	    //End section for si::tagJsonlizer
	    public:
	


	        //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
	        inline static void Jsonlize(A & json, T & data)
	        {
	        	//TODO Auto-generated method stub
                typedef typename 
                degradation_trait<T>::type C;

				typedef 
				typename
				if_else<is_array<C>::value,
					tagJsonArray<A, C, B>, 
					typename
					if_else<is_class<C>::value, 
						tagJsonClass<A, C, B>, 
						typename
						if_else<is_primitive<C>::value,
						    tagJsonPrimitive<A, C, B>,
						    tagJsonInvalid  <A, C, B>
						>::type 
					>::type 
				>::type Invoker;
		
				Invoker::Invoke( json, type_cast(data) );
			}
	


	};  //end struct tagJsonlizer
	


} // end namespace atom



#endif

这个就是Jsonlizer模板的入口了,接下来就是导入,导出类的代码。

#ifndef CJSONIMPORTER_H
#define CJSONIMPORTER_H
//Begin section for file CJsonImporter.h
//TODO: Add definitions that you want preserved
//End section for file CJsonImporter.h
#include "../../../Common.h"
#include "../../../interface/IEmbedInterface.h"
#include "../../../interface/IInterface.h"
#include "../../../interface/IJsonlizerRoot.h"
#include "../../../enumeration/INTERFACE_ID.h"
#include "../../../os/character/CCharset.h"
#include "../../stl/a_string.h"
#include "../../stl/a_wstring.h"
#include "../../hex/hex.h"
#include "../../tool/CInterface.h"
#include "tagJsonlizer.h"



namespace atom
{



    //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
    class CJsonImporter : public IEmbedInterface
    {

        //Begin section for atom::CJsonImporter
        //TODO: Add attributes that you want preserved
        //End section for atom::CJsonImporter

        private:


            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            IInterface * nest;



			#ifdef _SHIPPING_
			//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
			IReferencedInterface * cast;
			#endif




        public:

            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            CJsonImporter(); 



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            virtual ~CJsonImporter(); 



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            virtual int IncRef(); 



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            virtual int DecRef(); 



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            virtual int GetRef(); 



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            virtual IInterface * QueryInterface(U32 iid); 



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            virtual void SetNest(IInterface * nest); 



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline CJson GetCurrentNode()
            {
                // TODO Auto-generated method stub
                CJson result;
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) ) {
                    result = root -> GetCurrentNode();
                }
                return result;
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Push(CJson & node)
            {
                // TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) ) {
                    root -> Push( node );
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Pop()
            {
                // TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) ) {
                    root -> Pop();
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(bool & value)
            {
                // TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) )
                {
                    CJson node = root -> GetCurrentNode();
                    value = static_cast<bool>( node );
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(char & value)
            {
                // TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) )
                {
                    CJson node = root -> GetCurrentNode();
                    value = static_cast<U08>( node );
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(I08 & value)
            {
                // TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) )
                {
                    CJson node = root -> GetCurrentNode();
                    value = static_cast<I08>( node );
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(I16 & value)
            {
                // TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) )
                {
                    CJson node = root -> GetCurrentNode();
                    value = static_cast<I16>( node );
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(I32 & value)
            {
                // TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) )
                {
                    CJson node = root -> GetCurrentNode();
                    value = static_cast<I32>( node );
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(I64 & value)
            {
                // TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) )
                {
                    CJson node = root -> GetCurrentNode();
                    value = static_cast<I64>( node );
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(U08 & value)
            {
                // TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) )
                {
                    CJson node = root -> GetCurrentNode();
                    value = static_cast<U08>( node );
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(U16 & value)
            {
                // TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) )
                {
                    CJson node = root -> GetCurrentNode();
                    value = static_cast<U16>( node );
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(U32 & value)
            {
                // TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) )
                {
                    CJson node = root -> GetCurrentNode();
                    value = static_cast<U32>( node );
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(U64 & value)
            {
                // TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) )
                {
                    CJson node = root -> GetCurrentNode();
                    value = static_cast<U64>( node );
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(float & value)
            {
                // TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) )
                {
                    CJson node = root -> GetCurrentNode();
                    value = static_cast<float>( node );
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(double & value)
            {
                // TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) )
                {
                    CJson node = root -> GetCurrentNode();
                    value = static_cast<double>( node );
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(a_string & value)
            {
                // TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) )
                {
                    CJson node = root -> GetCurrentNode();
                    value = static_cast<a_string>( node );
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(a_wstring & value)
            {
                // TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) )
                {
                    CJson node = root -> GetCurrentNode();
                    a_string text = node;
                    CCharset charset( text.c_str() );
                    value = charset.FromUtf8.ToUnicode;
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(void * buffer, U64 length)
            {
                // TODO Auto-generated method stub
                if( buffer && length )
                {
                    CInterface<IJsonlizerRoot> root;
                    if( root.Mount(this, IID_JSONLIZER_ROOT) )
                    {
                        CJson node = root -> GetCurrentNode();
                        a_string text = node;
                        FromHex( text, buffer, length );
                    }
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            template<class T>
            inline void Bind(T & value)
            {
                //TODO Auto-generated method stub
	            tagJsonlizer<CJsonImporter, T, false>::Jsonlize( *this, value );
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            template<class T>
            inline void Bind(const char * name, T & value)
            {
                //TODO Auto-generated method stub
                if( name )
                {
                    CInterface<IJsonlizerRoot> root;
                    if( root.Mount(this, IID_JSONLIZER_ROOT) )
                    {
                        CJson node  = root -> GetCurrentNode();
                        CJson child = node[name];
                        root -> Push( child );
	                    Bind( value );
                        root -> Pop();
                    }
                }
            }




    };  //end class CJsonImporter



} //end namespace atom



#endif

#ifndef CJSONEXPORTER_H
#define CJSONEXPORTER_H
//Begin section for file CJsonExporter.h
//TODO: Add definitions that you want preserved
//End section for file CJsonExporter.h
#include "../../../Common.h"
#include "../../../interface/IEmbedInterface.h"
#include "../../../interface/IInterface.h"
#include "../../../interface/IJsonlizerRoot.h"
#include "../../../enumeration/INTERFACE_ID.h"
#include "../../../os/character/CCharset.h"
#include "../../stl/a_string.h"
#include "../../stl/a_wstring.h"
#include "../../hex/hex.h"
#include "../../tool/CInterface.h"
#include "tagJsonlizer.h"



namespace atom
{



    //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
    class CJsonExporter : public IEmbedInterface
    {

        //Begin section for atom::CJsonExporter
        //TODO: Add attributes that you want preserved
        //End section for atom::CJsonExporter

        private:


            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            IInterface * nest;



			#ifdef _SHIPPING_
			//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
			IReferencedInterface * cast;
			#endif




        public:

            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            CJsonExporter(); 



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            virtual ~CJsonExporter(); 



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            virtual int IncRef(); 



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            virtual int DecRef(); 



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            virtual int GetRef(); 



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            virtual IInterface * QueryInterface(U32 iid); 



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            virtual void SetNest(IInterface * nest); 



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline CJson GetCurrentNode()
            {
                // TODO Auto-generated method stub
                CJson result;
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) ) {
                    result = root -> GetCurrentNode();
                }
                return result;
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Push(CJson & node)
            {
                // TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) ) {
                    root -> Push( node );
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Pop()
            {
                // TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) ) {
                    root -> Pop();
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(bool & value)
            {
                //TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) )
                {
                    CJson node = root -> GetCurrentNode();
                    node = value;
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(char & value)
            {
                //TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) )
                {
                    CJson node = root -> GetCurrentNode();
                    node = value;
                }
            }


            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(I08 & value)
            {
                //TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) )
                {
                    CJson node = root -> GetCurrentNode();
                    node = value;
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(I16 & value) 
            {
                //TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) )
                {
                    CJson node = root -> GetCurrentNode();
                    node = value;
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(I32 & value)
            {
                //TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) )
                {
                    CJson node = root -> GetCurrentNode();
                    node = value;
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(I64 & value)
            {
                //TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) )
                {
                    CJson node = root -> GetCurrentNode();
                    node = value;
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(U08 & value)
            {
                //TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) )
                {
                    CJson node = root -> GetCurrentNode();
                    node = value;
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(U16 & value)
            {
                //TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) )
                {
                    CJson node = root -> GetCurrentNode();
                    node = value;
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(U32 & value)
            {
                //TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) )
                {
                    CJson node = root -> GetCurrentNode();
                    node = value;
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(U64 & value)
            {
                //TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) )
                {
                    CJson node = root -> GetCurrentNode();
                    node = value;
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(float & value)
            {
                //TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) )
                {
                    CJson node = root -> GetCurrentNode();
                    node = value;
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(double & value)
            {
                //TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) )
                {
                    CJson node = root -> GetCurrentNode();
                    node = value;
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(const char * value) 
            {
                //TODO Auto-generated method stub
                if( value )
                {
                    CInterface<IJsonlizerRoot> root;
                    if( root.Mount(this, IID_JSONLIZER_ROOT) )
                    {
                        CJson node = root -> GetCurrentNode();
                        node = value;
                    }
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(const wchar_t * value)
            {
                //TODO Auto-generated method stub
                if( value )
                {
                    CInterface<IJsonlizerRoot> root;
                    if( root.Mount(this, IID_JSONLIZER_ROOT) )
                    {
                        CJson node = root -> GetCurrentNode();
                        CCharset charset( value );
                        a_string text = charset.ToUtf8;
                        node = text.c_str();
                    }
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(a_string & value) 
            {
                //TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) )
                {
                    CJson node = root -> GetCurrentNode();
                    node = value.c_str();
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(a_wstring & value)
            {
                //TODO Auto-generated method stub
                CInterface<IJsonlizerRoot> root;
                if( root.Mount(this, IID_JSONLIZER_ROOT) )
                {
                    CJson node = root -> GetCurrentNode();
                    CCharset charset( value.c_str() );
                    a_string text = charset.ToUtf8;
                    node = text.c_str();
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Bind(void * buffer, U64 length)
            {
                //TODO Auto-generated method stub
                if( buffer && length )
                {
                    CInterface<IJsonlizerRoot> root;
                    if( root.Mount(this, IID_JSONLIZER_ROOT) )
                    {
                        a_string text = ToHex( buffer, length );
                        CJson node = root -> GetCurrentNode();
                        node = text.c_str();
                    }
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            template<class T>
            inline void Bind(T & value)
            {
                //TODO Auto-generated method stub
	        	tagJsonlizer<CJsonExporter, T, true>::Jsonlize( * this, value );
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            template<class T>
            inline void Bind(const T & value)
            {
                //TODO Auto-generated method stub
                Bind( const_cast<T &>(value) );
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            template<class T>
            inline void Bind(const char * name, T & value)
            {
                //TODO Auto-generated method stub
                if( name )
                {
                    CInterface<IJsonlizerRoot> root;
                    if( root.Mount(this, IID_JSONLIZER_ROOT) )
                    {
                        CJson node  = root -> GetCurrentNode();
                        CJson child = node[name];
                        root -> Push( child );
	                    Bind( value );
                        root -> Pop();
                    }
                }
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            template<class T>
            inline void Bind(const char * name, const T & value)
            {
                //TODO Auto-generated method stub
                Bind( name, const_cast<T &>(value) );
            }



    };  //end class CJsonExporter



} //end namespace atom



#endif

导入导出的类有一个特殊的函数:

inline void Bind(const char * name, T & value)

这个函数带了一个name的参数,其实就是针对类的成员变量而设的。

#ifndef CJSONLIZER_H
#define CJSONLIZER_H
//Begin section for file CJsonlizer.h
//TODO: Add definitions that you want preserved
//End section for file CJsonlizer.h
#include "../../../Common.h"
#include "CJsonExporter.h"
#include "CJsonImporter.h"
#include "CJsonlizerRoot.h"
#include "../CJson.h"



inline const char * last_dot(const char * value)
{
    const char * result(value);
    if( value ) 
    {
        for( ;; ++ value )
        {
            if( *value == 0 ) break;

            if( *value == '.' ) {
                result = value + 1;
            }
        }
    }    
    return result;
}



#define JKV(V) make_pair(a_string(#V), V)
#define JBD(A,V)  A.Bind(last_dot(#V), V)



namespace atom
{



    //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
    class CJsonlizer : public IInterface
    {

        //Begin section for atom::CJsonlizer
        //TODO: Add attributes that you want preserved
        //End section for atom::CJsonlizer

        private:


            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            CJsonExporter exporter;



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            CJsonImporter importer;



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            CJsonlizerRoot root;




        public:

            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            CJsonlizer(); 



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            virtual ~CJsonlizer(); 



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            virtual IInterface * QueryInterface(U32 iid); 



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            bool Assign(CJson & data); 



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            bool Obtain(CJson & data); 



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            void Clear(); 



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline CJson GetCurrentNode()
            {
                //TODO Auto-generated method stub
                root.GetCurrentNode();
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Push(CJson & node)
            {
                //TODO Auto-generated method stub
                root.Push( node );
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            inline void Pop()
            {
                //TODO Auto-generated method stub
                root.Pop();
            }


            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            template<class T>
            inline CJsonlizer & operator <<(const T & value)
            {
                //TODO Auto-generated method stub
                exporter.Bind( value );
                return( * this );
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            template<class T>
            inline CJsonlizer & operator <<(const pair<a_string, T> & value)
            {
                //TODO Auto-generated method stub
                exporter.Bind( value.first.c_str(), value.second );
                return( * this );
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            template<class T>
            inline CJsonlizer & operator >>(T & value)
            {
                //TODO Auto-generated method stub
                importer.Bind( value );
                return( * this );
            }



            //@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
            template<class T>
            inline CJsonlizer & operator >>(pair<a_string, T> & value)
            {
                //TODO Auto-generated method stub
                importer.Bind( value.first.c_str(), value.second );
                return( * this );
            }



    };  //end class CJsonlizer



} //end namespace atom



#endif

        最后就是最终的绑定接口了,接下来我展示一个简单的例子:

struct tagTest
{
	U32 index;
	a_string name;

	tagTest():index(0) {
	}

	~tagTest() {
	}
};

struct tagTest2
{
	U32 index;
	tagTest value[4];

	tagTest2():index(0) {
	}

	~tagTest2() {
	}
};


template<class A>
inline void Jsonlize(A & jr, tagTest & t, bool save)
{
	JBD( jr, t.index );
	JBD( jr, t.name );
}

template<class A>
inline void Jsonlize(A & jr, tagTest2 & t, bool save)
{
	JBD( jr, t.index );
	JBD( jr, t.value );
}

准备了两个类,这两个类有聚合关系。然后我初始化这两个类,然后用Jsonizer将这个类导出成Json数据结构。

		tagTest2 in, out;
		in.index = 1;
		in.value[0].index = 101;
		in.value[0].name  = "101";
		in.value[1].index = 102;
		in.value[1].name  = "102";
		in.value[2].index = 103;
		in.value[2].name  = "103";
		in.value[3].index = 104;
		in.value[3].name  = "104";

		CJsonlizer jr;
		jr << in;

		CJson json;
		jr.Obtain( json );

		a_string value = json.Stringify();
		printf( "%s\n", value.c_str() );

运行的结果如下:

其实并不困难。大家也可以试试,其中 trait 的那些代码可以去看看boost的序列化相关的代码。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2267139.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

删除拼排序链表中的重复元素(最优解)

题目来源 82. 删除排序链表中的重复元素 II - 力扣&#xff08;LeetCode&#xff09; 题目描述 给定一个已排序的链表的头 head &#xff0c; 删除原始链表中所有重复数字的节点&#xff0c;只留下不同的数字 。返回 已排序的链表 。 示例 1&#xff1a; 输入&#xff1a;head…

OpenHarmony-5.PM 子系统(2)

电池服务组件OpenHarmony-4.1-Release 1.电池服务组件 Battery Manager 提供了电池信息查询的接口&#xff0c;同时开发者也可以通过公共事件监听电池状态和充放电状态的变化。电池服务组件提供如下功能&#xff1a; 电池信息查询。充放电状态查询。关机充电。 电池服务组件架…

深入浅出 Linux 操作系统

深入浅出 Linux 操作系统 引言 在当今数字化的时代&#xff0c;Linux 操作系统无处不在。从支撑互联网巨头庞大的数据中心&#xff0c;到嵌入智能家居设备的微型芯片&#xff0c;Linux 都发挥着关键作用。然而&#xff0c;对于许多人来说&#xff0c;Linux 仍笼罩着一层神秘的…

uniapp 文本转语音

uniapp 文本转语音 基于 Minimax API 的 UniApp 文本转语音工具&#xff0c;支持文本分段、队列播放、暂停恢复等功能。目前只内置了 Minimax文本转语音Minimax 的语音生成技术以其自然、情感丰富和实时性强而著称 API_KEY、GroupId 获取方法 https://platform.minimaxi.com…

前端图像处理(二)

目录 一、上传 1.1、文件夹上传以及进度追踪 1.2、拖拽上传 1.3、图片裁剪上传原理 二、图片布局 2.1、渐进式图片 2.2、图片九宫格 2.3、轮播图(Js) 2.3.1、3D动画轮播图 2.3.2、旋转切换的轮播图 2.4、卡片移入翻转效果 2.5、环绕式照片墙 一、上传 1.1、文件夹…

3.BMS系统原理图解读

一、BMS电池板 (1)电池的连接关系&#xff1a;串联 (2)采样控制点&#xff1a;CELL0 - CELL5 (3)端子P1和P3&#xff1a;BAT和BAT- (4)开关S1&#xff1a;控制充放电回路的机械开关 二、BMS控制板 (1)主控MCU 电源 复位 晶振 (2)LED指示灯&#xff1a;4电量指示 1调试指…

用于汽车碰撞仿真的 Ansys LS-DYNA

使用 Ansys LS-DYNA 进行汽车碰撞仿真汽车碰撞仿真 简介 汽车碰撞仿真是汽车设计和安全工程的一个关键方面。这些仿真使工程师能够预测车辆在碰撞过程中的行为&#xff0c;从而有助于改进安全功能、增强车辆结构并符合监管标准。Ansys LS-DYNA 是一款广泛用于此类仿真的强大工具…

使用Java和不同HTTP客户端库发送各种Content-Type类型请求

1. 引言 在HTTP协议中&#xff0c;Content-Type头用于指示请求或响应中数据的媒体类型。了解和正确设置Content-Type 对于确保客户端和服务器之间正确解析数据至关重要。本文将介绍如何使用Java 和 不同的HTTP客户端发送各种Content-Type 类型的请求。 2. 常见的Content-Type…

YOLO11改进-注意力-引入自调制特征聚合模块SMFA

本篇文章将介绍一个新的改进机制——SMFA&#xff08;自调制特征聚合模块&#xff09;&#xff0c;并阐述如何将其应用于YOLOv11中&#xff0c;显著提升模型性能。随着深度学习在计算机视觉中的不断进展&#xff0c;目标检测任务也在快速发展。YOLO系列模型&#xff08;You Onl…

【单片机通讯协议】—— 常用的UART/I2C/SPI等通讯协议的基本原理与时序分析

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、通信基本知识1.1 MCU的参见外设1.2 通信的分类按基本的类型从传输方向上来分 二、UART&#xff08;串口通讯&#xff09;2.1 简介2.2 时序图分析2.3 UART的…

Docker 部署 plumelog 最新版本 实现日志采集

1.配置plumelog.yml version: 3 services:plumelog:#此镜像是基于plumelog-3.5.3版本image: registry.cn-hangzhou.aliyuncs.com/k8s-xiyan/plumelog:3.5.3container_name: plumelogports:- "8891:8891"environment:plumelog.model: redisplumelog.queue.redis.redi…

Empire Lupin One靶机

靶机 ip&#xff1a;192.168.152.157 我们访问页面 第一步信息收集 我们先扫描一下端口 扫描到开启了 22 端口 80 端口 我们使用御剑扫描一下网站的后台 我们挨个访问一下 发现 apache 的帮助页面&#xff0c;暂时记录&#xff0c;看看等会有没有需要 我们查看到 robots.tx…

WPF 绘制过顶点的圆滑曲线(样条,贝塞尔)

项目中要用到样条曲线&#xff0c;必须过顶点&#xff0c;圆滑后还不能太走样&#xff0c;捣鼓一番&#xff0c;发现里面颇有玄机&#xff0c;于是把我多方抄来改造的方法发出来&#xff0c;方便新手&#xff1a; 如上图&#xff0c;看代码吧&#xff1a; -------------------…

绝美的数据处理图-三坐标轴-散点图-堆叠图-数据可视化图

clc clear close all %% 读取数据 load(MyColor.mat) %读取颜色包for iloop 1:25 %提取工作表数据data0(iloop) {readtable(data.xlsx,sheet,iloop)}; end%% 解析数据 countzeros(23,14); for iloop 1:25index(iloop) { cell2mat(table2array(data0{1,iloop}(1,1)))};data(i…

hdfs命令(三)- hdfs 管理命令(三)- hdfs dfsadmin命令

文章目录 前言一、hdfs分布式文件系统管理命令1. 介绍2. 语法及解释3. 命令3.1 生成HDFS集群的状态报告3.1.1 语法及解释3.1.2 示例 3.2 重新加载配置文件并更新NameNode中的节点列表3.3 刷新指定DataNode上的NameNode信息3.3.1 语法 3.4 获取并显示指定DataNode的信息3.4.1 语…

Word论文交叉引用一键上标

Word论文交叉引用一键上标 1.进入Microsoft word使用CtrlH快捷键或单击替换按钮 2.在查找内容中输入[^#] 3.鼠标点击&#xff0c;标签为“替换为&#xff1a;”的文本框&#xff0c;注意光标一定要打在图红色方框圈中的文本框中&#xff01; 4.点击格式选择字体 5.勾选上标…

JAVA:最简单多线程方法调用

以下介绍在JAVA中&#xff0c;最简单调用多线程的方法。 在需要使用多线程方法的类中&#xff0c;新增线程类Thread并实现方法run。 //定义多线程class ThreadLinePoints extends Thread{private String m;public ThreadLinePoints(){}public ThreadLinePoints(String m){this…

Hadoop中MapReduce过程中Shuffle过程实现自定义排序

文章目录 Hadoop中MapReduce过程中Shuffle过程实现自定义排序一、引言二、实现WritableComparable接口1、自定义Key类 三、使用Job.setSortComparatorClass方法2、设置自定义排序器3、自定义排序器类 四、使用示例五、总结 Hadoop中MapReduce过程中Shuffle过程实现自定义排序 一…

科技云报到:人工智能时代“三大件”:生成式AI、数据、云服务

科技云报到原创。 就像自行车、手表和缝纫机是工业时代的“三大件”。生成式AI、数据、云服务正在成为智能时代的“新三大件”。加之全球人工智能新基建加速建设&#xff0c;成为了人类社会数字化迁徙的助推剂&#xff0c;让新三大件之间的耦合越来越紧密。从物理世界到数字世…

Windows 11 中部署 Linux 项目

一、总体思路 在 Windows 11 中部署 Linux 项目&#xff0c;主要是借助 Windows Subsystem for Linux&#xff08;WSL&#xff09;来实现。在WSL中新建基于Linux的项目虚拟环境&#xff0c;以供WIN下已克隆的项目使用。WSL 允许在 Windows 系统上运行原生的 Linux 二进制可执行…