https://stackoverflow.com/questions/72534815/expected-end-object-but-found-field-name

CSharp · Console

/Program.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters;
using System.IO;
using System.Data;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Reflection;
using System.Collections.ObjectModel;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;	

using NUnit.Framework; // Throws NUnit.Framework.AssertionException

//https://stackoverflow.com/questions/72534815/expected-end-object-but-found-field-name

	#region Manually created from Must

	public interface IMustConstraint { }

    public class RangeConstraint : IMustConstraint
	{
        public Range range { get; set; }
	}
	
    public class WildcardConstraint : IMustConstraint
	{
        public Wildcard wildcard { get; set; }
    }

	#endregion Manually created from Must

    public class Range
    {
        [JsonProperty("@timestamp")]
        public Timestamp Timestamp { get; set; }
    }

    public class Timestamp
    {
        public string gte { get; set; }
        public string lt { get; set; }
    }

    public class Wildcard
    {
        [JsonProperty("request.keyword")]
        public RequestKeyword RequestKeyword { get; set; }
    }

    public class RequestKeyword
    {
        public string value { get; set; }
        public bool case_insensitive { get; set; }
    }

    public class BoolQuery // Renamed from Bool for clarity
    {
        public List<IMustConstraint> must { get; set; } // Modified from List<Must>
    }

    public class Query
    {
        public BoolQuery @bool { get; set; }
    }

    public class Root
    {
        public Query query { get; set; }
    }

    class TestClass
    {
        public static void Test()
        {
			Root root = new ()
			{
				query = new ()
				{
					@bool = new ()
					{
						must = new ()
						{
							new RangeConstraint() { range = new () { Timestamp = new () { gte = "now-7d", lt = "now" } } },
							new WildcardConstraint() { wildcard = new () { RequestKeyword = new () { value = "/message/*/*-message/2c35669dd87e471faad1f90374d8d380/status", case_insensitive = true } } },
						},
					},
				},
			};
			
			var json = JsonConvert.SerializeObject(root, Formatting.Indented);
			
			Console.WriteLine(json);
			
			Assert.IsTrue(JToken.DeepEquals(JToken.Parse(json), JToken.Parse(GetRequiredJson())));
		}	
		
		static string GetRequiredJson() => @"{
  ""query"": {
    ""bool"": {
      ""must"": [
        {
          ""range"": {
            ""@timestamp"": {
              ""gte"": ""now-7d"",
              ""lt"": ""now""
            }
          }
        },
              {
          ""wildcard"": {
            ""request.keyword"": {
              ""value"": ""/message/*/*-message/2c35669dd87e471faad1f90374d8d380/status"",
              ""case_insensitive"": true
            }
          }
        }
      ]     
    }
  }
}";
	}

	public class Program
	{
		public static void Main()
		{
			Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , Environment.Version);
			Console.WriteLine("{0} version: {1}", typeof(JsonSerializer).Assembly.GetName().Name,  typeof(JsonSerializer).Assembly.FullName);
			Console.WriteLine();

			try
			{
				TestClass.Test();
			}
			catch (Exception ex)
			{
				Console.WriteLine("Failed with unhandled exception: ");
				Console.WriteLine(ex);
				throw;
			}
		}
	}
;