Prerequisites
Description
To use the scripting engine on mock templates, most of times there is a need to write an invalid JSON structure, like this:
{
"request": {
"method": "PUT",
"route": "customers/{id}"
"condition": "<#=
var isAuthCorrect = Request.Header["Authorization"]?.ToString() == "Basic bG9naW46cGFzc3dvcmQ=";
return isAuthCorrect;
#>"
},
"response": {
"status": "OK",
"body": {
"url": "<#= Request.Url?.ToString() #>",
"customerId": "<#= Request.Route["id"]?.ToString() #>",
"acceptHeader": "<#= Request.Header["Content-Type"]?.ToString() #>",
"queryString": "<#= Request.Query["dummy"]?.ToString() #>",
"requestBodyAttribute": "<#= Request.Body["address"]?[0]?.ToString() #>"
},
"indented": false
}
}
Proposed solution
json5 seems to be suitable alternative to write templates with C# scripts, as it is more permissive on content, like the example bellow, which is a completely valid json5:
{
request: {
method: 'PUT',
route: 'customers/{id}',
condition: '<#=\
var isAuthCorrect = Request.Header["Authorization"]?.ToString() == "Basic bG9naW46cGFzc3dvcmQ=";\
return isAuthCorrect;\
#>'
},
response: {
status: 'OK',
body: {
url: '<#= Request.Url?.ToString() #>',
customerId: '<#= Request.Route["id"]?.ToString() #>',
acceptHeader: '<#= Request.Header["Content-Type"]?.ToString() #>',
queryString: '<#= Request.Query["dummy"]?.ToString() #>',
requestBodyAttribute: '<#= Request.Body["address"]?[0]?.ToString() #>'
},
indented: false
}
}
Alternatives
nop
Additional context
There are some disadvantages using json5:
- no fully supported by vscode;
- need to append c#-strings by symbol '\' for multiline code.
Prerequisites
Description
To use the scripting engine on mock templates, most of times there is a need to write an invalid JSON structure, like this:
{ "request": { "method": "PUT", "route": "customers/{id}" "condition": "<#= var isAuthCorrect = Request.Header["Authorization"]?.ToString() == "Basic bG9naW46cGFzc3dvcmQ="; return isAuthCorrect; #>" }, "response": { "status": "OK", "body": { "url": "<#= Request.Url?.ToString() #>", "customerId": "<#= Request.Route["id"]?.ToString() #>", "acceptHeader": "<#= Request.Header["Content-Type"]?.ToString() #>", "queryString": "<#= Request.Query["dummy"]?.ToString() #>", "requestBodyAttribute": "<#= Request.Body["address"]?[0]?.ToString() #>" }, "indented": false } }Proposed solution
json5 seems to be suitable alternative to write templates with C# scripts, as it is more permissive on content, like the example bellow, which is a completely valid json5:
Alternatives
nop
Additional context
There are some disadvantages using json5: