using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
There is no way to do what you want out of the box with Json.NET because
- As noted by Marc Gravell, [JsonPropertyAttribute](https:
- The trick from [this answer](https:
As a workaround, you could create a [custom contract resolver](https:
private int colliderType = 0;
private int ignoreMe = 0;
public void SetColliderType(int type) => colliderType = type;
public class IncludeSerializedFieldsResolver : DefaultContractResolver
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
var property = base.CreateProperty(member, memberSerialization);
if (member is FieldInfo f && !f.IsPublic
&& Attribute.IsDefined(f, typeof(SerializedFieldAttribute))
&& !Attribute.IsDefined(f, typeof(JsonIgnoreAttribute)))
property.Readable = property.Writable = true;
protected override List<MemberInfo> GetSerializableMembers(Type objectType)
var members = base.GetSerializableMembers(objectType);
var initialCount = members.Count;
var toAdd = objectType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic)
.Where(f => Attribute.IsDefined(f, typeof(SerializedFieldAttribute))
&& !Attribute.IsDefined(f, typeof(JsonIgnoreAttribute)))
.Where(f => members.IndexOf(f, 0, initialCount) < 0);
[System.AttributeUsage(System.AttributeTargets.Field, AllowMultiple = false)]
public class SerializedFieldAttribute : Attribute { }
public static void Test()
var rootModel = new MyClass();
rootModel.SetColliderType(22);
IContractResolver resolver = new IncludeSerializedFieldsResolver {
var settings = new JsonSerializerSettings {
ContractResolver = resolver,
var json = JsonConvert.SerializeObject(rootModel, settings);
Assert.AreEqual(json, """{"colliderType":22}""");
var myClass2 = JsonConvert.DeserializeObject<MyClass>(json, settings);
var json2 = JsonConvert.SerializeObject(myClass2, settings);
Assert.AreEqual(json2, """{"colliderType":22}""");
public static void Main()
Console.WriteLine("Environment version: {0} ({1}), {2}", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , Environment.Version, Environment.OSVersion);
Console.WriteLine("{0} version: {1}", typeof(JsonSerializer).Namespace, typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");