Skip to content

Consider json5 as template format #149

Description

@evorios

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure a similar issue has not already been created

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions