Skip to content

A new JsonConverterOptions attribute for new converter management #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Assets/Newtonsoft.Json.UnityConverters.Tests/Features.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Linq;
using NUnit.Framework;

namespace Newtonsoft.Json.UnityConverters.Tests
{
public class HiddenConverterTests
{
[Test]
public void AssertHiddenConverterNotRegistered()
{
var registered = UnityConverterInitializer.defaultUnityConvertersSettings.Converters
.Any(x => x.GetType() == typeof(TestHiddenJsonConverter));

Assert.IsFalse(registered, "TestHiddenJsonConverter registered, but should not.");
}

[Test]
public void AssertVisibleConverterRegistered()
{
var registered = UnityConverterInitializer.defaultUnityConvertersSettings.Converters
.Any(x => x.GetType() == typeof(TestVisibleJsonConverter));

Assert.IsTrue(registered, "TestVisibleJsonConverter not registered, but should be.");
}


[HideInJsonConverterSettings]
internal class TestHiddenJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType) => false;

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}


internal class TestVisibleJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType) => false;

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace Newtonsoft.Json.UnityConverters
{
/// <summary>
/// The attribute allows to hide JsonConverter from Json.Net converters settings
/// and exclude it from auto sync process.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class HideInJsonConverterSettings : Attribute { }
Comment on lines +9 to +10
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a doc comment on this type to explain how it works and why you would use it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try. But I'm not good at English))) Maybe you'll want to do it yourself later.

}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.UnityConverters.Configuration;
using Newtonsoft.Json.UnityConverters.Helpers;
Expand Down Expand Up @@ -195,6 +196,7 @@ private static IEnumerable<Type> FilterToJsonConvertersAndOrder(IEnumerable<Type
.Where(type
=> typeof(JsonConverter).IsAssignableFrom(type)
&& !type.IsAbstract && !type.IsGenericTypeDefinition
&& !type.IsDefined(typeof(HideInJsonConverterSettings))
&& type.GetConstructor(Array.Empty<Type>()) != null
)
.OrderBy(type => type.FullName);
Expand Down