Save JSON files.. Loading Crash!? (Fixed!)

Monkey Forums/Monkey Programming/Save JSON files.. Loading Crash!? (Fixed!)

therevills(Posted 2016) [#1]
[edit: I was using v85a, this has been fixed in later versions of MX (tested on v86e), it was working on v79 though!]

Hi All,

I'm saving and loading IAP on Android using the following methods:

Load:
	Method LoadPurchases:Void()
		#If TARGET="android" or TARGET="ios"
			Local f:filestream.FileStream = filestream.FileStream.Open("monkey://internal/.purchases", "r")
			If ( Not f) Return
			Local json:String = f.ReadString()
			Print("LoadPurchases: Json = " + json)
			_purchases = New JsonObject(json)
			If _purchases.GetBool(NON_CONSUMABLES[REMOVE_ADS], False) = True
				disableAds = True
			End
			f.Close()
		#End
		
	End


Save:
  
	Method SavePurchases:Void()
		Local f:filestream.FileStream = filestream.FileStream.Open("monkey://internal/.purchases", "w")
		If ( Not f) Error "Unable to save purchases"
		Local json:String = _purchases.ToJson()
		Print("SavePurchases: Json = " + json)
		f.WriteString(json)
		Print("Finished writing string...")
		f.Close()
		Print("Closed the file")
	End


Within the output of SavePurchases:

I/[Monkey](15278): SavePurchases: Json = {"coins1000":1}

I/[Monkey](15278): Finished writing string...

I/[Monkey](15278): Closed the file

I/[Monkey](15278): Bought Coins...


All good... but when loading I get this:

I/[Monkey](10944): LoadPurchases: Json = {"coins1000

--------- beginning of crash

E/AndroidRuntime(10944): FATAL EXCEPTION: GLThread 14901

E/AndroidRuntime(10944): Process: com.therevillsgames.csusatripeaks, PID: 10944

E/AndroidRuntime(10944): com.therevillsgames.csusatripeaks.c_JsonError: Uncaught Monkey Exception


Because the JSON is invalid... but I dont know why as the Save outputs valid JSON :(

The default encoding for write/read string is utf8.

Any ideas?

Cheers,
Steve


therevills(Posted 2016) [#2]
And heres the log when saving/loading a boolean:

I/[Monkey]( 8401): BuyProductComplete, result = 0

I/[Monkey]( 8401): SavePurchases: Json = {"removeads":true}

I/[Monkey]( 8401): Finished writing string...

I/[Monkey]( 8401): Closed the file

I/[Monkey]( 8401): Bought RemoveAds...


I/[Monkey]( 9872): LoadPurchases: Json = {"removeads":t

--------- beginning of crash

E/AndroidRuntime( 9872): FATAL EXCEPTION: GLThread 15386

E/AndroidRuntime( 9872): Process: com.therevillsgames.csusatripeaks, PID: 9872

E/AndroidRuntime( 9872): com.therevillsgames.csusatripeaks.c_JsonError: Uncaught Monkey Exception



therevills(Posted 2016) [#3]
Runnable example code:




Danilo(Posted 2016) [#4]
Can you check the file ".purchases“? Is the string correctly written as UTF8?
- If not, it’s probably a problem in the UTF8 encoder -> DataBuffer.PokeString()
- If yes, it may be a problem in the UTF8 decoder -> DataBuffer.PeekString()

As a test for a workaround, can you try to use f.WriteString(json, "ascii“) and f.ReadString("ascii“) ?


therevills(Posted 2016) [#5]
Hey Danilo, I've just tried the ascii encoding and I get the same issue.


Error log:
I/[Monkey](21435): Saving...ascii

I/[Monkey](21435): SavePurchases: Json = {"removeAds":true}

I/[Monkey](21435): Finished writing string...

I/[Monkey](21435): Closed the file

E/MotoNetwCtrlr( 1359): getDataController: No data sim selected

E/MotoNetwCtrlr( 1359): getDataController: No data sim selected

I/[Monkey](21435): Loading...ascii

I/[Monkey](21435): LoadPurchases: Json = {"removeAds":t

I/[Monkey](21435): Monkey Runtime Error : Uncaught Monkey Exception


Do you know where the file is located?


therevills(Posted 2016) [#6]
GHAHAHAHAHAAA!!!!!AARRGHGHGHHG!!!

I was using Monkey v85a... so I thought I would try the latest version 86e and it works!!!!

The last time I messed with IAP was back on v79d.


Danilo(Posted 2016) [#7]
Nice. Happy Easter! :)


therevills(Posted 2016) [#8]
Thanks Danilo, you too :)


marksibly(Posted 2016) [#9]
Yay!