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

Implement robust JSON isolation between .NET console app and Node.js #78

Merged
merged 1 commit into from
May 24, 2024
Merged
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
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