const data = {
ModelId: "dXJuOmFkc2sud2lwcHJvZDpmcy5maWxlOnZmLjNSTzhMWmtUVGVHM3p1M1FoNjRGM3c_dmVyc2lvbj0x",
ElementIds: [{
"UniqueId": "ca5762b5-0f46-4a2f-8599-1d1a5dd19a81-00024b8c"
}, {
"UniqueId": "91e18c07-1bae-4949-a093-e0a4552bb478-000241ea"
}]
};
var file = new File([data], "inputParameters.json", {
type: "application/json"
});
[07/09/2022 03:36:41] Exception when parsing json file: Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ProjectSchedule.InputCache' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
public static bool ImportFromCache(Application rvtApp, Document doc)
{
InputCache inputCache = InputCache.Parse("inputParameters.json");
}
internal class InputCache
{
public string ModelId { get; set; } = "";
public string[] ElementIds { get; set; } = { };
static public InputCache Parse(string jsonPath)
{
if (!File.Exists(jsonPath)) return new InputCache { ModelId = "", ElementIds = { } };
string jsonContents = File.ReadAllText(jsonPath);
return JsonConvert.DeserializeObject<InputCache>(jsonContents);
}
}
public static bool ImportFromCache(Application rvtApp, Document doc)
{
string jsonContents = File.ReadAllText("cache.json");
var inputCache = JsonConvert.DeserializeObject<List<InputCache>>(jsonContents);
}
internal class InputCache
{
public string ModelId { get; set; } = "";
public string[] ElementIds { get; set; } = { };
}
[07/09/2022 05:02:16] Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: o. Path '', line 1, position 1.
There are 2 problems with your example.
First: You are saving the json data as an array but trying to deserialize as an object:
var file = new File([data], "inputParameters.json", {
type: "application/json"
});
This explains the error you have:
Exception when parsing json file: Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ProjectSchedule.InputCache' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this, you can change your js code to not convert your data into an array or account for an array in your appbundle code.
A possible solution in js code:
var file = new File([JSON.stringify(data)], "inputParameters.json", {
type: "text/plain"
});
Or change in appbundle code (removes the redundant square brackets):
string fileContents = File.ReadAllText("cache.json");
string jsonContents = fileContents.Substring(1, fileContents.Length-2);
var inputCache = JsonConvert.DeserializeObject<List<InputCache>>(jsonContents);
The second problem that you have not yet encountered with deserialization is explained in an earlier answer.