How can I serialize and deserialize CScriptDictionary to a string?

Started by
4 comments, last by WitchLord 19 hours, 46 minutes ago

Hello. Is there any simple and fast way to convert a dictionary to a string and also deserialize it back to a CScriptDictionary? Thanks in advance.

Advertisement

I'm interested in that, too. I would be glad to have any help.

I don't have a ready example for serializing a dictionary to string and then back, but in the asrun sample application I have an implementation that can print the content of the dictionary on the screen for debugging.

https://www.angelcode.com/angelscript/sdk/docs/manual/doc_samples_asrun.html

https://svn.code.sf.net/p/angelscript/code/trunk/sdk/samples/asrun/source/main.cpp

// This is the to-string callback for the dictionary type
std::string DictionaryToString(void *obj, int expandMembers, CDebugger *dbg)
{
	CScriptDictionary *dic = reinterpret_cast<CScriptDictionary*>(obj);
 
	std::stringstream s;
	s << "(len=" << dic->GetSize() << ")";
 
	if( expandMembers > 0 )
	{
		s << " [";
		asUINT n = 0;
		for( CScriptDictionary::CIterator it = dic->begin(); it != dic->end(); it++, n++ )
		{
			s << "[" << it.GetKey() << "] = ";

			// Get the type and address of the value
			const void *val = it.GetAddressOfValue();
			int typeId = it.GetTypeId();

			// Use the engine from the currently active context (if none is active, the debugger
			// will use the engine held inside it by default, but in an environment where there
			// multiple engines this might not be the correct instance).
			asIScriptContext *ctx = asGetActiveContext();

			s << dbg->ToString(const_cast<void*>(val), typeId, expandMembers - 1, ctx ? ctx->GetEngine() : 0);
			
			if( n < dic->GetSize() - 1 )
				s << ", ";
		}
		s << "]";
	}
 
	return s.str();
}

Start from the above function, but then look at the other ToString for other types to understand how they are done as well.

In the output you'll need to add type ids so you know how to deserialize a value in order to rebuild the objects.

You may also want to read the article on serialization. The same concepts would apply for serializing to text or when serializing to binary format.

https://www.angelcode.com/angelscript/sdk/docs/manual/doc_serialization.html

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

@witchlord
I don't quite understand how I can use CSerializer to serialize a dictionary into a binary format, and back into a CScriptDictionary.

If you'd like to use the CSerializer you'll need to implement the handler for the CScriptDictionary type.

I don't have a ready implementarion for you, but I have one for the CScriptArray, which you can see here: http://svn.code.sf.net/p/angelscript/code/trunk/sdk/tests/test_feature/source/test_addon_serializer.cpp

The principle would be the same, the difference is just that for the dictionary you need to store each key/value pair with the type rather than just the values as is done for the array.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement