Announcement

Collapse
No announcement yet.

JSON Stringify & Parse

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • JSON Stringify & Parse

    Hello, I've tried to save & load to/from localStorage some objects via JSON. I've done few objects, but I cannot get one of them back, JSON.Parse returns null. I can save it, but I can't load it.

    - JSON is valid (tested at https://jsonformatter.curiousconcept.com/)
    - These two string, that I save to LocalStorage and that I load from LocalStorage are completely the same (tested using http://www.textdiff.com/)

    How I save & load it:
    - I save 3 different object, 3 different JSONs to CSV (separator: ASCII 31)
    - When I load it, I split it into array with 3 strings
    - Then, I load object from each string



    - Everything works, only the one object cannot be loaded
    - Here's JSON:
    {"saveName":"20-04-2017 20-12-27","endless":true,"campaign":false,"level":0,"campaignName":"","campaignInstr":0,"difficulty":1,"characterX":24,"characterY":41,"random":{"seedArray":[0,2071854394,2061970799,1469184134,772600498,1867864185,1290120453,496616254,2112636484,994064548,993313317,2007842945,1373445092,968833313,659809882,7305745,1894743668,798637734,395737171,612252320,169029149,2030777124,1845139261,37834444,786181383,557886747,1194140671,1129324444,575489712,1271036501,557658081,158326691,1965039977,852725297,560701037,1245059946,1582419253,586241689,1064375112,824298214,1970470580,895184682,1925992860,1919669562,633642055,1126697535,1394556248,1120050655,1524413812,1239293922,468008252,788362947,419767332,1528742769,1362575845,2134714157],"inext":39,"inextp":5}}


    - When I try to get the object back, immediatelly after stringify, it isn't null:
    if (JSON.Parse(JSON.Stringify(DataStorage.data)) == null)
                    Window.Alert("It's NULL!!!");
                else
                    Window.Alert("It isn't null"); // It isn't null
    - Here's my converting objects into JSON:
    string s = "";
                char separator = (char)31;
                s = JSON.Stringify(DataStorage.activeMap.map) + separator;
                s += JSON.Stringify(DataStorage.activeMap.mainCharacter) + separator;
                Console.WriteLine(JSON.Stringify(DataStorage.data));
                s += JSON.Stringify(DataStorage.data);
            /*    if (JSON.Parse(JSON.Stringify(DataStorage.data)) == null)
                    Window.Alert("It's NULL!!!");
                else
                    Window.Alert("It isn't null");*/
                return s;
    - Here's my loading objects from JSON:
    string[] s = data.Split((char)31);
    
                    DataStorage.activeMap = new Map(name, Map.delkaRandom, Map.vyskaRandom);
                    DataStorage.activeMap.map = JSON.Parse(s[0]) as MapObject[,];
                    DataStorage.activeMap.mainCharacter = JSON.Parse(s[1]) as Character;
                    DataStorage.data = JSON.Parse(s[2]) as DataStorage; // This is always null
                 /*   Console.WriteLine(s[2]);
                    Console.WriteLine(DataStorage.data != null);
                    Console.WriteLine((JSON.Parse(s[2].Replace(((char)31).ToString(), "")) as DataStorage) != null);*/
                    return true;

  • #2
    Hi SoptikHa – Can you share a link to a Deck.NET sample demonstrating your scenario?

    Comment


    • #3
      geoffrey.mcgill
      Yes, I was playing with it a little and I've found something interesting: Without changing type of object, it isn't null, but when I change it, it turns to null. Here's link to deck:

      ​​​​​​​https://deck.net/0b2db7ff76dca6eea50c74a6c23dbb4a

      Comment


      • #4
        Just pass a Type to Parse.

        https://deck.net/7bba688b7ad3ac656ad472159d2ff69c

        JSON.Parse<DataStorage>(s);
        In Bridge 16, we are adding enhanced support for Json serialize + deserialize, see

        https://github.com/bridgedotnet/Bridge/issues/2418

        Hope this helps.

        Comment


        • #5
          It works now, thanks :-)

          Comment

          Working...
          X