Bug - Fixed JSONException: A JSONArray text must start with '[' at 1

apologue

New member
When starting up r19417, 38 of the following occur:

Code:
Unexpected error, debug log printed.
class org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]
org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]
	at org.json.JSONTokener.syntaxError(JSONTokener.java:423)
	at org.json.JSONArray.<init>(JSONArray.java:113)
	at org.json.JSONArray.<init>(JSONArray.java:157)
	at net.sourceforge.kolmafia.persistence.ScriptManager.getJSONArray(ScriptManager.java:168)
	at net.sourceforge.kolmafia.persistence.ScriptManager.updateRepoScripts(ScriptManager.java:151)
	at net.sourceforge.kolmafia.persistence.ScriptManager.<clinit>(ScriptManager.java:138)
	at net.sourceforge.kolmafia.swingui.widget.TableCellFactory.getColumnNames(TableCellFactory.java:471)
	at net.sourceforge.kolmafia.swingui.widget.ShowDescriptionTable.<init>(ShowDescriptionTable.java:311)
	at net.sourceforge.kolmafia.swingui.widget.ShowDescriptionTable.<init>(ShowDescriptionTable.java:170)
	at net.sourceforge.kolmafia.swingui.panel.ItemTableManagePanel.<init>(ItemTableManagePanel.java:48)
	at net.sourceforge.kolmafia.swingui.panel.ItemTableManagePanel.<init>(ItemTableManagePanel.java:61)
	at net.sourceforge.kolmafia.swingui.panel.InventoryPanel.<init>(InventoryPanel.java:121)
	at net.sourceforge.kolmafia.swingui.panel.CreateSpecialPanel.<init>(CreateSpecialPanel.java:87)
	at net.sourceforge.kolmafia.swingui.ItemManageFrame.<init>(ItemManageFrame.java:216)
	at net.sourceforge.kolmafia.swingui.ItemManageFrame.<init>(ItemManageFrame.java:124)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
	at java.lang.reflect.Constructor.newInstance(Unknown Source)
	at net.sourceforge.kolmafia.CreateFrameRunnable.runConstruction(CreateFrameRunnable.java:286)
	at net.sourceforge.kolmafia.CreateFrameRunnable.createFrame(CreateFrameRunnable.java:182)
	at net.sourceforge.kolmafia.CreateFrameRunnable.run(CreateFrameRunnable.java:164)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$500(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
 

fronobulax

Developer
Staff member
Hmmm..

That happened a lot as I was moving the repository file to SourceForge and then reading, but fix was to read the file as 'raw'. That is still in place and I have not seen that since. My SVN copy of the file is also correct.

My knee jerk reaction is to have you delete svnrepo.json in the data directory and try again. It is possible that you had a comms error and the file was not completely read or there was some file corruption and the bad version was not replaced.

If it is still happening then maybe we need to know your operating system and what the first few characters of your svnrepo.json actually are.
 

xKiv

Active member
> [character 2 line 1]

Since in my copy the [ is character 1, maybe the file got clobbered by windows's propensity to prepend a BOM to text files?
 

Veracity

Developer
Staff member
Interesting. I changed the way we read that JSON file to simply load the the file into a byte array and initialize a string with it.
Works like a charm on my Mac and your Linux (if I recall correctly).

The previous way was too ugly for words. Before:

Code:
      BufferedReader reader = FileUtilities.getReader( repoFile );
      StringBuilder builder = new StringBuilder();

      try
      {
          String[] data;
          while ( ( data = FileUtilities.readData( reader ) ) != null )
          {
              for ( String s : data )
              {
                  builder.append( s );
              }
          }
      }
      finally
      {
          try
          {
              reader.close();
          }
          catch ( IOException e )
          {
              StaticEntity.printStackTrace( e );
          }
      }

      try
      {
            JSONArray jArray = new JSONArray( builder.toString() );
            return jArray;
      }
After:

Code:
      byte[] bytes = ByteBufferUtilities.read( repoFile );
      String string = StringUtilities.getEncodedString( bytes, "UTF-8" );

      try
      {
            JSONArray jArray = new JSONArray( string );
            return jArray;
      }
That technique works perfectly for loading in a raw file of HTML for "test load FILENAME", for example.
 
Last edited:

apologue

New member
The svnrepo.json file is empty. When I deleted it and started KoLmafia, I got the same errors and the file was recreated, again empty (0 bytes). Oh and this is in Windows 7.
 

fronobulax

Developer
Staff member
Well, I didn't write the reader code, at least.

If the file is zero bytes then this is exactly the error one would expect when trying to read it.

I have not had a problem on Windows 10.

Looking at the code there is a point where if the local file exists after the remote read and has zero bytes, it is supposed to be deleted. So it is interesting that a zero byte file exists.

Some of the IO does have debugging so maybe turn debugging on (it's on a menu on the RHS of the main screen but I can't check just now) try and load the script menu, triggering the error, and then turn off debugging and share the log?

I have a vague recollection of some charset issues on W7 vs. W10. Maybe we are expecting a charset that is not in 7?
 

apologue

New member
Umm... could you be more specific with "load the script menu"? Clicking any of "Scripts", "Refresh menu", "Load scripts...", "Script Manager" does not trigger the error.
 

Veracity

Developer
Staff member
Since it seems to occur when creating the Item Manager frame (why in the world?), turn on debugging when you have the Login Frame showing before it logs you in and creates other frames.
 

apologue

New member
Ok. So, I would expect this to be relevant:

Code:
Requesting: https://sourceforge.net/p/kolmafia/code/HEAD/tree/data/SVN/svnrepo.json?format=raw
0 request properties

Unexpected error, debug log printed.
class javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
	at sun.security.ssl.Alerts.getSSLException(Unknown Source)
	at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
	at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
	at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
	at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)
	at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
	at sun.security.ssl.Handshaker.processLoop(Unknown Source)
	at sun.security.ssl.Handshaker.process_record(Unknown Source)
	at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
	at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
	at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
	at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
	at sun.net.[url]www.protocol.https.HttpsClient.afterConnect(Unknown[/url] Source)
	at sun.net.[url]www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown[/url] Source)
	at sun.net.[url]www.protocol.http.HttpURLConnection.getInputStream0(Unknown[/url] Source)
	at sun.net.[url]www.protocol.http.HttpURLConnection.getInputStream(Unknown[/url] Source)
	at java.net.HttpURLConnection.getResponseCode(Unknown Source)
	at sun.net.[url]www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown[/url] Source)
	at net.sourceforge.kolmafia.utilities.FileUtilities.downloadFileToStream(FileUtilities.java:277)
	at net.sourceforge.kolmafia.utilities.FileUtilities.downloadFile(FileUtilities.java:428)
	at net.sourceforge.kolmafia.persistence.ScriptManager.updateRepoScripts(ScriptManager.java:148)
	at net.sourceforge.kolmafia.persistence.ScriptManager.<clinit>(ScriptManager.java:138)
	at net.sourceforge.kolmafia.swingui.widget.TableCellFactory.getColumnNames(TableCellFactory.java:471)
	at net.sourceforge.kolmafia.swingui.widget.ShowDescriptionTable.<init>(ShowDescriptionTable.java:311)
	at net.sourceforge.kolmafia.swingui.widget.ShowDescriptionTable.<init>(ShowDescriptionTable.java:170)
	at net.sourceforge.kolmafia.swingui.panel.ItemTableManagePanel.<init>(ItemTableManagePanel.java:48)
	at net.sourceforge.kolmafia.swingui.panel.ItemTableManagePanel.<init>(ItemTableManagePanel.java:61)
	at net.sourceforge.kolmafia.swingui.panel.InventoryPanel.<init>(InventoryPanel.java:121)
	at net.sourceforge.kolmafia.swingui.panel.CreateSpecialPanel.<init>(CreateSpecialPanel.java:87)
	at net.sourceforge.kolmafia.swingui.ItemManageFrame.<init>(ItemManageFrame.java:216)
	at net.sourceforge.kolmafia.swingui.ItemManageFrame.<init>(ItemManageFrame.java:124)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
	at java.lang.reflect.Constructor.newInstance(Unknown Source)
	at net.sourceforge.kolmafia.CreateFrameRunnable.runConstruction(CreateFrameRunnable.java:286)
	at net.sourceforge.kolmafia.CreateFrameRunnable.createFrame(CreateFrameRunnable.java:182)
	at net.sourceforge.kolmafia.CreateFrameRunnable.run(CreateFrameRunnable.java:164)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$500(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
	at sun.security.validator.PKIXValidator.doBuild(Unknown Source)
	at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)
	at sun.security.validator.Validator.validate(Unknown Source)
	at sun.security.ssl.X509TrustManagerImpl.validate(Unknown Source)
	at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source)
	at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
	... 48 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
	at sun.security.provider.certpath.SunCertPathBuilder.build(Unknown Source)
	at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)
	at java.security.cert.CertPathBuilder.build(Unknown Source)
	... 54 more
 

