on Android, using com.google.gson:gson:2.8.5,
when passing a josnString and when the json is large (noticed for example when string length is 669304), it got
com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException:
Unterminated string at line 2902 column 564904 path $.items.result[10].quoted_status.full_text
or different errors:
the json has array of sub json strings, structured like,
{
"items": {
"result": [
{subJsonString...},
{subJsonString...},
...
]
}
}
and the json string is stored in the res/raw/jsonstring.txt, and read in at runtime before give to Gson fro deserialization.
if reduce the sub json elements in the array (i.e. 10 items or less in the array) it works fine, and the individual json elements string all working fine. But when the array has more items it starts to throw.
update: it seems is problem from reading the json string from res/raw after further look, the log shows the output json string read from the res/raw/jsonString.txt are corrupted, when there are more items. Here after adding the 11th sub item into the json string array, it shows 000000000... at certain point (the 11th item who encountered issue are just a copy from the 1st item, so the string should be fine)
here is the code how the raw string is read, how it logs it
val jsonStr = getJsonFromFile(context, "res/raw/jsoninraw.txt")
returnedModel = gson.fromJson<T>(jsonStr, dataClassType)
......
fun getJsonFromFile(context: Context?, fileName: String): String? {
val json: String
try {
val inputStream: InputStream = context.getAssets().open(fileName)
val size = inputStream.available()
val buffer = ByteArray(size)
inputStream.use { it.read(buffer) }
json = String(buffer)
} catch (ioException: IOException) {
ioException.printStackTrace()
return null
}
return json
.also {
logString(it)
}
}
fun logString(jsonStr: String) {
val chunkSize = 512
var i = 0
while (i < jsonStr.length) {
Log.e("+++", jsonStr.substring(i, Math.min(jsonStr.length, i + chunkSize)))
i += chunkSize
}
}
what are the better way to deserialize the json string into model?