Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QNTM-5415 Expose ToJson() and OpenJsonFileFromPath() in DynamoCore as Public methods #9161

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/DynamoCore/Graph/Workspaces/SerializationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static class SerializationExtensions
/// Save a Workspace to json.
/// </summary>
/// <returns>A string representing the serialized WorkspaceModel.</returns>
internal static string ToJson(this WorkspaceModel workspace, EngineController engine)
public static string ToJson(this WorkspaceModel workspace, EngineController engine)
{
var logger = engine != null ? engine.AsLogger() : null;

Expand Down
6 changes: 3 additions & 3 deletions src/DynamoCore/Models/DynamoModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1444,7 +1444,7 @@ public void OpenFileFromPath(string filePath, bool forceManualExecutionMode = fa

if (DynamoUtilities.PathHelper.isValidJson(filePath, out fileContents, out ex))
{
OpenJsonFileFromPath(fileContents, filePath, forceManualExecutionMode);
OpenJsonFileFromPath(fileContents, forceManualExecutionMode, filePath);
return;
}
else
Expand Down Expand Up @@ -1486,11 +1486,11 @@ static private DynamoPreferencesData DynamoPreferencesDataFromJson(string json)
/// Opens a Dynamo workspace from a path to an JSON file on disk.
/// </summary>
/// <param name="fileContents">Json file contents</param>
/// <param name="filePath">Path to file</param>
/// <param name="forceManualExecutionMode">Set this to true to discard
/// <param name="filePath">Optional Path to file on disk. This is used to register dyf files in the same directory</param>
/// execution mode specified in the file and set manual mode</param>
/// <returns>True if workspace was opened successfully</returns>
private bool OpenJsonFileFromPath(string fileContents, string filePath, bool forceManualExecutionMode)
public bool OpenJsonFileFromPath(string fileContents, bool forceManualExecutionMode, string filePath = null)
{
try
{
Expand Down
26 changes: 26 additions & 0 deletions src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
using System.Windows;
using System.Windows.Forms;
using System.Windows.Threading;
using Newtonsoft.Json.Linq;
using ISelectable = Dynamo.Selection.ISelectable;

namespace Dynamo.ViewModels
Expand Down Expand Up @@ -1482,6 +1483,31 @@ internal bool ShowSaveDialogIfNeededAndSave(WorkspaceModel workspace)
return false;
}

/// <summary>
/// Provides the combined model and view json data as is found in a Dynamo file.
/// </summary>
/// <returns></returns>
public string GetCurrentWorkspaceModelAndViewJson()
{
try
{
// Serialize the workspace.
var json = CurrentSpaceViewModel.Model.ToJson(Model.EngineController);
var json_parsed = JObject.Parse(json);

// Add the View data to the json.
var jo = CurrentSpaceViewModel.AddViewBlockToJSON(json_parsed);

return jo.ToString();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message + " : " + ex.StackTrace);
throw (ex);
}
}


internal bool CanVisibilityBeToggled(object parameters)
{
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static class SerializationExtensions
/// </summary>
/// <param name="viewModel"></param>
/// <returns>A JSON string representing the WorkspaceViewModel</returns>
internal static string ToJson(this WorkspaceViewModel viewModel)
public static string ToJson(this WorkspaceViewModel viewModel)
{
var settings = new JsonSerializerSettings
{
Expand Down
2 changes: 1 addition & 1 deletion src/DynamoCoreWpf/ViewModels/Core/WorkspaceViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ internal void Save(string filePath, bool isBackup = false, EngineController engi
/// This function appends view block to the model json
/// </summary>
/// <param name="modelData">Workspace Model data in JSON format</param>
private JObject AddViewBlockToJSON(JObject modelData)
internal JObject AddViewBlockToJSON(JObject modelData)
{
var token = JToken.Parse(this.ToJson());
modelData.Add("View", token);
Expand Down
57 changes: 57 additions & 0 deletions test/DynamoCoreWpfTests/WorkspaceSaving.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Dynamo.Graph.Connectors;
using Dynamo.Graph.Nodes;
using Dynamo.Wpf;
using Newtonsoft.Json.Linq;

namespace Dynamo.Tests
{
Expand Down Expand Up @@ -50,6 +51,62 @@ public void HomeWorkspaceCanSaveAsNewFile()
Assert.IsTrue(File.Exists(newPath));
}

[Test]
[Category("UnitTests")]
public void EnsureGetCurrentWorkspaceModelAndViewJsonReturnsPropperJson()
{
//openFile
string openPath = Path.Combine(TestDirectory, @"core\isInput_isOutput\IsInput.dyn");
ViewModel.OpenCommand.Execute(openPath);

//Get file json contents
var fileContents = File.ReadAllText(openPath);
var fileObj = Newtonsoft.Json.Linq.JToken.Parse(fileContents);

//Get json from DynamoViewModel
var json = ViewModel.GetCurrentWorkspaceModelAndViewJson();
var jsonObj = Newtonsoft.Json.Linq.JToken.Parse(json);

//Check file json and json provided are the same
//Exclude "View" property as "View.Dynamo.Version" value may vary
foreach (var token in fileObj.Children())
{
var prop = token as JProperty;

if (prop.Name != "View")
{
Assert.AreEqual(prop.Value, jsonObj[prop.Name]);
}
}

//Check file "View" property vs json "View" provided
//Exclude "Dynamo" property as "Dynamo.Version" value may vary
foreach (var token in fileObj["View"].Children())
{
var prop = token as JProperty;

if (prop.Name != "Dynamo")
{
Assert.AreEqual(prop.Value, jsonObj["View"][prop.Name]);
}
}

//Check file "Dynamo" property vs json "Dynamo" provided
//Exclude "Version" property as "Version" value may vary
foreach (var token in fileObj["View"]["Dynamo"].Children())
{
var prop = token as JProperty;

if (prop.Name != "Version")
{
Assert.AreEqual(prop.Value, jsonObj["View"]["Dynamo"][prop.Name]);
}
}

//Verify "Version" poperty included
Assert.NotNull(jsonObj["View"]["Dynamo"]["Version"]);
}

[Test]
[Category("UnitTests")]
public void CleanWorkbenchClearsUndoStack()
Expand Down