B4J Library [B4X] BalConverter - Convert the layouts files to JSON (and vice versa)

warwound

Expert
Licensed User
Longtime User
I can not imagine how you did.

The JSON file is so intricate!

Have you used the Replace only or are you able to get "clean" hierarchical structures of layouts?

Without this, the conversion is useless.

My PHP script uses the PHP json_decode function to turn the json created by BalConverter into a PHP object.
I then manipulate the PHP object and use the json_encode function to convert the object back into a json file.
BalConverter then converts this json file back into a new layout file.

If you'd like to see the script i can upload it...

Martin.
 

LucaMs

Expert
Licensed User
Longtime User
Thanks, Martin.

I do not know JSON (after all, I hate xml in general).

I think I could integrate the functions of this project with those of the JsonTree and get the same result.

The point is that the two sources are complex, I'll have to study a lot to get what I have posted in #18.

Thanks again.
 

Mashiane

Expert
Licensed User
Longtime User
Hi Martin, can you upload your script, im very curious. thanks
 

warwound

Expert
Licensed User
Longtime User
Here's that PHP script.

  • SOURCE_PATH is a folder full of JSON files.
    Each JSON file is a b4a .bal file converted to JSON using BalConverter.
  • DEST_PATH is where the script will save each updated file from SOURCE_PATH.
  • $layout_file_blacklist is an array of JSON file names from SOURCE_PATH that should be ignored by the script.
    In this example no files are ignored.
  • $resource_value_blacklist is an array of string values which should not be updated.
    These string values are typically the text properties of views such as Labels.
    If a view displays text that is in this array then that text value is not updated.
    So the script doesn't try to update text values such as the empty string and non-ascii characters.
  • $view_name_blacklist is an array of views names that the script should not update.

The main part of the script is this part:

PHP:
foreach ($directory_iterator as $xml_file) {

	if ($xml_file->isFile()) {
		$json_file_filename = $xml_file->getFilename();

		if (in_array($json_file_filename, $layout_file_blacklist)) {
			echo "Ignoring blacklisted layout file: '$json_file_filename'<br>";
		} else {
			$json_string = file_get_contents(SOURCE_PATH . "/$json_file_filename");
			if ($json_string === false) {
				echo "Failed to open layout file: '$json_file_filename'<br>";
			} else {
				$layout_object = json_decode($json_string, false);
				if ($layout_object === null) {
					echo "Failed to decode json in layout file: '$json_file_filename'";
				} else {
					echo "Updating layout file: '<b>$json_file_filename</b>'<br>";
					localize_kids_object($layout_object->{'Data'}->{':kids'}, $layout_object);
					echo '<hr>';

					file_put_contents(DEST_PATH . "/$json_file_filename", json_encode($layout_object));
				}
			}
		}
	}
}

It gets a list of files in the source directory.
Converts each file into a PHP JSON object and passes the object to the localize_kids_object() function.
localize_kids_object() then looks at the type of b4a View that is to be updated and updates it accordingly.
Finally the updated PHP JSON object is saved to the destination directory.

All files in the destination directory can then be converted from JSON to .bal using BalConverter.

Martin.
 

Attachments

  • b4a_layout_updater.zip
    2 KB · Views: 581

DavideV

Active Member
Licensed User
Longtime User
Hi, i'm sharing B4A projects with other developers over there. We compare and merge our projects using winmerge but unfortunately it can't handle .bal files so json will be good.
I just found this tool but it crashes with some layouts (in particular with customviews) and it's ok with others. We are using B4A V7.80. Does this tool need an update ?
Also if the source is public i really don't know where to put my hands on...

Thanks in advance
Davide
 

Alessandro71

Well-Known Member
Licensed User
Longtime User
any chance of having Designer reading and writing text files directly?
it would simplify diffs and merges between projects
 

DavideV

Active Member
Licensed User
Longtime User
Great work Erel, thanks.

A side note: if you rebuild the layout from the json then you need to reopen it in the designer before make use of it.
I've got a crash with the layout uploaded above that make use of FontAwesome fonts. The log says: b4xfontawesome.otf not found in asset dir,
solved by open, save and close the layout from the designer (no other changes).

Have a Great New Year
 

ilan

Expert
Licensed User
Longtime User
this is really awesome, i was looking for this for a long time. i have one question, please. i would like to get only part of the json file back.

for example this is a part of the file:


what i need is only this:


so the name of the view, his position (left and top) and size (width height).
can i get only those 5 parameteres for all views in the bal file?

thanx
 

ilan

Expert
Licensed User
Longtime User
Parse the json string and extract whatever information you need. It should be simple.

i would like to have something that will work with any bal file and not only with one.
i will try to get the info from the props map and create my own JSON file.
if i will make it this tool can save me lots of hours of codding. i can create a tool that handles the resizing of my layout. something today i do manually.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…