applejag / Newtonsoft.Json-for-Unity

Newtonsoft.Json (Json.NET) 10.0.3, 11.0.2, 12.0.3, & 13.0.1 for Unity IL2CPP builds, available via Unity Package Manager

Home Page:https://github.com/jilleJr/Newtonsoft.Json-for-Unity

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Only install json-for-unity (Newtonsoft.Json) into specific assembly

rdcm opened this issue · comments

Hi!

In my unity project, I have several assemblies, in "Contracts" defined interfaces and concrete implementations in "Impl".
After installation package json-for-unity visible for all assemblies. Could I restrict visibility for this package? Would be perfect if this code:

var json = JsonConvert.SerializeObject("....");

will be compiled only in "Impl" assembly.

@rdcm Why yes you can!

Given you have your two assemblies as assembly definitions (.asmdef) in your Unity project, there is a setting on those definition files to only reference specific assemblies.

By default, your assembly definition will reference all assemblies loaded into the project. But you can disable this by toggling "Override References"

image

Screenshot taken from Unity 2019.4.2f1. In older/newer versions, the setting may be named differently.

By enabling the "Override References" setting, a new setting appears: the "Assembly References" list.

With the list empty, it won't reference this package's Newtonsoft.Json assembly. The following script does not compile:

using UnityEngine;

public class ContractsScript : MonoBehaviour
{
    void Start()
    {
        var json = Newtonsoft.Json.JsonConvert.SerializeObject("....");
    }
}
Assets\Contracts\ContractsScript.cs(10,20): error CS0103: The name 'Newtonsoft' does not exist in the current context

If your assembly still relies on other assemblies, you can of course add them manually.

There's no way to only blacklist the Newtonsoft.Json assembly and still automatically reference all other assemblies though.

@jilleJr Thanks for the answer, you save my day.

But for example, extenject provides a more simple way for including dependencies into the project.
image

As you can see in Assembly Definition References explicit reference to zenject and without this ref I get a compile error. No need to enable Override References.

Is it possible to provide the same mechanism for including dependencies?

@rdcm Unity sadly handles precompiled assemblies (.DLL's) and assembly definitions (.asmdef) very differently.

As asmdef are only whitelist and assemblies are by default always loaded but optionally opt in to whitelist, I do not see issue here.

I won't refactor Newtonsoft.Json to work as an asmdef due to a lot of reasons (some of which: It's a ton of source code, faster and smaller in size to deliver as a DLL. It's a ton of code to refactor. It works as-is).

By using the "Override References" list, you should be able to get the same level of control on your loaded assemblies. Just that you'd have to work with 2 lists instead of 1.

image

Image taken from Unity docs: "Assembly definitions / Referencing a precompiled, plugin assembly"
https://docs.unity3d.com/Manual/ScriptCompilationAssemblyDefinitionFiles.html#reference-precompiled-assembly

@jilleJr Ok, thanks for your answers!
I close this issue.