Android Code Snippet Reverse Engineer JSON Menu to Tree: Menu Designer

Hi

I had designed a b4J app using the menu designer, saved the menu to an external file and unfortunately lost the file, back-ups didn't have it either.

Anyway, I thought of reverse engineering the menu from my already saved JSON menu in my app so that my 'tree' is regenerated so as to update it. After careful scrutity of the source code I came up with this snippet.

This uses code available from this post:
https://www.b4x.com/android/forum/threads/menu-designer.56853/#content

To reproduce.

1. Open the internal designer and add a new button, e.g. cmdReverse with text 'Reverse' and generate members for new button.

2. Add the source code below for your new button

B4X:
Sub cmdReverse_Action
    If ResultTA.Text.Length = 0 Then Return
    ' clear the kvs
    kvsmainmap.DeleteAll
    ' initialize other items
    treelist.Initialize
    DoneList.Initialize
    ' read the contents of the json menu
    Dim parser As JSONParser
    parser.Initialize(ResultTA.text)
    Dim root As List = parser.NextArray
    For Each colroot As Map In root
        ' get text
        Dim mText As String = colroot.Get("Text")
        ' get children if they exist
        Dim Children As List = colroot.Get("Children")
        If Children.IsInitialized = False Then Children.Initialize
        ' update the treelist
        treelist.Add(mText)
        Dim cList As List
        cList.Initialize
        ' add each child to kvs
        For Each colChildren As Map In Children
            Dim Text As String = colChildren.Get("Text")
            ' add child to the kvs
            kvsmainmap.PutObject(Text,colChildren)
            cList.Add(Text)
        Next
        ' add main menu item
        Dim m As Map
        m.Initialize
        m.Put("Text", mText)
        m.Put("Children", cList)
        kvsmainmap.PutObject(mText,m)
    Next
    ' add treelist to database
    kvsmainmap.PutObject("treelist",treelist)
    ' reload the new menu
    Try
        Load_From_kvs
    Catch
        Log("kvs empty")
    End Try   
    DoneList.Initialize
    treelist.Initialize
End Sub

3. Run the app, paste your json menu to the section that outputs the 'Create Code'

4. Click on the Reverse button.

5. Your tree will be re-generated with the json menu you copied.

I only tested this with the menu that was previously created with the menu designer.
 
Top