using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public class CrimeResultModel
[JsonProperty("criminal_record_list")]
public List<criminal_record_list> criminal_record_list{ get; set; }
public query query{ get; set; }
[JsonProperty("identity")]
public identity identity { get; set; }
public long CriminalRecordType { get; set; }
public class criminal_record_list
public string crime{ get; set; }
public int value{ get; set; }
public string number{ get; set; }
public string location{ get; set; }
public string date{ get; set; }
public string querytype{ get; set; }
public string reason{ get; set; }
public string identitytype{ get; set; }
public string name{ get; set; }
public string surname{ get; set; }
public string id{ get; set; }
public string mothername{ get; set; }
public string fathername{ get; set; }
public string birthlocation{ get; set; }
public string birthday{ get; set; }
public string birthmonth{ get; set; }
public string birthyear{ get; set; }
public string birthlocationCity{ get; set; }
public class ObjectAsObjectArrayConverter : JsonConverter
static IContractResolver DefaultResolver { get; } = JsonSerializer.Create().ContractResolver;
readonly IContractResolver resolver;
public ObjectAsObjectArrayConverter() : this(DefaultResolver) { }
public ObjectAsObjectArrayConverter(IContractResolver resolver) => this.resolver = resolver ?? throw new ArgumentNullException(nameof(resolver));
public override bool CanConvert(Type objectType)
if (objectType.IsPrimitive || objectType == typeof(string))
if (!(resolver.ResolveContract(objectType) is JsonObjectContract contract))
if (contract.DefaultCreator == null)
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
var contract = (serializer.ContractResolver.ResolveContract(value.GetType()) as JsonObjectContract) ?? throw new ArgumentException("Wrong contract type");
writer.WriteStartArray();
foreach (var property in contract.Properties.Where(p => ShouldSerialize(p, value)))
var propertyValue = property.ValueProvider.GetValue(value);
if (propertyValue == null && (serializer.NullValueHandling == NullValueHandling.Ignore || property.NullValueHandling == NullValueHandling.Ignore))
writer.WriteStartObject();
writer.WritePropertyName(property.PropertyName);
if (propertyValue == null)
else if (property.Converter != null && property.Converter.CanWrite)
property.Converter.WriteJson(writer, propertyValue, serializer);
serializer.Serialize(writer, propertyValue);
protected virtual bool ShouldSerialize(JsonProperty property, object value) =>
property.Readable && !property.Ignored && (property.ShouldSerialize == null || property.ShouldSerialize(value));
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
if (existingValue == null)
existingValue = serializer.ContractResolver.ResolveContract(objectType).DefaultCreator();
switch (reader.MoveToContentAndAssert().TokenType)
case JsonToken.StartArray:
while (reader.ReadToContentAndAssert().TokenType != JsonToken.EndArray)
switch (reader.TokenType)
case JsonToken.StartObject:
serializer.Populate(reader, existingValue);
throw new JsonSerializationException("Unexpected token type " + reader.TokenType.ToString());
case JsonToken.StartObject:
serializer.Populate(reader, existingValue);
throw new JsonSerializationException("Unexpected token type " + reader.TokenType.ToString());
public static partial class JsonExtensions
public static JsonReader ReadToContentAndAssert(this JsonReader reader) =>
reader.ReadAndAssert().MoveToContentAndAssert();
public static JsonReader MoveToContentAndAssert(this JsonReader reader)
throw new ArgumentNullException();
if (reader.TokenType == JsonToken.None)
while (reader.TokenType == JsonToken.Comment)
public static JsonReader ReadAndAssert(this JsonReader reader)
throw new ArgumentNullException();
throw new JsonReaderException("Unexpected end of JSON stream.");
public static void Test()
var jsonString = GetJson();
var inputSettings = new JsonSerializerSettings
Converters = { new ObjectAsObjectArrayConverter() },
var result = JsonConvert.DeserializeObject<CrimeResultModel>(jsonString, inputSettings);
var newJson = JsonConvert.SerializeObject(result, Formatting.Indented);
Console.WriteLine("Re-serialized {0}", result);
Console.WriteLine(newJson);
Assert.AreEqual(result.query.reason, "TEST");
Assert.AreEqual(result.query.location, "xx");
Assert.AreEqual(result.identity.name, "xx");
Assert.AreEqual(result.identity.birthlocationCity, "xx");
static string GetJson() => @"[{""query"":[{""number"":""0000-2022-64129734 / 19:26""},{""location"":""xx""},{""date"":""01.06.2022""},{""querytype"":""uu""},{""reason"":""TEST""},{""identitytype"":""xx""}]},{""identity"":[{""name"":""xx""},{""surname"":""xx""},{""id"":""xx""},{""mothername"":""xx""},{""fathername"":""xx""},{""birthlocation"":""xx""},{""birthday"":""05""},{""birthmonth"":""05""},{""birthyear"":""1964""},{""birthlocationCity"":""xx""}]},{""criminal_record_list"":[[{""crime"":""xx""},{""value"":3}]]}]";
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("Failed with unhandled exception: ");