Skip to content

Commit

Permalink
prioritize json over xaml (#14477)
Browse files Browse the repository at this point in the history
Co-authored-by: Bogdan Zavu <[email protected]>
  • Loading branch information
BogdanZavu and zavub authored Oct 10, 2023
1 parent e1aeeac commit 01ef5fc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 53 deletions.
24 changes: 12 additions & 12 deletions src/DynamoCore/Core/CustomNodeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
{
Expand All @@ -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;
Expand Down
66 changes: 30 additions & 36 deletions src/DynamoCore/Models/DynamoModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1941,11 +1941,12 @@ public void OpenFileFromJson(string fileContents, bool forceManualExecutionMode
/// execution mode specified in the file and set manual mode</param>
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
Expand All @@ -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;
}
}
}

/// <summary>
Expand All @@ -1982,11 +1979,12 @@ public void OpenFileFromPath(string filePath, bool forceManualExecutionMode = fa
/// <param name="forceManualExecutionMode"></param>
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
Expand All @@ -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;
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/Tools/NodeDocumentationMarkdownGenerator/MdFileInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Xml;
using Dynamo.Graph.Workspaces;
using Dynamo.Logging;
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 01ef5fc

Please sign in to comment.