GLTFUtility icon indicating copy to clipboard operation
GLTFUtility copied to clipboard

Custom JSON converters inside project should be isolated

Open rwetzold opened this issue 4 years ago • 0 comments

I have a rather big project and use JSON.net for other purposes as well and I am using other custom converters for Unity standard types (vector2/3...). I'd assume most big project do so. The thing is, that the converters are not compatible and I had to manually isolate the converters now between the calls.

I recommend to do so per default in GLTF. Here my changes to the importer class:

  1. Declare used converters on top:

public static class Importer { // only use specified converters to ensure consistent file format private static readonly List<JsonConverter> JSON_CONVERTERS = new List<JsonConverter> { new StringEnumConverter(), new KeyValuePairConverter(), new BinaryConverter(), new ColorRGBConverter(), new ColorRGBAConverter(), new EnumConverter(), new Matrix4x4Converter(), new QuaternionConverter(), new TranslationConverter(), new Vector2Converter(), new Vector3Converter() };

	public static JsonSerializerSettings GetDefaultJsonSettings()
	{
		return new JsonSerializerSettings
		{
			NullValueHandling = NullValueHandling.Ignore,
			Converters = JSON_CONVERTERS
		};
	}
  1. Use settings in each deserialization

     GLTFObject gltfObject = JsonConvert.DeserializeObject<GLTFObject>(json, GetDefaultJsonSettings());
    

rwetzold avatar Jun 17 '21 12:06 rwetzold