Я пытаюсь понять, почему JSON.Net изменит исходный JObject (formSchema) в двух нижеприведенных случаях, где я добавляю JArray и JObject к объектам, которые я определил как vars.
Я предполагаюдве переменные, которые я создал, были самостоятельными объектами сами по себе, но, похоже, что это не так.
В первом случае requiredItems - это переменная JArray, в которую я хочу добавить токен «Certification» в formSchema,но я добавил метод Add для обязательных элементов. Не formSchema.
Во втором случае я добавляю certifyProperty к formSchema, используя метод Add для согласияProps. Свойство легко добавляется в formSchema без прямой ссылки на него.
Почему это так? Они связаны в памяти? Где в документах JSON.Net это объясняется? Я не могу его найти.
namespace JSONProps
{
class Program
{
static void Main(string[] args)
{
string jsonSchema = @"
{
""jsonSchema"": {
""type"": ""object"",
""title"": ""a title"",
""properties"": {
""consent"": {
""type"": ""object"",
""title"": ""Consent of Applicant"",
""required"": [
""applicantConsent""
],
""properties"": {
""applicantConsent"": {
""type"": ""boolean"",
""title"": ""I give my Consent"",
},
}
}
}
}
}
";
// First case
var formSchema = JObject.Parse(jsonSchema);
var requiredProps = formSchema["jsonSchema"]["properties"]["consent"]["required"] as JArray;
requiredProps.Add("certification");
// Second case
var consentProps = formSchema["jsonSchema"]["properties"]["consent"]["properties"] as JObject;
var certifyProperty = JObject.Parse(@" { ""type"" : ""boolean"", ""title"" : ""This is true."" } ");
consentProps.Add("certification", certifyProperty);
Console.WriteLine(formSchema.ToString());
}
}
}
$ dotnet run
{
"jsonSchema": {
"type": "object",
"title": "a title",
"properties": {
"consent": {
"type": "object",
"title": "Consent of Applicant",
"required": [
"applicantConsent",
"certification"
],
"properties": {
"applicantConsent": {
"type": "boolean",
"title": "I give my Consent"
},
"certification": {
"type": "boolean",
"title": "This is true."
}
}
}
}
}
}