Custom Vector3 adapter for com.unity.serialization package, missing public properties?

Hi,

I’m trying to use the com.unity.serialization package to deserialize a json coming from an external source (simplified version):

{
    "filename": "body.obj",
    "local": {
        "origin": [0.0, 1.0, 0.0],
        "rotation_xyz": [315.0, 0.0, 0.0],
    }
}

As you can see, it uses arrays to represent Vector3, instead of what the com.unity.serialization expect by default:

{
    "filename": "body.obj",
    "local": {
        "origin": {
            "x": 0.0,
            "y": 1.0,
            "z": 0.0
        },
        "rotation_xyz": {
            "x": 315.0,
            "y": 0.0,
            "z": 0.0
        }
    }
}

So, I wrote a custom adapter and had to end with (still needs more exception checks for wrong formatted arrays):

class Vector3ArrayAdapter : IJsonAdapter<Vector3>
{
    public Vector3 Deserialize(in JsonDeserializationContext<Vector3> context)
    {
        if (context.SerializedValue.Type == TokenType.Array) {
            SerializedArrayView arrayView = context.SerializedValue.AsArrayView();
            SerializedArrayView.Enumerator enumerator = arrayView.GetEnumerator();
            enumerator.MoveNext();
            float x = enumerator.Current.AsFloat();
            enumerator.MoveNext();
            float y = enumerator.Current.AsFloat();
            enumerator.MoveNext();
            float z = enumerator.Current.AsFloat();
            return new Vector3(x, y, z);
        }
        return context.ContinueVisitation();
    }

    public void Serialize(in JsonSerializationContext<Vector3> context, Vector3 value)
    {
        using var objectScope = context.Writer.WriteArrayScope();
        context.Writer.WriteValue(value.x);
        context.Writer.WriteValue(value.y);
        context.Writer.WriteValue(value.z);
    }
}

It works, but the Deserialize method is ugly. As you can see, I have to use an SerializedArrayView.Enumerator and couldn’t use the [] operator. I’d like to be able to write:

public Vector3 Deserialize(in JsonDeserializationContext<Vector3> context)
{
    if (context.SerializedValue.Type == TokenType.Array) {
        SerializedArrayView arrayView = context.SerializedValue.AsArrayView();
        return new Vector3(arrayView[0].AsFloat(), arrayView[1].AsFloat(), arrayView[2].AsFloat());
    }
    return context.ContinueVisitation();
}

In Unity’s package code, there is everything I need, but they arn’t public… Visual Studio say there is 0 references to those methods, so unless it’s made public, they are useless. See line 135 of SerializedArrayView.cs:

Is it done on purpose or just someone forget to make this public? Or did I miss something?

Edit: just saw when posting that the [] method is using the enumerator, so switching to [] will be less perfromant because of the use of a new enumerator for each index instead of reusing it as I did.

Not really, it’s an explicit interface implementation which just requires you to cast the object to IList<SerializedValueView> in order to access the indexer. Explicit interface implementations can be used as a mechanism to hide members of the interface that you want to steer consumers away from. Considering that you subsequently found that the member would have some amount of overhead reading an element, there’s good reason to hide the member away.

Okey! I wasn’t aware of “explicit interface implementation”.

I tested and it’s possible to do this (still missing safety checks for bad json):

public Vector3 Deserialize(in JsonDeserializationContext<Vector3> context)
{
    if (context.SerializedValue.Type == TokenType.Array) {
        IList<SerializedValueView> arrayView = context.SerializedValue.AsArrayView();
        return new Vector3(arrayView[0].AsFloat(), arrayView[1].AsFloat(), arrayView[2].AsFloat());
    }

    return context.ContinueVisitation();
}

But it’s slower, so I’ll keep the (longer code) first version.

Thank you!

(And happy to provide my array adapter to everyone, I didn’t found samples online)