LitJson doesnt write empty arrays
chris-rogers-anki opened this issue · 2 comments
It seems this has been a problem since 2014.
LitJson does not handle empty lists or arrays as a new JsonData object or as an addition to an existing JsonData object.
These 4 tests in Unity all fail.
using UnityEngine;
using LitJson;
using UnityEditor;
using System.Collections.Generic;
using System.IO;
namespace DefaultNamespace
{
public class TestLitJsonArray : MonoBehaviour
{
[MenuItem("Window/TestLitJsonList")]
static void doTest1()
{
List strings = new List();
JsonData jd = new JsonData();
jd.Add(strings);
File.WriteAllText(Application.streamingAssetsPath+("/arrayTest1.json"),jd.ToJson());
}
[MenuItem("Window/TestLitJsonList2")]
static void doTest2()
{
List<string> strings = new List<string>();
JsonData jd = new JsonData(strings);
File.WriteAllText(Application.streamingAssetsPath+("/arrayTest2.json"),jd.ToJson());
}
[MenuItem("Window/TestLitJsonArray")]
static void doTest3()
{
string[] strings2 = new string[1]; // Im trying to write a 0 length array, it errors if this is 1 or 0
JsonData jd = new JsonData();
jd.Add(strings2);
File.WriteAllText(Application.streamingAssetsPath+("/arrayTest3.json"),jd.ToJson());
}
[MenuItem("Window/TestLitJsonArray2")]
static void doTest4()
{
string[] strings2 = new string[1];
JsonData jd = new JsonData(strings2);
File.WriteAllText(Application.streamingAssetsPath+("/arrayTest4.json"), jd.ToJson());
}
}
}
error is
Unable to wrap ... with JsonData
here's a real world example of the output:
], "connections": "camerapositionX" : "0", "camerapositionY": "0",
connections should be [] but i'd be ok with null but its blank.