Problem reading value tree

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())
	FileOutputStream fos(fileToSerializeTo);

	File debugLog(File::getCurrentWorkingDirectory().getFullPathName() + "\\debug.txt");
	FileOutputStream debugStream(debugLog);

If I check my debug log I have :

  <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"/>

So it looks very good.

Now, my deserialization code

	FileInputStream fis(fileToDeserializeFrom);

	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.

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)

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

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 !

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.

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

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