Search code examples
autodesk-forgeautodesk-designautomation

Cannot deserialize the current JSON array for Revit design automation workitem


  1. i have a json file like below as input parameters for Revit design automation:
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"
});
  1. "success" workitem except the below error message in report file:
[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.
  1. my Revit plugin C# code as below:
    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);
        }
    }
  1. i tried to fix it via below C# code:
    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; } = { };
    }
  1. but "failedInstructions" workitem w/ below error message in report file:
[07/09/2022 05:02:16] Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: o. Path '', line 1, position 1.

Solution

  • 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.