Changing the way my program accepts files will create this error from GSON: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $?
Changing the way my program accepts files will create this error from GSON: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $?
There's been a lot of questions asked about this topic but this question is different because the way I define a certain variable is what determines whether or not this error is thrown.
Basically, I have the below JSON file:
[
"appiumVersion":"1.8.1",
"buildTag": "build-0",
"newCommandTimeout": "30",
"deviceName":"Samsung Galaxy S9 WQHD GoogleAPI Emulator",
"deviceOrientation":"portrait",
"browserName":"",
"platformName":"Android",
"platformVersion":"7.1",
"app":"sauce-storage:ApiDemos-debug.apk"
,
"appiumVersion":"1.8.1",
"buildTag": "build-0",
"newCommandTimeout": "30",
"deviceName":"Samsung Galaxy S9 WQHD GoogleAPI Emulator",
"deviceOrientation":"portrait",
"browserName":"",
"platformName":"Android",
"platformVersion":"7.0",
"app":"sauce-storage:ApiDemos-debug.apk"
]
I am using GSON to parse it and this is the code for it:
File capsJsonFile = new File(capsFilePath);
Reader capsReader = null;
try
capsReader = new FileReader(capsJsonFile);
catch (FileNotFoundException e)
e.printStackTrace();
Gson gsonCapsJsonArr = new Gson();
JsonObject capsJsonArr = gsonCapsJsonArr.fromJson(capsReader, JsonObject.class);
Then, I have another program that calls the above code:
capsFile = "C:\Users\exue\IdeaProjects\j-mobile\tests\src\test\resources\capsJsonArray.json";
AutomationFramework.setCapsFilePath(capsFile);
And this code sets the "capsFilePath" variable equal to "capsFile".
What's weird is that if I write the program like this I have no error. But, I want to pass this capsFile string in as a system property through the maven-surefire-plugin that I am using to run this program, like so:
capsFile = System.getProperty("capsFile");
AutomationFramework.setCapsFilePath(capsFile);
I configured the plugin to look like so:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<parallel>classes</parallel>
<threadCount>4</threadCount>
<redirectTestOutputToFile>false</redirectTestOutputToFile>
<systemProperties>
<property>
<property>
<name>capsFile</name>
<value>C:\Users\exue\IdeaProjects\j-mobile\tests\src\test\resources\cfgJsonFile.json</value>
</property>
</systemProperties>
</configuration>
</plugin>
Having written it like this, taking the file as a system property, I now get this error:
[Utils] [ERROR] [Error] com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
at com.google.gson.Gson.fromJson(Gson.java:939)
at com.google.gson.Gson.fromJson(Gson.java:865)
at com.transunion.qecop.automationframework.AutomationFramework.mobileDeviceDataProvider(AutomationFramework.java:208)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:74)
at org.testng.internal.MethodInvocationHelper.invokeMethodNoCheckedException(MethodInvocationHelper.java:45)
at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:131)
at org.testng.internal.Parameters.handleParameters(Parameters.java:706)
at org.testng.internal.ParameterHandler.handleParameters(ParameterHandler.java:49)
at org.testng.internal.ParameterHandler.createParameters(ParameterHandler.java:37)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:924)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350)
at com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:70)
at com.google.gson.Gson.fromJson(Gson.java:927)
... 19 more
But if I just hard-code the string, then this error does not occur. How could the way in which I feed in the file possibly cause this error?
1 Answer
1
The reason for this problem is that you are referring to two different JSON's
the one you are passing in string is capsJsonArray.json
whereas the one you are passing in maven is cfgJsonFile.json
I think the problem lies in json being refereed as it may be incorrect
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.