From 802ff3572d4794347119ddc035b8a0208ed7fa5f Mon Sep 17 00:00:00 2001 From: Safeer Hussain Date: Thu, 23 May 2024 22:43:06 +0100 Subject: [PATCH] Implement robust JSON isolation between .NET console app and Node.js script (fixes #61 #70) --- index.js | 13 ++++++++++++- lib/csharp-models-to-json/Program.cs | 9 ++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 04188e9..ef5c29a 100755 --- a/index.js +++ b/index.js @@ -61,7 +61,18 @@ dotnetProcess.stdout.on('end', () => { let json; try { - json = JSON.parse(stdout); + // Extract the JSON content between the markers + const startMarker = '<<<<<>>>>>'; + const endMarker = '<<<<<>>>>>'; + const startIndex = stdout.indexOf(startMarker); + const endIndex = stdout.indexOf(endMarker); + + if (startIndex !== -1 && endIndex !== -1 && endIndex > startIndex) { + const jsonString = stdout.substring(startIndex + startMarker.length, endIndex).trim(); + json = JSON.parse(jsonString); + } else { + throw new Error('JSON markers not found or invalid order of markers.'); + } } catch (error) { return console.error([ 'The output from `csharp-models-to-json` contains invalid JSON.', diff --git a/lib/csharp-models-to-json/Program.cs b/lib/csharp-models-to-json/Program.cs index e77fa54..023a5c7 100644 --- a/lib/csharp-models-to-json/Program.cs +++ b/lib/csharp-models-to-json/Program.cs @@ -5,6 +5,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.Extensions.Configuration; using Ganss.IO; +using System.Text; namespace CSharpModelsToJson { @@ -36,7 +37,13 @@ static void Main(string[] args) } string json = JsonSerializer.Serialize(files); - System.Console.WriteLine(json); + + var sb = new StringBuilder(); + sb.AppendLine("<<<<<>>>>>"); + sb.AppendLine(json); + sb.AppendLine("<<<<<>>>>>"); + + System.Console.WriteLine(sb.ToString()); } static List getFileNames(List includes, List excludes) {