Problem reading value tree


#1

Hi there,

I’m trying to seralize/deserialize a value tree but I’ve got as problem during deserialization.

Here is my serialization code:

if (fileToSerializeTo.existsAsFile())
	{
		fileToSerializeTo.deleteFile();
	}
	FileOutputStream fos(fileToSerializeTo);
	jassert(!fos.failedToOpen());

	mAssetsTree.writeToStream(fos);
        fos.flush();
#ifdef JUCE_DEBUG
	File debugLog(File::getCurrentWorkingDirectory().getFullPathName() + "\\debug.txt");
	debugLog.deleteFile();
	FileOutputStream debugStream(debugLog);
	
	debugStream.writeString(mAssetsTree.toXmlString());
#endif

If I check my debug log I have :

<Assets>
  <Asset Hash="A" Thumbnail="A" Name="My way" Extension="Wav"/>
  <Asset Hash="B" Thumbnail="A" Name="MP3" Extension=""/>
  <Asset Hash="C" Thumbnail="A" Name="Supeflip, vite" Extension="Flac"/>
</Assets>

So it looks very good.

Now, my deserialization code

       jassert(fileToDeserializeFrom.existsAsFile());
	DBG(fileToDeserializeFrom.getFullPathName());
	FileInputStream fis(fileToDeserializeFrom);
	jassert(!fis.failedToOpen());

	mAssetsTree.readFromStream(fis);
	
	for (int pos = 0; pos < mAssetsTree.getNumChildren(); pos++)
	{
           // do stuff
        }

My problem is that it never enters the loop, so I checked what happens in readFromStream(fis) and there is some code

const int numProps = input.readCompressedInt();

where numprops=0 !

Just before, there is :

const String type (input.readString());

and type is Assets so it can read the value tree !

I triple checked and I read and write to the same file.

The contents of the binary file are :
AssetsAsset Hash A Thumbnail A Name My way Extension Wav Asset Hash B Thumbnail A Name MP3 Extension Asset Hash C Thumbnail A Name Supeflip, vite Extension Flac

So it looks ok. I really don’t understand what’s happening here.


#2

ValueTree::readFromStream is a static method, so you have to do :
mAssetsTree = ValueTree::readFromStream (fis);

btw, if you are on the tip, you can now write your for loop like that :

for (auto child : mAssetsTree)
{
}

#3

Thanks, you’re absolutely right. However I still have the exact same problem ?


#4

Ooops I spoke too fast. For some reason compiling the project didn’t take the change in account. Now, recompiling everything it works. Thanks a million !


#5

As I’ve advised a million times on the forum, please never, never create filenames by concatenating strings! Always use the File class to build paths, it’s way less error-prone.


#6

Yeah, that was quick and dirty code to check my serialization code was working, not production code :wink:


#8

How eery - I was scratching my head for quite some time yesterday about the exact same thing.