Search Unity

  1. Click here to see what's on sale for the "Best of Super Sale" on the Asset Store
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Resolved Modify Json schema

Discussion in 'Scripting' started by Mazdamundi, Oct 8, 2020.

  1. Mazdamundi

    Mazdamundi

    Joined:
    Dec 5, 2017
    Posts:
    8
    Hi Everyone !

    I use a little Visionlib who is very great but I would like to modify a variable on a json schema (.vl file) :

    https://visionlib.com/2020/04/02/visionlib-release-20-3-1/

    (part json schema)

    I don't understand how to change it (the name of the variable is "showLineModel": true )

    I would like to change it to false or true in function of the action of the users (in a script).

    If someone know how to do ? Or have a good tutorial ? I saw tutorials for create a json schema (.txt file) but no how to modify a value in a file already existing.

    Thanks and have a nice day !
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    9,699
    VisionLib has nothing to do with Unity and I've never heard of it. In fact I've never used a "JSON Schema" outside of just changing the C# code POCO itself.

    There are JSON2CSharp and CSharp2JSON type websites you can use, plus JSONLint.

    For anything specific to VisionLib, I recommend you start on their forums.
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    4,199
  4. Mazdamundi

    Mazdamundi

    Joined:
    Dec 5, 2017
    Posts:
    8
    Thanks for your reply.

    VisionLib has a SDK for Unity, that's why I talk about it but Unfortunaly they don't have a forum /:.

    I have script this but it's not working great :

    I have a little problem, I would like to read a txt file, modify it and save it. So I use the SimpleJSON from @Bunny83.

    Read it is ok,
    Modify is ok
    Save it is ok

    So where is the problem ? When I save it Unity doesn't include my modification.

    Here my txt file :

    Code (csharp):
    1.  {
    2.       "type": "VisionLibTrackerConfig",
    3.       "version": 1,
    4.       "meta": {
    5.         "author": "",
    6.         "description": "",
    7.         "name": "",
    8.         "vlTrackingConfig": {},
    9.         "uuid": "2bcc7e7f-6220-4a6a-af5b-166b7afd17c6",
    10.         "timeCreated": 1601545823,
    11.         "timeChanged": 1601545825
    12.       },
    13.       "tracker": {
    14.         "type": "modelTracker",
    15.         "version": 1,
    16.         "parameters": {
    17.           "minInlierRatioInit": 0.6,
    18.           "minInlierRatioTracking": 0.42,
    19.           "laplaceThreshold": 1,
    20.           "normalThreshold": 2.5,
    21.           "lineGradientThreshold": 40,
    22.           "lineSearchLengthInitRelative": 0.04125,
    23.           "lineSearchLengthTrackingRelative": 0.03125,
    24.           "keyFrameDistance": 50,
    25.           "metric": "cm",
    26.           "showLineModel": true,
    27.           "models": [
    28.             {
    29.               "name": "Gaio_ModelTarget_V01",
    30.               "uri": "project_dir:Gaio_ModelTarget_V01.fbx"
    31.             }
    32.           ],
    33.           "initPose": {
    34.             "t": [
    35.               -7.307137489318848,
    36.               37.529937744140625,
    37.               299.53997802734375
    38.             ],
    39.             "q": [
    40.               0.995162308216095,
    41.               0.09824426472187042,
    42.               0,
    43.               0
    44.             ]
    45.           }
    46.         }
    47.       }
    48.     }
    Here my code
    Code (csharp):
    1.  
    2.         using System.Collections;
    3.         using System.Collections.Generic;
    4.         using UnityEngine;
    5.         using System.IO;
    6.         using SimpleJSON;
    7.      
    8.     public class ChangeJsonValue : MonoBehaviour
    9.     {
    10.         string path;
    11.         string jsonString;
    12.  
    13.         // Start is called before the first frame update
    14.         void Start()
    15.         {
    16.             path = Application.dataPath + "/StreamingAssets/VisionLib/Gaio/test.vl";
    17.             jsonString = File.ReadAllText(path);
    18.  
    19.             JSONNode data = JSON.Parse(jsonString);
    20.          
    21.             foreach (JSONNode record in data["tracker"])
    22.             {
    23.                 record["showLineModel"] = false;
    24.                 Debug.Log(record["showLineModel"]);
    25.             }
    26.  
    27.          
    28.         }
    29.  
    30.         private void Update()
    31.         {
    32.             File.WriteAllText(Application.dataPath + "/StreamingAssets/VisionLib/Gaio/sdsd.vl", jsonString);
    33.         }
    34.     }


    In Unity Console the changement is ok, but when I save my file the value is not edit. I don't understand why !

    If someone can help me, that's will be great !! Thanks
     
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    315
    You did not modify anything ^^. This line parses your json text into a proper object tree:

    Code (CSharp):
    1. JSONNode data = JSON.Parse(jsonString);
    You then modify the object representation but you never convert them back to json text. You have to call ToString(2) on the root node to convert the objects back to json. (the 2 is the indention depth. Without any depth it would convert to compact json without newline characters)

    Note that your foreach loop doesn't make much sense. You iterate over all "values" in the "tracker" object and try to set the field "showLineModel" to true on all of them. So you would loop over the value "modelTracker" the value "1" and your parameters object (which is what you're interested in). You should do something like this:

    Code (CSharp):
    1.     public class ChangeJsonValue : MonoBehaviour
    2.     {
    3.         void Start()
    4.         {
    5.             string path = Application.dataPath + "/StreamingAssets/VisionLib/Gaio/test.vl";
    6.             string jsonString = File.ReadAllText(path);
    7.            
    8.             JSONNode data = JSON.Parse(jsonString);
    9.             data["tracker"]["parameters"]["showLineModel"] = false;
    10.            
    11.             // convert back to string
    12.             jsonString = data.ToString(2);
    13.             File.WriteAllText(Application.dataPath + "/StreamingAssets/VisionLib/Gaio/sdsd.vl", jsonString);
    14.         }
    15.     }
    16.  
     
    PraetorBlue likes this.
  6. Mazdamundi

    Mazdamundi

    Joined:
    Dec 5, 2017
    Posts:
    8
    Hi,

    I know the both but didn't know how to fix it. It's work great, thanks a lot !!

    Have a good day and thank again !
     
unityunity