Skip to content

Commit

Permalink
Implement robust JSON isolation between .NET console app and Node.js …
Browse files Browse the repository at this point in the history
…script (fixes #61 #70)
  • Loading branch information
Safeer Hussain authored and Safeer Hussain committed May 23, 2024
1 parent a079606 commit 802ff35
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,18 @@ dotnetProcess.stdout.on('end', () => {
let json;

try {
json = JSON.parse(stdout);
// Extract the JSON content between the markers
const startMarker = '<<<<<<START_JSON>>>>>>';
const endMarker = '<<<<<<END_JSON>>>>>>';
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.',
Expand Down
9 changes: 8 additions & 1 deletion lib/csharp-models-to-json/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.Extensions.Configuration;
using Ganss.IO;
using System.Text;

namespace CSharpModelsToJson
{
Expand Down Expand Up @@ -36,7 +37,13 @@ static void Main(string[] args)
}

string json = JsonSerializer.Serialize(files);
System.Console.WriteLine(json);

var sb = new StringBuilder();
sb.AppendLine("<<<<<<START_JSON>>>>>>");
sb.AppendLine(json);
sb.AppendLine("<<<<<<END_JSON>>>>>>");

System.Console.WriteLine(sb.ToString());
}

static List<string> getFileNames(List<string> includes, List<string> excludes) {
Expand Down

0 comments on commit 802ff35

Please sign in to comment.