简体   繁体   中英

NewtonSoft JSON Parsing fails, since JSON Schema has a value as arrow function without quotes enclosed

I write an ASP.NET Core REST API. The end point return the JSON. The API parse JSON schema red from a file. The JSON Schema has some value as arrow function as given below.

The Newtonsoft unable parse the JSON Schema without quotes in arrow function in validation message as below.

      "ip": {
        "$id": "#/properties/ip",
        "type": "string",
        "title": "The Ip Schema",
        "default": "",
        "examples": [
          "111.123.789.654"
        ],
        "pattern": "^(\\d{1,3}\\.){3}\\d{1,3}$",
        "widget": {
          "formlyConfig": {
            "validation": {
              "messages": {
                "pattern": (error, field: FormlyFieldConfig) => `${ field.formControl.value } is not a valid IP Address`
              }
            }
          }
        }
      }

The following C# code fail, since JSON is not valid.

 var wraperobject = JObject.Parse(ui_schema);

If I add quotes like below as given below, the parsing works. I need to send it to UI without quotes, otherwise all consumer need to do manipulation at client side.

Please give me a solution.

  "ip": {
    "$id": "#/properties/ip",
    "type": "string",
    "title": "The Ip Schema",
    "default": "",
    "examples": [
      "111.123.789.654"
    ],
    "pattern": "^(\\d{1,3}\\.){3}\\d{1,3}$",
    "widget": {
      "formlyConfig": {
        "validation": {
          "messages": {
            "pattern": "(error, field: FormlyFieldConfig) => `${ field.formControl.value } is not a valid IP Address`"
          }
        }
      }
    }
  }

JSON should be able to have value as arrow function with quotes enclosed.

I can send it as string but need to escape the entire JSON to make it as a string. The UI side JSON parsing to be done.

Please let me know if there best alternative.

The value of a JSON property can be one of : object, array, string, number, "true", "false", or "null".

The value of "pattern" is none of those

"pattern": (error, field: FormlyFieldConfig) => `${ field.formControl.value } is not a valid IP Address`

Since that value isn't valid for JSON, JSON.Net (or any other parser) cannot interpret it.

When you quote your value, it becomes a valid string, which is why the parser can handle it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM