[osg-users] XML serialisation issues / examples

Hartwig Wiesmann hartwig.wiesmann at skywind.eu
Mon Jan 15 15:54:41 PST 2018


Hi,

as previously mentioned there are some issues when using serialisation with XML. Here is a small code snippet:

Code:

#define USE_BRACKETS 1
#define USE_USER_SERIALIZER 1

namespace TestOSG
{
	class TestStruct : public osg::Object
	{
	public:
		TestStruct(void)
		{
		}
		TestStruct(TestStruct const& test, osg::CopyOp const& copyOperator=osg::CopyOp::SHALLOW_COPY)
							:osg::Object(test,copyOperator),
							 m_From(test.m_From), m_Till(test.m_Till)
		{
		}
		
		META_Object(TestOSG,TestStruct);
		
		osg::Vec3 m_From;
		osg::Vec3 m_Till;
		
		osg::Vec3 const& getFrom(void) const {return m_From;}
		osg::Vec3 const& getTill(void) const {return m_Till;}
		
		void setFrom(osg::Vec3 const& from) {m_From = from;}
		void setTill(osg::Vec3 const& till) {m_Till = till;}
	};
}

namespace
{
	bool checkTestSerialization(TestOSG::TestStruct const&)
	{
		return true;
	}

	bool readTestSerialization(osgDB::InputStream& inputStream, TestOSG::TestStruct& testData)
	{
		inputStream >> inputStream.BEGIN_BRACKET;
		inputStream >> inputStream.PROPERTY("From") >> testData.m_From;
		inputStream >> inputStream.PROPERTY("Till") >> testData.m_Till;
		inputStream >> inputStream.END_BRACKET;
		return true;
	}
	
	bool writeTestSerialization(osgDB::OutputStream& outputStream, TestOSG::TestStruct const& testData)
	{
#if (USE_BRACKETS)
		outputStream << outputStream.BEGIN_BRACKET << std::endl;
#endif
		outputStream << outputStream.PROPERTY("From") << testData.m_From << std::endl;
		outputStream << outputStream.PROPERTY("Till") << testData.m_Till << std::endl;
#if (USE_BRACKETS)
		outputStream << outputStream.END_BRACKET << std::endl;
#endif
		return true;
	}
}

#if (USE_USER_SERIALIZER)
REGISTER_OBJECT_WRAPPER(TestStruct,
												new TestOSG::TestStruct,
												TestOSG::TestStruct,
												"osg::Object TestOSG::TestStruct")
{
	ADD_USER_SERIALIZER(TestSerialization);
}
#else
REGISTER_OBJECT_WRAPPER(TestStruct,
												new TestOSG::TestStruct,
												TestOSG::TestStruct,
												"osg::Object TestOSG::TestStruct")
{
	ADD_VEC3_SERIALIZER(From,osg::Vec3(1,0,0));
	ADD_VEC3_SERIALIZER(Till,osg::Vec3(1,0,0));
}
#endif

extern "C" void wrapper_serializer_library_TestOSG(void)
{
}

USE_SERIALIZER_WRAPPER(TestStruct)




The output depends on the flags.

USE_BRACKETS == 1
USE_USER_SERIALIZER == 0 (OK):

< ?xml version="1.0" encoding="UTF-8" ?>
< TestOSG::TestStruct>
  < UniqueID attribute="1&nl;">
    < From attribute="0 0 0" />
    < Till attribute="0 0 0&nl;&nl;" />
  < /UniqueID>
< /TestOSG::TestStruct>

USE_BRACKETS == 1
USE_USER_SERIALIZER == 1 (wrong):

< ?xml version="1.0" encoding="UTF-8" ?>
< TestOSG::TestStruct>
  < UniqueID attribute="1&nl;">
    < TestSerialization attribute="&nl;">
      < From attribute="0 0 0&nl;">
        < Till attribute="0 0 0&nl;&nl;&nl;" />
      < /From>
    < /TestSerialization>
  < /UniqueID>
< /TestOSG::TestStruct>


USE_BRACKETS == 0
USE_USER_SERIALIZER == 1 (wrong):

< ?xml version="1.0" encoding="UTF-8" ?>
< TestOSG::TestStruct>
  < UniqueID attribute="1&nl;">
    < TestSerialization />
    < From attribute="0 0 0" />
    < Till attribute="0 0 0&nl;&nl;" />
  < /UniqueID>
< /TestOSG::TestStruct>

PS: I have added the space between "<" and the following xml text because otherwise the output does not show up.

Cheers,
Hartwig[/quote]

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=72761#72761







More information about the osg-users mailing list