Skip to content

Commit

Permalink
Json.NET Unity Converters 1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
applejag committed Feb 5, 2022
1 parent 0355a4f commit 6064d12
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 11 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Unity Converters for Newtonsoft.Json changelog

## 1.4.0 (2022-02-05)

- Added support for `UnityEngine.AddressableAssets.AssetReference`.
The new `AssetReferenceConverter` is only included in the build if your
project contains the `com.unity.addressables` package.
([#67](https://github.com/jilleJr/Newtonsoft.Json-for-Unity.Converters/pull/67))

This automatic inclusion relies on AssemblyDefinition version defines, which
was introduced in Unity 2019.1.x. To enable the `AssetReferenceConverter` in
earlier versions of Unity, please add `HAVE_MODULE_ADDRESSABLES` to your
project's "Scripting Define Symbols" found in the
"Project Settings" -> "Player" -> "Other Settings" panel.

## 1.3.0 (2021-10-21)

- Changed the following modules to be automatically excluded from compilation
Expand Down
3 changes: 2 additions & 1 deletion Editor/UnityConvertersConfigEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ private void FoldoutConvertersList(SerializedProperty property, AnimBool fadedAn
.Select(o => (
serializedProperty: o,
type: TypeCache.FindType(o.FindPropertyRelative(nameof(ConverterConfig.converterName)).stringValue)
));
))
.OrderBy(o => o.type.FullName);

foreach (var namespaceGroup in allConfigsWithType.GroupBy(o => GetTypeNamespace(o.type)))
{
Expand Down
2 changes: 1 addition & 1 deletion Newtonsoft.Json.UnityConverters.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
"define": "HAVE_MODULE_PHYSICS2D"
}
]
}
}
3 changes: 3 additions & 0 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Newtonsoft.Json.UnityConverters.Addressables")]
[assembly: InternalsVisibleTo("Newtonsoft.Json.UnityConverters.Editor")]
[assembly: InternalsVisibleTo("Newtonsoft.Json.UnityConverters.Tests")]
[assembly: InternalsVisibleTo("Newtonsoft.Json.UnityConverters.Tests.Addressables")]

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

43 changes: 43 additions & 0 deletions UnityConverters/Addressables/AssetReferenceConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using Newtonsoft.Json.UnityConverters.Helpers;
using UnityEngine.AddressableAssets;

namespace Newtonsoft.Json.UnityConverters.Addressables
{
public class AssetReferenceConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(AssetReference);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
{
return null;
}

if (reader.TokenType == JsonToken.String && reader.Value is string stringValue)
{
return new AssetReference(stringValue);
}
else
{
throw reader.CreateSerializationException($"Expected string when reading UnityEngine.Addressables.AssetReference type, got '{reader.TokenType}' <{reader.Value}>.");
}
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value is null || string.IsNullOrEmpty(((AssetReference)value).AssetGUID))
{
writer.WriteNull();
}
else
{
writer.WriteValue(((AssetReference)value).AssetGUID);
}
}
}
}

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,24 @@
{
"name": "Newtonsoft.Json.UnityConverters.Addressables",
"references": [
"Unity.Addressables",
"Newtonsoft.Json.UnityConverters"
],
"optionalUnityReferences": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [
"HAVE_MODULE_ADDRESSABLES"
],
"versionDefines": [
{
"name": "com.unity.addressables",
"expression": "",
"define": "HAVE_MODULE_ADDRESSABLES"
}
]
}

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

4 changes: 0 additions & 4 deletions UnityConverters/Properties/AssemblyInfo.cs

This file was deleted.

10 changes: 8 additions & 2 deletions UnityConverters/UnityConverterInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static class UnityConverterInitializer
/// <summary>
/// The default <see cref="JsonSerializerSettings"/> given by <c>Newtonsoft.Json-for-Unity.Converters</c>
/// </summary>
///
///
/// <remarks>
/// All its properties stay default, but the <c>Converters</c> includes below:
/// 1. Any custom <see cref="JsonConverter"/> has constructor without parameters.
Expand Down Expand Up @@ -178,7 +178,13 @@ private static IEnumerable<Type> FindFilteredUnityConverters(UnityConvertersConf
/// <returns>The types.</returns>
internal static IEnumerable<Type> FindUnityConverters()
{
return FilterToJsonConvertersAndOrder(typeof(UnityConverterInitializer).Assembly.GetTypes());
var typesFromPackageDomains = AppDomain.CurrentDomain.GetAssemblies()
.Select(dll => dll.GetLoadableTypes()
.Where(type => type.Namespace?.StartsWith("Newtonsoft.Json.UnityConverters") == true)
)
.SelectMany(types => types)
.OrderBy(type => type.FullName);
return FilterToJsonConvertersAndOrder(typesFromPackageDomains);
}

private static IEnumerable<Type> FindFilteredJsonNetConverters(UnityConvertersConfig config)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jillejr.newtonsoft.json-for-unity.converters",
"displayName": "Json.NET Converters of Unity types",
"version": "1.3.0",
"version": "1.4.0",
"unity": "2018.1",
"description": "This package contains converters to and from common Unity types for Newtonsoft.Json. Types such as Vector2, Vector3, Matrix4x4, Quaternions, Color, even ScriptableObject, and more.\n\nGoes hand in hand with the jillejr.newtonsoft.json-for-unity package.\n\nThis package is licensed under The MIT License (MIT)\n\nCopyright © 2019 Kalle Jillheden (jilleJr)\nhttps://github.com/jilleJr/Newtonsoft.Json-for-Unity.Converters\n\nCopyright © 2020 Wanzyee Studio\nhttp://wanzyeestudio.blogspot.com/2017/03/jsonnet-converters.html\n\nCopyright © 2007 ParentElement\nhttps://github.com/ianmacgillivray/Json-NET-for-Unity\n\nCopyright © 2017 .NET Foundation and Contributors\nhttps://github.com/dotnet/runtime\n\nSee full copyrights in LICENSE.md inside package",
"main": "index.js",
Expand Down

0 comments on commit 6064d12

Please sign in to comment.