fronobulax

Developer
Staff member
Sorry for the delay.

The original error about JSON parsing will occur if the file is created but empty.

The second trace, with "unable to find valid certification path to requested target" indicates there was a problem trying to establish a SSL connection to SourceForge.

I have not had to resolved one of those is a very long time and my recollection is that the solution requires Java configuration and/or importing certificates. If you can load a script using the svn command in mafia there is a chance that the problem is not just a straight forward certificate problem with SourceForge.

My first suggestion would be to try again if only because the file has been changing rapidly and the issue might be transient. If that doesn't work we probably need to know your operating system (Windows 7, but which version, which patches and updates, etc.) and which version of Java,

The fact that you had a zero byte file bothers me since a quick look at the code suggests that should be detected and dealt with, so I will spend some more time investigating.
 

apologue

New member
Loading a script using the svn command works fine.
The certificate exception seems to happen every time.
This is Windows 7 Home Premium, Service Pack 1 with security updates
Java turned out to be Version 8 Update 60, which is pretty old, so I updated it to 221. The update fixed the certificate issue. The empty svnrepo.json file still raised exceptions and was not automatically deleted but after manually deleting it, it was downloaded successfully, putting an end to the exceptions.
 

fronobulax

Developer
Staff member
Loading a script using the svn command works fine.
The certificate exception seems to happen every time.
This is Windows 7 Home Premium, Service Pack 1 with security updates
Java turned out to be Version 8 Update 60, which is pretty old, so I updated it to 221. The update fixed the certificate issue. The empty svnrepo.json file still raised exceptions and was not automatically deleted but after manually deleting it, it was downloaded successfully, putting an end to the exceptions.

Thank you. There is a minor bug outstanding in that KoLmafia should not try and get JSON data out of a zero byte file, and I will look at that eventually. I'm puzzled that this triggered the certificate issue, while other things did not, but there are several ways KoLmafia deals with https: so it may be the recent change introduced a different path. I am glad the Java update handled things. I had a vague recollection that Java itself had been a player in similar problems in the past but my memory is not what it needs to be.
 
Top