From 01ef5fc280b77b3099dc5d0ee2dd0f14ebcd460f Mon Sep 17 00:00:00 2001 From: Bogdan Zavu Date: Tue, 10 Oct 2023 15:28:28 -0400 Subject: [PATCH] prioritize json over xaml (#14477) Co-authored-by: Bogdan Zavu --- src/DynamoCore/Core/CustomNodeManager.cs | 24 +++---- src/DynamoCore/Models/DynamoModel.cs | 66 +++++++++---------- .../MdFileInfo.cs | 10 +-- 3 files changed, 47 insertions(+), 53 deletions(-) diff --git a/src/DynamoCore/Core/CustomNodeManager.cs b/src/DynamoCore/Core/CustomNodeManager.cs index 30928003aeb..8dabe1ec45b 100644 --- a/src/DynamoCore/Core/CustomNodeManager.cs +++ b/src/DynamoCore/Core/CustomNodeManager.cs @@ -684,18 +684,18 @@ internal bool TryGetInfoFromPath(string path, bool isTestMode, out CustomNodeInf Exception ex; try { - if (DynamoUtilities.PathHelper.isValidXML(path, out xmlDoc, out ex)) + if (DynamoUtilities.PathHelper.isValidJson(path, out jsonDoc, out ex)) { - if (!WorkspaceInfo.FromXmlDocument(xmlDoc, path, isTestMode, false, AsLogger(), out header)) + if (!WorkspaceInfo.FromJsonDocument(jsonDoc, path, isTestMode, false, AsLogger(), out header)) { Log(String.Format(Properties.Resources.FailedToLoadHeader, path)); info = null; return false; } } - else if (DynamoUtilities.PathHelper.isValidJson(path, out jsonDoc, out ex)) + else if (DynamoUtilities.PathHelper.isValidXML(path, out xmlDoc, out ex)) { - if (!WorkspaceInfo.FromJsonDocument(jsonDoc, path, isTestMode, false, AsLogger(), out header)) + if (!WorkspaceInfo.FromXmlDocument(xmlDoc, path, isTestMode, false, AsLogger(), out header)) { Log(String.Format(Properties.Resources.FailedToLoadHeader, path)); info = null; @@ -854,7 +854,14 @@ private bool InitializeCustomNode( XmlDocument xmlDoc; string strInput; Exception ex; - if (DynamoUtilities.PathHelper.isValidXML(path, out xmlDoc, out ex)) + + if (DynamoUtilities.PathHelper.isValidJson(path, out strInput, out ex)) + { + WorkspaceInfo.FromJsonDocument(strInput, path, isTestMode, false, AsLogger(), out info); + info.ID = functionId.ToString(); + return InitializeCustomNode(info, null, out workspace); + } + else if (DynamoUtilities.PathHelper.isValidXML(path, out xmlDoc, out ex)) { if (WorkspaceInfo.FromXmlDocument(xmlDoc, path, isTestMode, false, AsLogger(), out info)) { @@ -865,13 +872,6 @@ private bool InitializeCustomNode( } } } - else if (DynamoUtilities.PathHelper.isValidJson(path, out strInput, out ex)) - { - // TODO: Skip Json migration for now - WorkspaceInfo.FromJsonDocument(strInput, path, isTestMode, false, AsLogger(), out info); - info.ID = functionId.ToString(); - return InitializeCustomNode(info, null, out workspace); - } else throw ex; Log(string.Format(Properties.Resources.CustomNodeCouldNotBeInitialized, customNodeInfo.Name)); workspace = null; diff --git a/src/DynamoCore/Models/DynamoModel.cs b/src/DynamoCore/Models/DynamoModel.cs index 87fd50b140b..7617d781f8d 100644 --- a/src/DynamoCore/Models/DynamoModel.cs +++ b/src/DynamoCore/Models/DynamoModel.cs @@ -1941,11 +1941,12 @@ public void OpenFileFromJson(string fileContents, bool forceManualExecutionMode /// execution mode specified in the file and set manual mode public void OpenFileFromPath(string filePath, bool forceManualExecutionMode = false) { - XmlDocument xmlDoc; - Exception ex; - if (DynamoUtilities.PathHelper.isValidXML(filePath, out xmlDoc, out ex)) + + Exception ex; + string fileContents; + if (DynamoUtilities.PathHelper.isValidJson(filePath, out fileContents, out ex)) { - OpenXmlFileFromPath(xmlDoc, filePath, forceManualExecutionMode); + OpenJsonFileFromPath(fileContents, filePath, forceManualExecutionMode); return; } else @@ -1955,24 +1956,20 @@ public void OpenFileFromPath(string filePath, bool forceManualExecutionMode = fa { throw ex; } - if (ex is System.Xml.XmlException) - { - // XML opening failure can indicate that this file is corrupted XML or Json - string fileContents; - if (DynamoUtilities.PathHelper.isValidJson(filePath, out fileContents, out ex)) - { - OpenJsonFileFromPath(fileContents, filePath, forceManualExecutionMode); - return; - } - else - { - // When Json opening also failed, either this file is corrupted or there - // are other kind of failures related to Json de-serialization - throw ex; - } + XmlDocument xmlDoc; + + // When Json opening failed, either this file is corrupted or file might be XML + if (ex is JsonReaderException && DynamoUtilities.PathHelper.isValidXML(filePath, out xmlDoc, out ex)) + { + OpenXmlFileFromPath(xmlDoc, filePath, forceManualExecutionMode); + return; } - } + else + { + throw ex; + } + } } /// @@ -1982,11 +1979,12 @@ public void OpenFileFromPath(string filePath, bool forceManualExecutionMode = fa /// public void InsertFileFromPath(string filePath, bool forceManualExecutionMode = false) { - XmlDocument xmlDoc; Exception ex; - if (DynamoUtilities.PathHelper.isValidXML(filePath, out xmlDoc, out ex)) + string fileContents; + + if (DynamoUtilities.PathHelper.isValidJson(filePath, out fileContents, out ex)) { - InsertXmlFileFromPath(xmlDoc, filePath, forceManualExecutionMode); + InsertJsonFileFromPath(fileContents, filePath, forceManualExecutionMode); return; } else @@ -1996,24 +1994,20 @@ public void InsertFileFromPath(string filePath, bool forceManualExecutionMode = { throw ex; } - if (ex is System.Xml.XmlException) - { - // XML opening failure can indicate that this file is corrupted XML or Json - string fileContents; - if (DynamoUtilities.PathHelper.isValidJson(filePath, out fileContents, out ex)) + if (ex is JsonReaderException) + { + XmlDocument xmlDoc; + if (DynamoUtilities.PathHelper.isValidXML(filePath, out xmlDoc, out ex)) { - InsertJsonFileFromPath(fileContents, filePath, forceManualExecutionMode); + InsertXmlFileFromPath(xmlDoc, filePath, forceManualExecutionMode); return; } - else - { - // When Json opening also failed, either this file is corrupted or there - // are other kind of failures related to Json de-serialization - throw ex; - } } - + else + { + throw ex; + } } } diff --git a/src/Tools/NodeDocumentationMarkdownGenerator/MdFileInfo.cs b/src/Tools/NodeDocumentationMarkdownGenerator/MdFileInfo.cs index 68b7c82f230..3e0bc64a1ef 100644 --- a/src/Tools/NodeDocumentationMarkdownGenerator/MdFileInfo.cs +++ b/src/Tools/NodeDocumentationMarkdownGenerator/MdFileInfo.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Xml; using Dynamo.Graph.Workspaces; using Dynamo.Logging; @@ -73,13 +73,13 @@ internal static MdFileInfo FromCustomNode(string path) WorkspaceInfo header = null; ILogger log = new DummyConsoleLogger(); - if (DynamoUtilities.PathHelper.isValidXML(path, out XmlDocument xmlDoc, out Exception ex)) + if (DynamoUtilities.PathHelper.isValidJson(path, out string jsonDoc, out Exception ex)) { - WorkspaceInfo.FromXmlDocument(xmlDoc, path, true, false, log, out header); + WorkspaceInfo.FromJsonDocument(jsonDoc, path, true, false, log, out header); } - else if (DynamoUtilities.PathHelper.isValidJson(path, out string jsonDoc, out ex)) + else if (DynamoUtilities.PathHelper.isValidXML(path, out XmlDocument xmlDoc, out ex)) { - WorkspaceInfo.FromJsonDocument(jsonDoc, path, true, false, log, out header); + WorkspaceInfo.FromXmlDocument(xmlDoc, path, true, false, log, out header); } else throw ex;